skip to Main Content

everyone.

I’m interested in get the content of SQS Message using Lambda. Let me explain my infrastructure: I have an EC2 instance that has a script like that bellow. So, it script will send to SQS the message containing instance ID.

#!/bin/bash
INSTANCE_ID=$(curl http://*.*.*.*/latest/meta-data/instance-id)
REGION=$(curl http://*.*.*.*/latest/meta-data/placement/availability-zone | sed '$s/.$//')
QUEUE-URL=$(...)

aws sqs send-message --queue-url "${QUEUE-URL}" --message-body "${INSTANCE_ID}" --region "${REGION}"

The ideia is: when the SQS recieve the message, I would like to trigger a Lambda Function to modificate this instance. But, for that, I need the instance ID. I have searching a lot and unfortunately, I couldn’t understand very well how I could get the instance ID, using AWS Lambda, from the SQS Message mentioned above.

I’ve been trying to solve this problem, but as I don’t understand Lambda so much, I searched for many solutions and tested then. Unfortunately, I had no success. So, I interested to learn more about this service.

If someone’s could help me with that, I’d be very greateful.

2

Answers


  1. The AWS Lambda function can be configured with the Amazon SQS queue as a ‘trigger’.

    When a message is sent to the SQS queue, the Lambda function will be invoked. The message both will be available in the Lambda function body.

    The code would look something like:

    def lambda_handler(event, context):
        for record in event['Records']:
          body = record['body']
          print("From SQS: " + body)
    

    It is possible that multiple messages are passed to the Lambda function, so it first loops through each Record, then extracts the passed-in information in the body parameter.

    The print() will show the contents of the message body in CloudWatch Logs. Check to make sure it contains what you expect. Then, add code that uses that value.

    There is no need for your Lambda function to specifically call SQS — this is handled automatically by the AWS Lambda service, which will then delete the message from the queue after your Lambda function successfully completes.

    Login or Signup to reply.
  2. @John’s answer Is all new you need.

    Since you are new to AWS. I would tell you answer in a way that will help you to debug for future too.

    • You need to first make sure sns topic target is configured as trigger.
    • In your lambda function, there is parameter irrespective of any language event. This parameter contains the information about the event which triggered the lambda
    • Inside the event parameter there is not all the information, like source, event information, in this information you will get Your instance id.
    • Just try logging event -> then records and you will get your instance id.
    • Then you can play with your Instance ID

    Tip : In the console there is lambda test event where you can generate a sample event form different aws services, sns is also there. You can visualise the sample event too before testing with real event form sns .

    Docs for reference – https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html#gettingstarted-concepts-event

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search