Search code examples
aws-lambdaserverless-framework

Can you still set the timeout for a lambda function in Serverless when it's in a container


In normal lambdas, it's straightforward to set the timeout within the serverless.yml

For eg

functions:
  my_function:
    handler: handler.my_function
    timeout: 60

However when I use a container image, this seems to get lost and AWS uses the default of 6s. This is the example code:

functions:
  my_function:
    image:
      name: appimage
      command:
        - handler.my_function
    timeout: 60

Am I doing something wrong? I can of course set it manually on the AWS console, but it would be nice to have it all in the yml


Solution

  • Why yes, yes you can. Inside your serverless.yml add the following: From the docs:

    provider:
      runtime: nodejsXX.x
      # Default timeout for functions (default: 6 seconds)
      # Note: API Gateway has a maximum timeout of 30 seconds
      timeout: 10
    

    6 seconds is the default timeout, and the max timeout for is 30 seconds, so just add your timeout under provider chunk and you should be all set! You were close!

    NOTE: Keep in mind that API Gateway is the service that actually times out. Your lambda can still be running, and compete successfully, as nascente-diskreta mentioned, for a long time after that. The response will appear to timed out in the request cycle. I ran into this recently where jobs would return a failed response. But the lambda successfully completed. It just could not tell the server it had completed using API Gateway.