0
votes

Is there a simple way for me to add to an AWS SQS queue from an AWS lambda?

From my google search research findings, it seems that the suggested workflow is to publish to an SNS topic, which can then push the message to the queue.

Question: Is there a reason to use SNS on top of SQS instead of using SQS only?

Context (FYI): My scenario is that I am using SQS as a logger queue. I need to push logs to this queue from a lambda.

2
Yes, you can do that. You have to send a message to SQS via lambda. You don't need to set up SNS -> SQS. If you want to do that with node js sdk then I can provide a snippet. Just let me know!Nikolay Vetrov
I thought that SNS on top of the SQS is just to improve the flexibility. The SNS can send the message to multiple AWS services (SQS) at once.willisc
@ChouW Yeah that is certainly a benefit, without adding too much complexity. In fact, considering how simple it is to send SNS from Lambda compared to pushing messages directly to SQS (requires explicit authN), using SNS might even be a simpler approachJames Wierzba

2 Answers

1
votes

It is possible to push from Lambda to SQS, you can try and make sense of the code below not sure if its gonna help or not

 public async Task<string> FunctionHandler(ILambdaContext context, int count )
        {
            {
                StringBuilder sb = new StringBuilder(1024);
                using (StringWriter sr = new StringWriter(sb))

                {
                    try
                    {                  
                        var credentials = new BasicAWSCredentials("AWS_ACCESS_KEY", "AWS_SECRET_KEY");
                        var client = new AmazonSQSClient(credentials, RegionEndpoint.EUWest1);

                        SendMessageRequest request = new SendMessageRequest();
                        request.DelaySeconds = 0;
                        request.QueueUrl = "queue url";
                        request.MessageBody = "text of what you logging to SQS";
                        for (int i = 0; i < count; i++)
                        {
                            SendMessageResponse response = client.SendMessageAsync(request);
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    return "";
                }
            }
        }
0
votes

You can just use the AWS SDK to send your messages to your SQS queue. There is no magic here.