# How to use queues in your serverless app

**URL:** <https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314>\
**Category:** Chapter Comments\
**Created:** [February 10, 2021, 2:25am UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314 "2021-02-10T02:25:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [February 10, 2021, 2:25am UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314/1 "2021-02-10T02:25:34Z")

</div>

Link to chapter — [https://serverless-stack.com/examples/how-to-use-queues-in-your-serverless-app.html](https://serverless-stack.com/examples/how-to-use-queues-in-your-serverless-app.html)

---

<div class="post-metadata">

**Author:** ![Udaygajavalli](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/udaygajavalli/32/1312_2.png) [@Udaygajavalli](https://discourse.sst.dev/u/Udaygajavalli)\
**Post date:** [March 27, 2022, 9:34am UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314/2 "2022-03-27T09:34:54Z")

</div>

Is there any way to retrieve MessageBody in the consumer lambda function?

---

<div class="post-metadata">

**Author:** ![villecoder](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/villecoder/32/1345_2.png) [@villecoder](https://discourse.sst.dev/u/villecoder)\
**Post date:** [May 12, 2022, 6:07pm UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314/3 "2022-05-12T18:07:25Z")

</div>

The example code doesn’t work as is (as of the time of this post), nor does it show how to process a consumed event from the queue. After downloading the example, here are the modifications that I made.

First, **queue.QueueName will not work in the lambda**. The example sets the environment variable `queueUrl` to queue.queueName and then uses it in `lambda.ts`. However, if you run it as-is, you’ll get an error that you’re referencing an unknown endpoint. **What you should do** is use the `sqs` object to get the QueueUrl. So you go from this:

```nohighlight
  await sqs.sendMessage({ QueueUrl: process.env.queueUrl });

```

to this

```nohighlight
  const queueUrl = await sqs.getQueueUrl({ QueueName: process.env.queueUrl }).promise();
  await sqs.sendMessage({ QueueUrl: queueUrl.QueueUrl });

```

Second, **the example does not show what arguments are passed to the consumer**. You can track this down by looking for other SQS consumer examples. For typescript, you’ll change the consumer to this:

```nohighlight
export const main: SQSHandler = async(event) => {
  console.log('Event received!');

  event.Records.forEach((record) => {
    console.log(JSON.stringify(record));
  });
}

```

Notice that we’ve typed the function to the type of handler the AWS SDK expects. It will help you understand what types of parameters and their properties exist.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [May 12, 2022, 6:27pm UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314/4 "2022-05-12T18:27:11Z")

</div>

Oh that’s not good. We’ll have it checked out!

---

<div class="post-metadata">

**Author:** ![jpbarbosa](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jpbarbosa/32/1360_2.png) [@jpbarbosa](https://discourse.sst.dev/u/jpbarbosa)\
**Post date:** [June 9, 2022, 7:04pm UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314/5 "2022-06-09T19:04:13Z")

</div>

I had the same problem and @villecoder solution worked like a charm. Nonetheless, I’ve found another way to fix the issue by replacing queueName with queueUrl:

```auto
// MyStack.ts

const api = new Api(stack, "Api", {
  defaults: {
    function: {
      // Pass in the queue to our API
      environment: {
        queueUrl: queue.queueUrl, // Replaced queueName with queueUrl
      },
    },
  },
  routes: {
    "POST /": "functions/lambda.handler",
  },
});

```

**Original error:**

```auto
ERROR UnknownEndpoint: Inaccessible host: `my-queue' at port `undefined'. This service may not be available in the `us-east-1' region.

```

---

<div class="post-metadata">

**Author:** ![tikiyan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/tikiyan/32/1414_2.png) [@tikiyan](https://discourse.sst.dev/u/tikiyan)\
**Post date:** [February 15, 2023, 11:01pm UTC](https://discourse.sst.dev/t/how-to-use-queues-in-your-serverless-app/2314/6 "2023-02-15T23:01:10Z")

</div>

Did you figure out how to make this work?

I have created this issue but no one is replying
