Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
August 17, 2023

AWS Lambda with SQS — Setup SQS Trigger to Lambda

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

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.

CodeStax.Ai
Profile
August 17, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles