Lambda timeout on sqs sendmessage
Asked Answered
F

2

10

I'm sending messages to a queue from a lambda function. But some times sqs.sendMessage dont return anything and the lambda get timeout. This happens sometimes

I tried changing de code many times, to work with await, promises and callback but the error persist.

Lambda log

const sqs = new aws.SQS({apiVersion: '2012-11-05'});

//TODO: Validar campos obrigatórios nas mensagens de acordo com o tipo de mensagem
exports.sendMessage =  async (message) => {
  let params = {
    MessageBody: JSON.stringify(message),
    QueueUrl: 'https://sqs.us-east-1.amazonaws.com/....',
  };

  try {
    await sqs.sendMessage(params).promise();
    return {statusCode: 200, body: {data: "Notification sent successfully"}};
  } catch (e) {
    return {statusCode: 400, body: {data: e}};
  }
}
Fischer answered 9/8, 2019 at 12:12 Comment(9)
What is the memory size configured in your Lambda function? Code looks good.Zhdanov
Hello, 128mb. In CloudWatch logs the most expensive use is 80mb.Fischer
The problem may be it. The more memory you configure, the better the configuration for the machine your code runs on is. For example, at 1792MB you get a dedicated core. Increase it to 1024MB and test it.Zhdanov
I tried to increase it, but the problem persist. It looks like a problem with the aws sdkFischer
I doubt it. I have been using it for years and never ran into issues. It’s definitely something to do with your codeZhdanov
A changed my code and set httpOptions: {connectTimeout: 5000} on create the instance of sqs. Now my lambda dont get timeout, but it get 5s to finish. Second aws docs this params is:Sets the socket to timeout after failing to establish a connection with the server. So the problem is the connetion with the server.Fischer
I have seen something that seems very similar but with the python3.6 lambda runtime. In addition to sqs, it also happens with the Python Requests module. So far, I have come to the same concussion; that it's a problem with socket connection to the server. I have been unable to reproduce this behavior with requests to the same server when running the client code outside of lambda.Ankle
@Ankle I'm curious if your lambda was VPC attached or not?Dehiscent
@Dehiscent I believe it was but I don't remember this situation 100%. I have implemented request retries so the problem hasn't been bothering me anymore.Ankle
I
3

I just had the same issue. The problem was that the lambda had 3 subnets assigned to it and only 1 of them had a routing entry to 0.0.0.0/0 Even tho 2/3 of the subnets were missing that routing entry, only 1-3% of the requests randomly timed out, so make sure to check that!

Intercommunicate answered 4/3, 2022 at 11:19 Comment(0)
M
2

I had a similar issues. What I did to make it work was add in the endpoint url into the boto3 client call.

For Example: boto3.client('sqs',endpoint_url='https://YourVPCDNSEndpointforSQS')

Mulciber answered 2/7, 2021 at 17:24 Comment(1)
Try formatting your answer to be a little more readable. CheersLyell

© 2022 - 2024 — McMap. All rights reserved.