Introduction

In today’s cloud computing world, AWS Lambda and Amazon SQS (Simple Queue Service) are powerful services for building scalable applications. Lambda allows you to run code without managing servers, while SQS acts as a reliable message queue that stores messages until they are processed. When combined, they create an automated system where Lambda functions are triggered by messages in an SQS queue.

Step 1. Create an SQS Queue.

First, log in to your AWS Management Console, and search for SQS.

Create a Queue

Now you have a queue that will store messages to be processed by Lambda.

Step 2. Set Up a Lambda Function.

Your Lambda function is now set up and ready to be triggered by the SQS queue.

Step 3. Set Lambda to Trigger from SQS.

Add SQS as a Trigger

Now, Lambda is connected to your SQS queue and will run whenever a new message is added to the queue.

Step 4. Give Lambda Permission to Access SQS.

  1. Go to the IAM Console and search for Roles.
  2. Attach Permission
    • Find the role Lambda is using (usually named lambda-role).
    • Click on Attach policies.
    • Search for AmazonSQSFullAccess and check the box next to it.
    • Click Attach policy.

Now, your Lambda function has permission to interact with SQS.

Step 5. Write Lambda Code to Process Messages.

Add Code to Your Lambda Function

Scroll down to the Function code section. You can use a simple Python script like this.

import json

def lambda_handler(event, context):
    for record in event['Records']:
        print(f"Processing message: {record['body']}")
        # Add any processing logic here (e.g., save to database)
    return {
        'statusCode': 200,
        'body': json.dumps('Message processed successfully')
    }

This code will print the message body to the logs whenever it is triggered.

Step 6. Test the Integration.

  1. Send a Test Message to SQS
    • Go to your SQS Queue (MyEventQueue).
    • Click on Send a message.
    • Type a message in the body (e.g., {"message": "Hello Lambda!"}).
    • Click Send Message.
  2. Check Lambda Logs
    • Go back to your Lambda Console.
    • Click on Monitor and then View logs in CloudWatch.
      Monitor
    • In the Cloudwatch just scroll down and you will see log streams just click on a recent log.
      Open log
    • View the processed message. Hello Lambda.
      Output

Conclusion

By connecting AWS Lambda with Amazon SQS, you’ve set up an automated system where Lambda functions process messages from the queue without needing to manage servers. This integration makes your application more scalable and efficient, allowing you to focus on the logic while AWS handles the heavy lifting. With just a few steps, you’ve created a powerful, event-driven architecture that can handle tasks like real-time data processing and asynchronous workflows.