1

I see many people talk about using mapping templates in the form of json object to make user agents and ip addresses available to Lambda functions?

Where are these json objects configured in the many control panels?

ox.
  • 3,579
  • 1
  • 21
  • 21

2 Answers2

2

Api gateway -> your api -> your endpoint/resource method -> integration request -> body mapping templates

Create one with a valid Content-type header such as application/json

You can then pick a template or roll your own map.

For example the template which maps everything (available in the dropdown) is

##  See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod",
    "stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath"
    }
}

You can also map lambda back to your api response with

Api gateway -> your api -> your endpoint/resource method -> integration response -> http status code -> body mapping templates

-- edit for comments

Note the expandable section at the bottom entitled Body mapping templates

Showing where body mapping section is

-- edit #2 to explain how to get the mapped values in your lambda function

exports.handler = (event, context, callback) => {
  console.log(event.context["user-agent"])
  console.log(event.context["source-ip"])
}
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
  • thank you, once I'm back at my box I'll try it out... ...more shortly.... – ox. Jul 23 '17 at 16:53
  • when I get to Api gateway -> your api -> your endpoint/resource method the only method I see is "ANY" and when I click on that method's integration request I see nothing that says body mapping templates. I see the HTTP option from the second path you suggest, but I am using a lambda function. – ox. Jul 23 '17 at 18:51
  • 1
    I've added a screenshot showing body mapping templates on an integration request for an any method – Ben Swinburne Jul 23 '17 at 22:22
  • 1
    yay! I unchecked "Use Lambda Proxy integration" and now "Body Mapping Template" shows up. Still cannot access ip and agent in lambda function yet, but working on it. More shortly... – ox. Jul 24 '17 at 01:46
  • 1
    Have explained how to grab those in your lambda function now too – Ben Swinburne Jul 24 '17 at 10:03
  • I got what I needed, and discovered that automatic/default api gateway endpoint creation from lambda passes through everything, you just need to know where to look. And now with this info I can go beyond that too. thank you for all your help, – ox. Jul 26 '17 at 12:23
1

Go to APIs>YourApi >Resources>/request >POST >Integration Request

Scroll to the end of the page and click on Body Mapping Templates

Select a content-type, post which a template window will open below your selection. This is where you configure the mapping.

user3165739
  • 166
  • 2
  • 3