0
0
AwsHow-ToBeginner · 4 min read

How to Set AWS Lambda Timeout: Simple Steps and Examples

You can set the AWS Lambda function timeout by specifying the Timeout property, which defines the maximum execution time in seconds. This can be done via the AWS Management Console, AWS CLI using --timeout, or Infrastructure as Code like AWS CloudFormation or Terraform.
📐

Syntax

The Lambda timeout is set using the Timeout property, which accepts an integer value representing seconds. The value must be between 1 and 900 seconds (15 minutes).

In AWS CLI, use the --timeout option when creating or updating a function.

In Infrastructure as Code, specify Timeout in the function resource definition.

bash
aws lambda update-function-configuration --function-name MyFunction --timeout 30
💻

Example

This example shows how to set the timeout to 10 seconds for a Lambda function using AWS CLI and AWS CloudFormation.

bash and yaml
aws lambda update-function-configuration --function-name MyFunction --timeout 10

---
Resources:
  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: MyFunction
      Handler: index.handler
      Runtime: nodejs18.x
      Timeout: 10
      Role: arn:aws:iam::123456789012:role/lambda-execution-role
Output
Updated function configuration for MyFunction
⚠️

Common Pitfalls

  • Setting a timeout too low causes your function to stop before completing its task.
  • Setting a timeout too high may cause unnecessary charges if the function hangs.
  • For long-running tasks, consider breaking work into smaller functions or using Step Functions.
  • Remember the maximum timeout is 900 seconds (15 minutes).
bash
Wrong:
aws lambda update-function-configuration --function-name MyFunction --timeout 0

Right:
aws lambda update-function-configuration --function-name MyFunction --timeout 30
📊

Quick Reference

MethodHow to Set TimeoutNotes
AWS ConsoleEdit function configuration > TimeoutSet in minutes and seconds, max 15 min
AWS CLI--timeout NN is seconds, 1 to 900
CloudFormationTimeout: N in secondsSet in function resource properties
Terraformtimeout argument in aws_lambda_functionSet in seconds

Key Takeaways

Set Lambda timeout using the Timeout property in seconds (1-900).
Use AWS Console, CLI, or Infrastructure as Code to configure timeout.
Avoid too low or too high timeout values to prevent errors or extra costs.
Maximum Lambda timeout is 15 minutes (900 seconds).
For long tasks, consider splitting or using AWS Step Functions.