ktippetts
ktippetts

Reputation: 311

How to get the AWS Region in a Javascript/Nodejs Lambda Function:

How do I identify the region from within a Nodejs/Javascript AWS Lambda function?

The AWS_DEFAULT_REGION environment variable gives a ReferenceError (See here, which is for Java, not Node/Javascript.)

I realize I can get the "invokedFunctionArn" from the context object and parse it for the region, but it seems like there should be a more direct way.

Upvotes: 21

Views: 19818

Answers (3)

Andrew Clark
Andrew Clark

Reputation: 1123

Use the environment variable:

process.env.AWS_REGION

Source: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

Upvotes: 53

ktippetts
ktippetts

Reputation: 311

There just doesn't seem to be a more direct way to identify the region in a running lambda function, other than using context.invokedFunctionArn property.

Here's what I ended up doing:

exports.handler = (event,context, callback) => {
    var arnList = (context.invokedFunctionArn).split(":");
    var lambdaRegion = arnList[3] //region is the fourth element in a lambda function arn


    //more code
}

Upvotes: 0

Bela Vizy
Bela Vizy

Reputation: 1164

You can take a look at the context that is passed in. There maybe more goodies to discover there.

exports.handler = function(event, context) {
    console.log('Function name:', context.functionName);
    context.succeed();
};

I don't know anything that simply tells you the region. An ugly hack would be exec-ing something in the containing OS (linux) and hoping for the info to show up. uname -a perhaps.

I'm just curious. What do you need it for? Some kind of debugging info?

Upvotes: 1

Related Questions