DEV Community

Cover image for Trigger Lambda from SQS in Minutes: No-Fluff Guide
Megha Shivhare
Megha Shivhare

Posted on

2 3 3 3 2

Trigger Lambda from SQS in Minutes: No-Fluff Guide

A step-by-step guide on how to trigger lambda using SQS

In this blog, we'll walk through how to set up an AWS Lambda function that processes messages from an Amazon SQS (Simple Queue Service) queue. This is a common pattern in serverless applications, useful for decoupling and buffering tasks.


1. Create a Lambda Function

  • Go to AWS Lambda → Click on Create function
  • Choose the following settings:
    • Runtime: Python, Node.js, etc. (your choice)
    • Architecture: Arm64 (cheaper and efficient)
    • Permissions: Select “Create a new role from AWS policy templates” Attach the Amazon SQS poller permissions template to allow the Lambda function to access SQS.

Click Create function.


2. Add Your Lambda Code

  • Remove the default sample code.
  • Start by printing the event to understand its structure:
def lambda_handler(event, context):
    print(event)
Enter fullscreen mode Exit fullscreen mode

This helps you inspect the incoming SQS message structure. A typical event will include a list of Records, each containing a body field, which holds the actual message content.

Update your code to extract and process messages:

def lambda_handler(event, context):
    for record in event['Records']:
        message_body = record['body']
        print("Message Received:", message_body)
Enter fullscreen mode Exit fullscreen mode

Click Deploy to save changes.


3. Test the Lambda Function

  • Click on Test → Create a new test event.
  • Choose the SQS Event Template.
  • Modify the body field with your sample message.

Click Invoke and check the logs/output.


4. Create an SQS Queue

  • Go to Amazon SQS → Click Create Queue
  • Keep the default settings (you can fine-tune later if needed)
  • After creation, go back to your Lambda function.

5. Wire SQS to Lambda

  • Inside your Lambda function → Choose Add trigger
  • Select SQS → Choose the queue you just created
  • Save the trigger

Now your Lambda function is ready to process real messages sent to this queue.


6. Test the End-to-End Flow

  • Go to SQS → Choose your queue → Click on Send and receive messages
  • Send a test message

Your Lambda function should automatically trigger, process the message, and log the output.

To monitor:

  • Go to LambdaMonitor tab → View logs in CloudWatch

Conclusion

You’ve successfully integrated AWS SQS with Lambda! This setup is a foundational pattern for building decoupled, scalable, and resilient serverless applications. You can now extend it by adding error handling, DLQs (dead letter queues), batching, and more.

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

Top comments (2)

Collapse
 
nevodavid profile image
Nevo David

Neat guide! How can we optimize this setup for maximum efficiency?

Collapse
 
vedansh_dubey profile image
Vedansh Dubey

Very helpful