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,310 points
164 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
• 56,160 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

Pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

Python String and Integer concatenation >>> print("arn:aws:swf:us-east-2:{0}:action/actions/AWS_EC2.InstanceId.Stop/1.0".format(acccnum)) arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0 >>> print("arn:aws:swf:us-east-2:" ...READ MORE

answered Oct 5, 2018 in AWS by Priyaj
• 56,160 points
102 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
• 56,160 points
1,231 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
• 56,160 points
45 views