简体   繁体   中英

Is there a TIMEOUT environment variable for Lambda functions in AWS?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM