AWS Lambda with SQS — Setup SQS Trigger to Lambda

CodeStax.Ai
5 min readFeb 21, 2023

--

AWS Lambda is an event-driven, server-less computing platform provided by Amazon. It is a fully server-less computing service that runs code in response to events and automatically manages the computing resources required.

Prerequisite for this article:

Before getting into the article you should have a basic idea about lambda and SQS in AWS. If you guys have no idea about those check out our previous articles to get an understanding about these services.

Things we are going to do:

  • Create a simple queue in SQS service by AWS.
  • Create a lambda function with just log statement.
  • Adding SQS trigger to the Lambda and its configurations.
  • Add message/data to the queue.
  • Check Cloudwatch logs for lambda.

Create a simple queue in SQS service by AWS

I have created a sample queue without any Dead Letter Queue and with all default settings provided by the AWS. To check how I have configured the Queue checkout my previous article: [AWS] Tutorial SQS (Simple Queue Service).

Create a lambda function with a log statement

Now lets go ahead and create a simple lambda function with just the console log statement.

I have a create a lambda function with node js environment.

So this is the code in the lambda function. It just logs the event received to the console. If you are not familiar with javascript environment you can use any other programming environment.

Adding SQS trigger to the Lambda and its configurations:

Before adding the trigger to the lambda kindly check the permissions of the lambda function. We have to provide the lambda permissions to access the SQS service. Go to configuration tab in your lambda function and click on the execution role and It will take you to IAM console where you can add the access.

As you can see the role has only one access that it to execute the lambda function. Click the Add permission and attach policies, it opens a window with list of many access policies as below.

Select any policy from the search bar. Type SQS in the search bar and select the AmazonSQSFullAccess policy and click the attach policy to add the access.

Now you can see there are 2 policies for our lambda role.

Lets add the SQS trigger for our lambda function. Click the add trigger option in the lambda function.

In trigger configuration:

  • Select the trigger source as SQS and choose the queue that we created already.
  • Activate trigger checkbox — This helps to trigger the lambda function when there is a message in the queue.
  • Batch size — This determines the messages can be sent from the queue as a batch of messages and process at a single instance of the lambda for cost optimization.
  • Batch window — The trigger will be activated based on either batch size or batch window time. For this article lets keep it as 0 you can set it up for your purpose
  • Report batch item failures — This is used to setup dead letter queues. Eg: When you have a sent a batch of 5 messages to the lambda and 4 have successfully passed if you have no enabled this option all the 5 messages are considered as failures and will get added to DLQ. By checking this box only the failed messages will get added to the DLQ.
  • Rest of the options can be left with the default values. Now click on Add.

Now you can see that the trigger has been setup for our lambda function.

Add message/data to the queue:

Now lets go ahead and start adding messages to the queue.

  • Go to SQS in AWS console.
  • Click send and receive message, type in some content in message body and click send.
  • Message has been added to the queue, now it should have triggered our lambda. Lets check the Cloudwatch for our lambda function.

Check Cloudwatch logs of our Lambda function:

Go to cloudwatch from console. Click the logs and log groups and search for your lambda function and look into the log streams. (Note : You can see this directly from lambda too by checking the monitor tab in the lambda function).

As you can our log statement has printed the message that was received by our Lambda function.

And with that we have covered how we can setup our SQS as a trigger to our lambda function. My next article will be dealing with the dead letter queue with our current lambda function and handling the failed messages.

About the Author

Sangar Naarayanan R is a Software Development Engineer in CodeStax.Ai. With a hands-on experience and good knowledge in various technologies and looking forward how to upgrade his own skills by researching every day and provide the best possible solutions.

About CodeStax.Ai

At CodeStax.Ai, we stand at the nexus of innovation and enterprise solutions, offering technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.

But the real magic? It’s our tech tribe behind the scenes. If you’ve got a knack for innovation and a passion for redefining the norm, we’ve got the perfect tech playground for you. CodeStax.Ai offers more than a job — it’s a journey into the very heart of what’s next. Join us, and be part of the revolution that’s redefining the enterprise tech landscape.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

CodeStax.Ai
CodeStax.Ai

Written by CodeStax.Ai

Tech tales from our powerhouse Software Engineering team!

No responses yet