65

Regions.getCurrentRegion() returns null from within an AWS Lambda function. It seems that Regions.getCurrentRegion() is not supported from within an AWS Lambda function. Is there an alternative way to determine which region the lambda function is running in?

NOTE: AWS Lambda function is written in Java.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Richard Crane
  • 1,110
  • 3
  • 9
  • 17

8 Answers8

77

You can read the AWS_REGION environment variable and use the Regions.fromName function to parse that into a useable region.

Regions.fromName(System.getenv("AWS_REGION"))

The advantage of this over the ARN parsing approach is that you do not need a Context object which means you can use it outside of your handler function.

Source: AWS's Lambda environment variables docs.

meustrus
  • 6,637
  • 5
  • 42
  • 53
sihil
  • 2,563
  • 1
  • 17
  • 24
  • 1
    This is documented in https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html#lambda-environment-variables – pba Feb 25 '18 at 08:30
  • Thanks @pba - I've updated my answer to reflect that as a better source. – sihil Mar 01 '18 at 11:45
22

All Lambda containers has environment variables set $AWS_REGION

From Java Code in Lambda.You can access it as below

System.getenv("AWS_REGION")
Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
Gokul
  • 473
  • 1
  • 4
  • 8
15

For anyone looking to do this in Python:

import os
import json

def lambda_handler(event, context):
    my_region = os.environ['AWS_REGION']
    print(my_region)
    return {
        'statusCode': 200,
        'body': json.dumps(f'Hello from {my_region}!')
    }
James Shapiro
  • 4,805
  • 3
  • 31
  • 46
13

The context object that is passed to your Lambda function has an attribute called invokedFunctionArn. The ARN is of the format:

arn:aws:<service>:<region>:<account_id>:<resource>

So you could split this string on the : character and find the region associated with the Lambda function.

Note: In java you would call the getInvokedFunctionArn() getter of the context object.

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
garnaat
  • 44,310
  • 7
  • 123
  • 103
  • 1
    The `AWS_DEFAULT_REGION` environment variable is also available. This is available outside the handler and there is no need to parse it. See my answer for code. – sihil May 25 '16 at 16:29
  • 1
    Note that this environment variable is not present in all runtime environments. As you show, it does work for Java. – garnaat Oct 06 '16 at 20:03
  • Can you give some more details on the circumstances in which this is not set @garnaat? This shouldn't be specific to the JVM. – sihil Nov 17 '16 at 13:29
  • You're right. I just ran tests in Python and NodeJS and it is present in both. I'm not sure when this was added but I don't think it was there in earlier versions of Lambda. This is an easier and more reliable way of finding the region than parsing the ARN. – garnaat Nov 18 '16 at 13:14
  • But if someone wants to find account id then lambda does not provide any environment variable, this will be the only approach to use – Jatin Mehrotra Jan 25 '23 at 04:06
6

If anyone looking to get the region in Node JS. This will be work

process.env.AWS_REGION
5

1) You can use environment variable and access it as

System.getenv("AWS_REGION")

Following is a list of environment variables that are part of the AWS Lambda execution environment and made available to Lambda functions. The table below indicates which ones are reserved by AWS Lambda and can't be changed, as well as which ones you can set when creating your Lambda function. For more information on using environment variables with your Lambda function

https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html

2) You can read the AWS_DEFAULT_REGION environment variable

Regions.fromName(System.getenv("AWS_DEFAULT_REGION"))
vaquar khan
  • 10,864
  • 5
  • 72
  • 96
1

Although using the AWS_REGION environment variable will work in most cases, I've found that with a Lambda@Edge, this variable will resolve to the region from which the content was served (i.e. the closest region to the client). Using the invokedFunctionArn value from the context object will not work either for the same reason. Here is the context I received when invoking a Lambda in us-east-1 from a location closest to us-east-2:

{
  "callbackWaitsForEmptyEventLoop": true,
  "functionVersion": "6",
  "functionName": "us-east-1.<FUNCTION_NAME>",
  "memoryLimitInMB": "128",
  "logGroupName": "/aws/lambda/us-east-1.<FUNCTION NAME>",
  "logStreamName": "2020/09/04/[6]<LOG STREAM NAME",
  "invokedFunctionArn": "arn:aws:lambda:us-east-2:<ACCOUNT ID>:function:us-east-1.<FUNCTION NAME>:6",
  "awsRequestId": "0fa5f5c3-90ea-41d5-b3c3-1714ccdf1b17"
}

So, the solution that I've found works consistently between Lambda@Edge and other Lambdas is to retrieve the region from the functionName value from the context object. Here's how I am doing this with Node.js:

functionName.split('.')[0];
  • Do we need to use this `getFunction-Property` : https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#getFunction-property – Rahul Ahire Apr 06 '21 at 16:04
  • 1
    @RahulAhire - no, it's available on the [context](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html) passed into the lambda. The event signature for a lambda handler is `async function(event, context)` if running async, or `function(event, context, callback)` if running the callback way. [See here](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html) for more details on what's available when the lambda is invoked. – ps2goat Nov 16 '21 at 22:13
1

As its javadoc states Regions.getCurrentRegion() is relevant in the AWS EC2 context only. In other contexts it returns null as it does in the AWS Lambda context.

AWS Lambda by default defines the AWS_REGION environmental variable holding the region name. The value can be read by System.getenv("AWS_REGION").

Yuri
  • 4,254
  • 1
  • 29
  • 46