How to Invoke AWS Lambda Functions from Amazon SQS Message
Introduction
Hello, Hemanth from the Alliance Department here. In this blog, I demonstrate how to trigger AWS Lambda invocations when a message is sent from Amazon SQS.
AWS
Is a secure cloud service platform that offers compute power, database storage, content delivery, network, and other functionality to help businesses scale and grow. It is one of the first cloud vendors to start services in the year 2006. It offers all the 3 service models namely IAAS, PAAS, and SAAS. Some of the notable domains in AWS are Compute, Migration, Storage, Network and Content Delivery, Management Tools, Database, Messaging, Security and Identity Compliance, and many more.
AWS Lambda
A serverless compute service which runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests via Amazon API Gateway, changes to objects in S3, and many others.
Amazon SQS
It is a web service that enables message exchange between software components at any volume without message loss or the need for additional services to be accessible. It contains standard building blocks like cost allocation tags and dead-letter queues and offers a general web services API that may be used through any programming language that the AWS SDK supports.
Demo
Create a Lambda Function
Go to the AWS Management Console and navigate to AWS Lambda and click on "Create Function". Enter a function name, select the runtime as seen below, and click on "Create function". Make the necessary changes to the Lambda function code and click "Deploy". Go to the "Configuration" tab, then "Permissions", and select the role name. Click on "Add permissions" and attach the necessary policies. Search for "SQS", select "AWSLambdaSQSQueueExecutionRole", and click on "Add permissions". Policy successfully added to role
Create an SQS Queue
In the AWS Console, navigate to Amazon SQS and click on "Create queue". Provide a name for the queue. Keep other settings default and click on "Create queue" The queue has been successfully created
Configure Lambda Function Trigger
In the Lambda triggers section, select "Configure Lambda function trigger".
Select the previously created Lambda function and click "Save".
In the SQS queue, click on "Send and receive messages".
Write a message and click "Send message".
Verify Lambda Invocation
Navigate back to the Lambda function screen and check the "Monitor" tab. Click on "View CloudWatch logs". Click on the log stream to view the logs. The logs should show the message sent from SQS, indicating that the Lambda function was successfully invoked.
Conclusion
I explained in this blog how to configure Amazon SQS messages to trigger AWS Lambda. Through this integration, scalable and decoupled architectures may be created by utilizing the serverless computation capabilities of AWS Lambda and the dependable message queuing of Amazon SQS. This configuration is especially helpful for building event-driven systems with asynchronous communication between various parts.