简体   繁体   English

AWS Lambda - 在 lambda 函数中获取 API 网关路径

[英]AWS Lambda - Get API Gateway path in the lambda function

I have a Lambda function that is tied to API Gateway and i am trying to fetch the path and stage from either event or context object that are passed to the Lambda function.我有一个与 API 网关相关的 Lambda 函数,我试图从传递给 Lambda 函数的事件或上下文对象中获取路径和阶段。

The mapping template generated by the AWS console is below: AWS 控制台生成的映射模板如下:

##  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" : {
"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",
    "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"
    }
}

I am trying to get the stage from the context object like this:我试图从这样的上下文对象中获取舞台:

exports.handler = function(event, context) {
 console.log("Stage: " + context.stage);
 ...
}

but the logcat shows as Stage : undefined .但 logcat 显示为Stage : undefined

I have other query parameters that i am able to extract from params of event object like this我有其他查询参数,我可以从这样的事件对象的参数中提取

var id = event.params.querystring.id;
var publisher_id = event.params.querystring.publisher_id;

How can i extract the path and stage values from context with the above mapping template?如何使用上述映射模板从上下文中提取路径和阶段值?

The stage value is available in event.context.stage . stage值在event.context.stage可用。 So change your code to this:所以把你的代码改成这样:

exports.handler = function(event, context) {
 console.log("Stage: " + event.context.stage);
 ...
}

We are using a basic mapping template listed here:我们正在使用此处列出的基本映射模板:

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

I added this line "stage":"$context:stage", in the mapping template and used event[stage] to retrieve the value.我在映射模板中添加了这一行"stage":"$context:stage",并使用event[stage]来检索值。 This worked.这奏效了。 Don't forget to re-deploy the api.不要忘记重新部署api。

Changed Mapping Template:更改映射模板:

##  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
},
"stage" : "$context.stage", // added this line
"context" : {
"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",
    "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"
    }
}

and code:和代码:

exports.handler = function(event, context) {
 console.log("Stage: " + event[stage]);
 ...
}

You must check "Lambda Proxy Integration" in your method integration on API gateway, to receive the stage information.您必须在 API 网关上的方法集成中选中“Lambda 代理集成”,以接收阶段信息。

在此处输入图片说明

To use a stage variable to customize the HTTP integration endpoint, you must first configure a stage variable of a specified name, eg, url, and then assign it a value, eg, example.com.要使用阶段变量自定义 HTTP 集成端点,您必须首先配置指定名称的阶段变量,例如 url,然后为其分配一个值,例如 example.com。 Next, from your method configuration, set up an HTTP proxy integration, and instead of entering the endpoint's URL, you can tell API Gateway to use the stage variable value, http://${stageVariables.url}.接下来,从您的方法配置中,设置 HTTP 代理集成,而不是输入端点的 URL,您可以告诉 API Gateway 使用阶段变量值 http://${stageVariables.url}。 This value tells API Gateway to substitute your stage variable ${} at runtime, depending on which stage your API is running.该值告诉 API Gateway 在运行时替换您的阶段变量 ${},具体取决于您的 API 正在运行的阶段。 You can reference stage variables in a similar way to specify a Lambda function name, an AWS Service Proxy path, or an AWS role ARN in the credentials field.您可以以类似的方式引用阶段变量,以在凭证字段中指定 Lambda 函数名称、AWS 服务代理路径或 AWS 角色 ARN。

Reference: https://docs.aws.amazon.com/apigateway/latest/developerguide/stage-variables.html参考: https : //docs.aws.amazon.com/apigateway/latest/developerguide/stage-variables.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM