Vaulstein
Vaulstein

Reputation: 22041

Access Stage variables - AWS API Gateway

I have Stage variables defined under AWS API GATEWAY.

I want to access the values, that I have defined for these, in the request handler written in Scala. According to the AWS API gateway console, Stage variables can be accessed using the $context object.

Stage variables through context

The documentation of the context object is present here but it does not define how to use the stage variables in the handleRequest method.

override def handleRequest(input: java.util.Map[java.lang.String, Object], context: Context): util.Map[String, _] = {
    context.getLogger.log("Input: " + input + " \n")
    // How do I access the Stage variable here?
}

Upvotes: 1

Views: 2516

Answers (2)

Vaulstein
Vaulstein

Reputation: 22041

Vijayanath's answer is a simple way to achieve what is required.

If you want to do it in a programmatically way and without the need to change the stageVariables from the console, then I would suggest using Swagger API integration to handle the stage variables.

You can achieve this using the json templates as defined here - AWS labs Swagger Api and by adding the below lines under x-amazon-apigateway-integration

"requestTemplates": {
    "application/json": "#set($inputRoot = $input.path('$')) \n{ \n \"version\" : \"$stageVariables.version\" \n}"
    }

So the entire json file would be:

"x-amazon-apigateway-auth" : {

    "type" : "aws_iam"
},
"x-amazon-apigateway-integration" : {
   "type" : "aws",
   "uri" : "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:MY_ACCT_ID:function:helloWorld/invocations",
   "httpMethod" : "POST",
   "credentials" : "arn:aws:iam::MY_ACCT_ID:role/lambda_exec_role",
   "requestTemplates": {
"application/json": "#set($inputRoot = $input.path('$')) \n{ \n \"version\" : \"$stageVariables.version\" \n}"
},
   "requestParameters" : {
       "integration.request.path.integrationPathParam" :     "method.request.querystring.latitude",
       "integration.request.querystring.integrationQueryParam" :     "method.request.querystring.longitude"
   },
   "cacheNamespace" : "cache-namespace",
   "cacheKeyParameters" : [],
   "responses" : {
       "2\\d{2}" : {
           "statusCode" : "200",
           "responseParameters" : {
               "method.response.header.test-method-response-header" :     "integration.response.header.integrationResponseHeaderParam1"
           },
           "responseTemplates" : {
               "application/json" : "json 200 response template",
               "application/xml" : "xml 200 response template"
           }
       },
       "default" : {
           "statusCode" : "400",
           "responseParameters" : {
               "method.response.header.test-method-response-header" : "'static value'"
           },
           "responseTemplates" : {
               "application/json" : "json 400 response template",
               "application/xml" : "xml 400 response template"
           }
       }
   }
}

Upvotes: 0

Vijayanath Viswanathan
Vijayanath Viswanathan

Reputation: 8541

You can use body mapping template in the integration request section and create a JSON like the sample below to get stage variable.

#set($inputRoot = $input.path('$'))
{
"version" : "$stageVariables.version"
}

Source :- http://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html#call-api-lambda-backend-with-stage-variable

If you are sure about body mapping template please have a look on https://aws.amazon.com/blogs/compute/tag/mapping-templates/

Upvotes: 3

Related Questions