Last updated: Feb 26, 2024
Reading timeยท3 min
The "Task timed out after X seconds" error occurs because a Lambda function's execution has exceeded its configured timeout.
To view a Lambda function's timeout:
Configuration
tab and select General configuration
.To solve the "Task timed out after X seconds" error in AWS Lambda:
3
seconds and the
maximum is 15
minutes.128 Mb
which is way
too low and adds to the function's execution time.1024 Mb
ends up saving me money due to the reduced execution time.The following example initializes the AWS SNS SDK outside of a Lambda function's handler.
The example is in Node.js, however, the technique applies to any programming language.
const AWS = require("aws-sdk"); // ๐๏ธ Initialize expensive code here // this only runs once let sns = new AWS.SNS({region: 'us-east-1'}); exports.handler = async event => { if (!sns) { // ๐๏ธ should not run, but just to be safe sns = new AWS.SNS({region: 'us-east-1'}); } // your code here ๐๏ธ }
Monitor
tab and then View logs in CloudWatch
.internet
.If your Lambda function is in a VPC and trying to access the internet, it might time out if you don't have the correct setup, namely:
I've also written a tutorial on how to add permissions to Lambda functions in AWS CDK.
You can learn more about the related topics by checking out the following tutorials: