Trigger AWS Lambda with AWS SQS using Python

AWS Image

All the developers who have used AWS SQS earlier must have experienced the hassle of polling to receive messages from queue. Whether you use short polling or long polling, it was always a tough thing to handle the process of message retrieval. And then, AWS announced that SQS is available as AWS Lambda event source.

But what exactly is the advantage of triggering a lambda? I want to explain this with below example-

“Imagine you have ordered a gift for your friend, and you want to make sure that he receives it. What will you do? You will obviously keep checking that whether gift reached or not, this is not a good thing to do right? What if you ask him to inform you as soon as he receives it? This way you are not bothering him continuously and you are also getting the confirmation.”

This is called Event driven architecture which means you don’t have to wait or poll for the messages, your predefined action will happen automatically as soon as you receive the message in queue.

Let’s Start!

Firstly you need to create a queue , I chose the standard one as I don’t care about duplicate messages. You may configure the other parameters also like visibility timeout etc.

Next step is to create a Lambda function, so quickly create a function using AWS Console.

After creating Lambda function, you need to add some code which will deal with the messages received from your SQS. Below is a sample code –

import json

def sqs_lambdahandle(event, context):
   body = {
      "message": "Deliver to SQS",
      "event": event
  print json.dumps(body)

  response = {
      "statusCode": 200,
      "body": json.dumps(body)

  return response


Now, go to your queue settings and configure your lambda function as trigger.

Let’s Test!

Now, In order to test the whole process, click on ‘Send and receive messages’ in top right corner, and give your message.

Now, go and check the Cloudwatch for your lambda, and you will be able to see the message ‘Deliver to SQS’ as we gave in our code sample.


I know it was more about AWS and less about Python, but I found it useful and hope you find it too.

Keep learning!


One thought on “Trigger AWS Lambda with AWS SQS using Python

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s