Skip to content

Lambda Default Timeout

Level: Error

Initial version: 0.1.8

cfn-lint: ES1006

tflint: aws_lambda_function_default_timeout

You can define the timeout value, which restricts the maximum duration of a single invocation of your Lambda functions.

If your timeout value is too short, Lambda might terminate invocations prematurely. On the other side, setting the timeout much higher than the average execution may cause functions to execute for longer upon code malfunction, resulting in higher costs and possibly reaching concurrency limits depending on how such functions are invoked.

Implementations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myFunction = new Function(
      scope, 'MyFunction',
      {
        code: Code.fromAsset('src/hello/'),
        handler: 'main.handler',
        runtime: Runtime.PYTHON_3_8,
        // Change the function timeout
        timeout: 10,
      }
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        // Required properties
        "CodeUri": ".",
        "Runtime": "python3.8",
        "Handler": "main.handler",

        // Change the function timeout
        "Timeout": 10
      }
    }
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      # Required properties
      CodeUri: .
      Runtime: python3.8
      Handler: main.handler

      # Change the function timeout
      Timeout: 10
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
provider:
  name: aws
  # Change the timeout across all functions
  timeout: 10

functions:
  hello:
    handler: handler.hello
    # Change the timeout for one function
    timeout: 15
1
2
3
4
5
6
7
8
9
resource "aws_lambda_function" "this" {
  function_name = "my-function"
  runtime       = "python3.8"
  handler       = "main.handler"
  filename      = "function.zip"

  # Change the function timeout
  timeout = 10
}

See also