0

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.<Your Region>.amazonaws.com/<Your AWS Account ID>/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?

1 Answer 1

1

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>"
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help. The * in the Principal field means any application from your account can send messages to the SQS. You can fine tune this to suit your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.