Get the names of different stages in an AWS Lambda function?

0 votes

I have a lambda function(without APIGateWay) which I have created using BONES framework. I want same lambda function in both test and prod stage and for that I want to get the stage name during runtime. My code is written in java and I know that we can find out the default AWS region using following:

Regions regions = Regions.fromName(System.getenv("AWS_DEFAULT_REGION"));

So here are my questions:

1- How to get the name of the stage in which my lambda function is running?

2- What does this default region means? will it be always same as the region in which my lambda function is running?

Any lead is highly appreciated.

Sep 27, 2018 in AWS by bug_seeker
• 15,510 points
1,003 views

1 answer to this question.

0 votes

How to get the name of the stage in which my lambda function is running?

For Lambda Proxy integration, you can get the stage name from the requestContext in the input stream, which contains the API request serialized as a JSON string by API Gateway. The input data can also include the request's stage variables stageVariables (if you use/need any). For example:

JSONParser parser = new JSONParser();

public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  ...
  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  String stage = null;

  JSONObject event = (JSONObject)parser.parse(reader);
  if (event.get("requestContext") != null) {
    JSONObject requestContext = (JSONObject)parser.parse((String)event.get("requestContext"));
    if (requestContext.get("stage") != null) {
      stage = (String)requestContext.get("stage");
    }
  }
  ...
}

See full example.

What does this default region means? will it be always same as the region in which my lambda function is running?

YES according to docs and NO according to the SDK.

The SDK does not reference it. It uses AWS_REGION only. See SDKGlobalConfiguration.java.

answered Sep 27, 2018 by Priyaj
• 57,700 points

Related Questions In AWS

0 votes
1 answer

how to get the list of aws services i am used in aws my account by using the lambda function

Hi@shalk, You can create a session in your ...READ MORE

answered Sep 24 in AWS by MD
• 80,590 points
56 views
0 votes
1 answer

How to get the contents of a folder in an AWS CodeCommit repository?

Hi@akhtar, The following get-folder example demonstrates how to get the ...READ MORE

answered Nov 25 in AWS by MD
• 80,590 points
17 views
0 votes
1 answer

How to setup an alarm when Lambda function fails in AWS?

Hi@akhtar, To create an Alarm for Lambda function, ...READ MORE

answered Apr 2 in AWS by MD
• 80,590 points
378 views
0 votes
1 answer
0 votes
1 answer

how to access AWS S3 from Lambda in VPC

With boto3, the S3 urls are virtual by default, ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 57,700 points
5,484 views
0 votes
1 answer
0 votes
1 answer
+5 votes
2 answers

What are the different job roles that one can get for AWS

If you have the AWS certification for ...READ MORE

answered Sep 21, 2018 in AWS by Priyaj
• 57,700 points
165 views