Borislav Hadzhiev
Wed Sep 29 2021·3 min read
Photo by Jimmy Conover
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, you have to:
3 seconds
, the
maximum is 15 minutes
.128 Mb
which is
way too low and ads onto the function's execution time, which increases cost.1024 Mb
ends up saving me money most of the time, due to the reduced execution time of the function.This 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
VPC
and
trying to access the 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: