https://serverless-stack.com/ logo
#help
Title
# help
g

Greg

02/21/2022, 7:44 AM
Hey guys probably an easy one, but can’t seem to find it. How I can access the
queue
-url from within a sqs consumer? I would like to add an other message to the queue when the lambda is finished - I would like to chain commands this way. So I’m looking to achieve the following functionality . Unfortunately this doesn’t work since
Property 'setDefaultFunctionProps' does not exist on type 'Queue'.
Copy code
const queue = new sst.Queue(this, "Queue", {
      consumer: {
        function: "src/consumer.main",
        consumerProps: {
          batchSize: 1,
        },
      },
    })

    queue.setDefaultFunctionProps({
      environment: { queueUrl: queue.sqsQueue.queueUrl },
    })
a

Arpad

02/21/2022, 7:47 AM
There is a property in
event.Records
that you can use which AWS sends, you get the ARN of the queue in
eventSourceARN
, https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
not sure if it helps though
g

Greg

02/21/2022, 7:56 AM
thanks for the idea @Arpad. Yes I can use that to compute the sqs queue url for sure. Doesn’t seems like the prettiest solution tho, but it’ll work for now 💪
a

Arpad

02/21/2022, 8:13 AM
It seems like you can create the function separately with
new Function(...)
and pass that function as consumer
so you can create the queue, create the function, then addConsumer to the queue
g

Greg

02/21/2022, 9:15 AM
So for future reference, the final solution was this:
Copy code
const queue = new sst.Queue(this, "Queue", {})

    queue.addConsumer(this, {
      function: {
        handler: "src/consumer.main",
        environment: {
          queueUrl: queue.sqsQueue.queueUrl,
        },
        permissions: [queue],
      },

      consumerProps: {
        batchSize: 1,
      },
    })
a

Arpad

02/21/2022, 9:18 AM
looks great!
don't forget to add a dead letter queue in case some of your messages can't be processed haha, got bit in the ass once for this
3 Views