Home > Mobile >  AWS Lambda is not sending a messages to SQS queue
AWS Lambda is not sending a messages to SQS queue

Time:09-28

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: '[email protected]'
        }

        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?

CodePudding user response:

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>"
    }
  • Related