Lambda function can't push messages to sqs

-1

I have a lambda function with an exacution role with this policies:

  • AWSLambdaBasicExecutionRole ("logs:CreateLogStream", "logs:PutLogEvents")
  • a custom policy ({ "Effect": "Allow", "Action": [ "sqs:SendMessage", "sqs:ListQueues" ], "Resource": [ "arn:aws:sqs:myqueue" ] })
  • AWSCodeCommitReadOnly the my sqs queue has aan access policy:

{ "Sid": "AllowLambdaAccess", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::myLambdaServiceRole" }, "Action": "SQS:SendMessage", "Resource": "arn:aws:sqs:mySqsQueue" }

the lambda function and the queue are in the same region. No vpc configured for the lambda function. proplem is lambda times out and can't push messages to sqs

code for lambda is pretty standard:

const { SQSClient, SendMessageCommand } = require('@aws-sdk/client-sqs');
const sqsClient = new SQSClient({ region: 'us-east-1' });
const queueUrl = 'https://sqs.us-east-1.amazonaws.com/XXXXXXXXXXX/MySqsQueue';

const messageParams = {
                QueueUrl: queueUrl,
                MessageBody: JSON.stringify({MY OBJECT DEFINED HERE})
            };
            try {
                const command = new SendMessageCommand(messageParams);
                await sqsClient.send(command);
                console.log('Message sent to SQS queue successfully.');
            } catch (error) {
                console.error('Error sending message to SQS:', error);
            }

any idea what I am doing wrong?

2개 답변
1
수락된 답변

My apologies, the lambda function was actually in a VPC. I solved with a VPC endpoint.

답변함 일 년 전
profile picture
전문가
검토됨 한 달 전
0

Apologies if I'm taking your post too literally, but your ARN in your custom policy "arn:aws:sqs:myqueue" needs to be in format "arn:aws:sqs:us-east-1:444455556666:myqueue".

How do your logs look? Are you getting your "success" or "error" messages in there? If you have wrong permissions you should be catching an exception and logging it according to your code, not getting a timeout. So maybe your timeout is too small - the default 3 seconds can be a bit small for some AWS SDKs to get up and running.

전문가
답변함 일 년 전
  • thanks for helping me. arns are correct, in my example above I just simplified a bit. Logs in lambda just say "Task timed out after 5.04 seconds". I tried to increase the timeout to 1 min, same result. Anyway it shouldn't take long, as for test purposes I commented out everything and left only the sqs call bit. No logs on the sqs side. the sqs queue works as expected if I manually put a message from the console.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠