How to get the stage name in AWS Lambda Gateway API related function

I have the following Lambda function configured in AWS Lambda:

var AWS = require('aws-sdk'); var DOC = require('dynamodb-doc'); var dynamo = new DOC.DynamoDB(); exports.handler = function(event, context) { var item = { id: 123, foo: "bar"}; var cb = function(err, data) { if(err) { console.log(err); context.fail('unable to update hit at this time' + err); } else { console.log(data); context.done(null, data); } }; // This doesn't work. How do I get current stage ? tableName = 'my_dynamo_table_' + stage; dynamo.putItem({TableName:tableName, Item:item}, cb); }; 

Everything works as expected (I insert an element in DynamoDB every time I call it).

I would like the dynamo table name to depend on the stage in which lambda is deployed.

My table will be:

  • my_dynamo_table_staging for the staging phase
  • my_dynamo_table_prod for the prod stage

However, how do I get the name of the current stage inside lambda?

Edit : my Lambda is being called by HTTP through an endpoint defined using the Gateway API

+8
source share
4 answers

If you checked "integration with Lambda Proxy" in the method integration request on the API gateway, you should get the stage from the API gateway, as well as any stageVariable that you configured.

Here is an example of an event object from a Lambda function called by an API gateway configured with "Lambda Proxy Integration":

 { "resource": "/resourceName", "path": "/resourceName", "httpMethod": "POST", "headers": { "header1": "value1", "header2": "value2" }, "queryStringParameters": null, "pathParameters": null, "stageVariables": null, "requestContext": { "accountId": "123", "resourceId": "abc", "stage": "dev", "requestId": "456", "identity": { "cognitoIdentityPoolId": null, "accountId": null, "cognitoIdentityId": null, "caller": null, "apiKey": null, "sourceIp": "1.1.1.1", "accessKey": null, "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": null, "userAgent": "agent", "user": null }, "resourcePath": "/resourceName", "httpMethod": "POST", "apiId": "abc123" }, "body": "body here", "isBase64Encoded": false } 
+9
source

I managed to do it after a long mess. Here's a walkthrough:

I assume you have the Gateway and Lambda APIs. If not, here is a good guide . You need parts-1 and part-2. You can skip the end of Part 2 by clicking the recently entered "Enable CORS" button in the API Gateway

Go to the API.

Press here:

enter image description here

Press here:

enter image description here

Then expand Body Mapping Templates , enter application/json as the content type, click the Add button, then select a mapping template, click Edit

enter image description here

And paste the following content into the Mapping Pattern:

 { "body" : $input.json('$'), "headers": { #foreach($param in $input.params().header.keySet()) "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end #end }, "stage" : "$context.stage" } 

Then click the "Deploy API" button (this is important for the changes in the API to take effect)

You can check by changing the Lambda function to this:

 var AWS = require('aws-sdk'); var DOC = require('dynamodb-doc'); var dynamo = new DOC.DynamoDB(); exports.handler = function(event, context) { var currentStage = event['stage']; if (true || !currentStage) { // Used for debugging context.fail('Cannot find currentStage.' + ' stage is:'+currentStage); return; } // ... } 

Then call your endpoint. You should have an HTTP 200 response with the following response body:

 {"errorMessage":"Cannot find currentStage. stage is:development"} 

Important Note:
If you have a Body Mapping Template that is too simple, for example: {"stage" : "$context.stage"} , this will override the parameters in the request. Therefore, the body and headers keys are present in the Body Mapping Template . If this is not the case, your Lambda does not have access to it.

+6
source

For those using the serverless framework , it is already implemented, and they can access event.stage without any additional configurations.

See this issue for more information.

+1
source

You can get it from an event variable. I registered my event object and got this.

 { ... "resource": "/test" "stageVariables": { "Alias": "beta" } } 
0
source

All Articles