1

I have an aws lambda function that needs to trigger an aws sqs but I always get the following message:

Fail Send MessageAccessDenied: Access to the resource https://sqs.eu-west-1.amazonaws.com/ is denied

This is my lambda:

var QUEUE_URL = 'https://sqs.eu-west-1.amazonaws.com/*****/*****'
var AWS = require('aws-sdk');
var sqs = new AWS.SQS({region : 'eu-west-1'});


exports.handler = function(event, context) {

  var params = {
    MessageBody: JSON.stringify(event),
    QueueUrl: QUEUE_URL
  };
  sqs.sendMessage(params, function(err,data){
    if(err) {
      console.log('error:',"Fail Send Message" + err);
      context.done('error', "ERROR Put SQS");  // ERROR with message
    } else{
      console.log('data:',data.MessageId);
      context.done(null,'');  // SUCCESS
    }
  });
}

Anybody any idea what the problem could be or a good resource for the aws.sqs? Do I need to pass credentials, and how do I set my queue url in aws.sqs?

1 Answer 1

3

Do you have an execution role assigned to your Lambda function that allows it to send a message to the SQS queue? This article details how to do it, specifically the section, Setting up the IAM Role.

Sign up to request clarification or add additional context in comments.

5 Comments

I just found the solution, I had to add permissions to my queue
So you added a permission to your queue to allow your Lambda function to add messages to the queue? I guess that's one way to do it. Glad you found it!
You would typically add the SQS queue permissions to the IAM role of the Lambda function, rather than adding permissions on the SQS queue resource itself, allowing the Lambda role to access the queue.
@jarmod I agree and almost added that as a comment but although it appears to be a best practice, do you know why it is preferred over doing it in a resource policy?
@AshamanKingpin The distinction here is between identity-based policies and resource-based policies. Not all services support resource-based policies, first of all. The key use cases for resource-based policies, in my opinion, are a) cross-account access so that the other user does not have assume an IAM role in your account and hence lose their own IAM permissions in the process and b) when you want to assert a fundamental policy across all users of the resource, such as "require encryption" or "restrict access to the following IP addresses".

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.