2

I really don't understand why no one seems to have asked this question before, but is there a TIMEOUT environment variable which references the set timeout in the Lambda function in AWS?

It doesn't seem to be on the list of environment variables available, and that doesn't seem to make sense either: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html

2 Answers 2

5

I'm not sure of your programming environment but ever Lambda environment I've seen includes a Context object. That has the ability to check how much time is left in a Lambda run. In Java, for example:

public class ShowTimeout implements RequestStreamHandler {

    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LambdaLogger logger = context.getLogger();

        logger.log("there are " + context.getRemainingTimeInMillis() + "ms left to run");
    }
}

logs how much time is left. By default this is 3 seconds but, of course, can change depending on how you configure the Lambda.

EDIT

This is not the total time set. But as the very first thing the Lambda does it's pretty close. For a 15 second timeout Lambda I got:

there are 14998ms left to run

and

there are 14999ms left to run

If you've started your Lambda and it looks like the number is too small then you can do something about it. If it's the first thing you do like my simple code then you'll be very close. I'd argue that a simple rounding would be accurate enough.

Sign up to request clarification or add additional context in comments.

2 Comments

That's actually pretty helpful, but theoretically it will never show the exact time set on the UI dashboard right? Having the exact time could be useful for writing a check to remind the user if they haven't set the timeout manually
it works I guess, I still think it isn't ideal, but that is down to Amazon haha! Thanks for the advice, a round it will be!
0

If you are using SAM for your lambda, then you may set the TIMEOUT as a parameter and refer to that parameter in your environment variables as well as timeout.

Transform: AWS::Serverless-2016-10-31
Description: >
  This is a lambda function

Globals:
  Function:
    Timeout: !Ref TIMEOUT
    Environment:
      Variables:
        TIMEOUT: !Ref TIMEOUT

Parameters:
  TIMEOUT:
    Type: Number
    
Resources:
  myFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: functionName
      Handler: app.handler
      Runtime: nodejs18.x
      Architectures:
        - x86_64

Here, whatever value we will set as TIMEOUT will be the timeout of the lambda function and the same can be referenced in the code using process.env.TIMEOUT.
PS: For me, !Ref TIMEOUT didn't work when I added it within Resources -> Properties, but it worked when set as a global. This difference behaviour is not clearly documented though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.