18

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.

Doug Domeny
  • 4,410
  • 2
  • 33
  • 49
ktippetts
  • 181
  • 1
  • 1
  • 3
  • Afaik the only way is to parse the arn – Vsevolod Goloviznin Oct 06 '16 at 17:06
  • While it is not impossible to hack the way out to get the info; in my opinion, it is would be easier to feed in the REGION details as part of configuration details or environment info [i.e. explicitly add it to the code / config] – Naveen Vijay Oct 06 '16 at 17:10

3 Answers3

47

Use the environment variable:

process.env.AWS_REGION

Source: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html#lambda-environment-variables

Andrew Clark
  • 1,003
  • 9
  • 8
1

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?

Bela Vizy
  • 1,133
  • 8
  • 19
  • The lambda will be deployed in different regions and consequently will have region-specific configuration values (SNS topics, for one). – ktippetts Oct 07 '16 at 17:18
  • Additionally (and most importantly), the config file (json) contains the config for all regions, with the key as the region. – ktippetts Oct 07 '16 at 20:06
0

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
}
ktippetts
  • 181
  • 1
  • 1
  • 3