一个在AWS API Gateway或Lambda中在哪里配置映射模板? [英] Where does one configure mapping templates in AWS API Gateway or Lambda?

查看:433
本文介绍了一个在AWS API Gateway或Lambda中在哪里配置映射模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多人都在谈论使用json对象形式的映射模板使Lambda函数可以使用用户代理和ip地址吗?

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?

在许多控制面板中,这些json对象在哪里配置?

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

推荐答案

Api网关->您的api->您的端点/资源方法->集成请求->主体映射模板

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

使用有效的Content-type标头(例如application/json

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

您还可以使用以下方式将lambda映射回您的api响应

You can also map lambda back to your api response with

Api网关->您的api->您的端点/资源方法->集成响应-> http状态代码->主体映射模板

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

-编辑评论

请注意底部标题为人体贴图模板"的可扩展部分

Note the expandable section at the bottom entitled Body mapping templates

-编辑#2以解释如何在lambda函数中获取映射的值

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

这篇关于一个在AWS API Gateway或Lambda中在哪里配置映射模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆