Where does one configure mapping templates in AWS

2019-09-16 02:04发布

问题:

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?

回答1:

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

-- 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"])
}


回答2:

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.