Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AWS Lambda is not sending a messages to SQS queue

I am trying to send message (hardcoded) to SQS from Lambda. I am running lambda locally and getting messages in SQS. Deployed lambda (image) is not working, I have added AmazonSQSFullAcess to a lambda role.

const AWS = require('aws-sdk');
AWS.config.update({ region: process.env.REGION });
const sqs = new AWS.SQS({ apiVersion: '2012-11-05' });

let response;

/**
 *
 * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
 * @param {Object} event - API Gateway Lambda Proxy Input Format
 *
 * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
 * @param {Object} context
 *
 * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
 * @returns {Object} object - API Gateway Lambda Proxy Output Format
 *
 */
exports.lambdaHandler = async (event, context) => {
    try {
        hrValue = {
          projectId: '35', email: 'test@aaa.com'
        }

        try {
          const listString = JSON.stringify(hrValue)
          const params = {
            DelaySeconds: 1,
            MessageAttributes: {
              "email": {
                DataType: "String",
                StringValue: hrValue.email
              },
              "projectId": {
                DataType: "String",
                StringValue: hrValue.projectId
              },
            },
            MessageBody: listString,
            QueueUrl: "https://sqs.ca-central-1.amazonaws.com/694292245325/sqs-test-lambda"
          };
          console.log('## send params: ', JSON.stringify(params))
          let sendSqsMessage = sqs.sendMessage(params).promise();

          sendSqsMessage.then((data) => {
            console.log(`SQS | SUCCESS: ${data.MessageId}`);
          }).catch((err) => {
            console.log(`SQS | ERROR: ${err}`);
          });

          response = {
            'statusCode': 200,
            'body': JSON.stringify({
              message: 'send to sqs',
              // location: ret.data.trim()
            })
          }
        } catch (error) {
          console.log(error)
        }
    } catch (err) {
        console.log(err);
        return err;
    }

    return response
};

So SQS is empty when when deploy lambda from image, what else I need to make it work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add this to your SQS Access Policy

{
      "Sid": "TrustUsers",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "sqs:SendMessage",
      "Resource": "arn:aws:sqs:<Your SQS Region>:<Your AWS Account ID>:<Your SQS Name>"
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading