我看到很多人谈论使用 json 对象形式的映射模板来使用户代理和 IP 地址可用于 Lambda 函数?
这些 json 对象在众多控制面板中配置在哪里?
我看到很多人谈论使用 json 对象形式的映射模板来使用户代理和 IP 地址可用于 Lambda 函数?
这些 json 对象在众多控制面板中配置在哪里?
Api 网关 -> 你的 api -> 你的端点/资源方法 -> 集成请求 -> 正文映射模板
使用有效的 Content-type 标头创建一个,例如application/json
然后,您可以选择一个模板或滚动您自己的地图。
例如,映射所有内容的模板(在下拉列表中可用)是
## 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"
}
}
您还可以将 lambda 映射回您的 api 响应
Api 网关 -> 你的 api -> 你的端点/资源方法 -> 集成响应 -> http 状态码 -> 正文映射模板
-- 编辑评论
请注意底部标题为“身体映射模板”的可扩展部分
-- 编辑 #2 以解释如何在 lambda 函数中获取映射值
exports.handler = (event, context, callback) => {
console.log(event.context["user-agent"])
console.log(event.context["source-ip"])
}
转到 API>YourApi>Resources>/request>POST>Integration Request
滚动到页面末尾,然后单击Body Mapping Templates
选择一个内容类型,发布一个模板窗口将在您的选择下方打开。这是您配置映射的地方。