首页 > 解决方案 > AWS APIGW - 访问 Lambda 映射模板中集成请求的静态 HTTP 标头

问题描述

我正在尝试修改类似于在此处找到的 API GW 配置https://oozio.medium.com/serverless-discord-bot-55f95f26f743

我需要做的是将自定义静态 HTTP 标头传递给 lambda。我已经定义了APIGW -> /{resource} -> Integration Request -> HTTP Headers:一个标题{"Name": "application", "Mapped from": "'discord'", "Caching": false}(如文档中所述的静态值的单个刻度)

此标头永远不会出现在映射模板的标头列表中。我花了一天的大部分时间试图解决这个问题。

简化问题:

您如何访问映射模板中集成请求中定义的静态 HTTP 标头?

TIA

标签: aws-lambdarequest-headersamazon-api-gateway

解决方案


在与 AWS 支持合作后,这显然是不可能的。

Many Thanks for your kind patience. After our call I started to look for a possible
solution to fetch the statically set HTTP header in the mapping template, but
unfortunately could not find any solution. 

I then discussed this scenario with my colleagues and also reached out to the
back-end API Gateway Service Team. It seems that this is something that is not
possible currently. 
We cannot access the static header value defined in the integration request, in
the request body mapping template. If your use case needs to send the static header
in body for non proxy integration then it is recommended that you may define it
statically in the body mapping template directly.

I understand as you explained to me how you need to access the header in the mapping
template but unfortunately this does not seem to have a solution. Although the
statically set header is seen to be passed in the API Gateway logs, there is no
mechanism in the lambda service to fetch these headers. 

Currently, I can ask the Service Team to consider the same as a feature request but
I wont be having a ETA available and also would need a solid reasoning since the
same can be done in the mapping template. 

此时(2021 年 3 月)的解决方法必须是直接在映射模板本身中表达它,或者使用 Proxy for Lambda。

提供以下内容作为功能请求的理由。

When creating APIGW via a templating process (Such as terraform), being able to
express 'API Wide' variables is a useful tool for sending contextual information
to a backend integration. For example, a Lambda that can process requests from
multiple sources, but it needs to know which source should be used. While the
value can be statically expressed in the mapping template, having it be a
variable as part of the API allows for better visibility and management as its
an interact-able component, where as the mapping template is effectively a
complex string. This also does not need to be a header, it could simply be
"integration variables" that allow you to define simple k:v pairs that are either
passed as part of context, or accessible in $context within the mapping template.
While statically expressing it in the mapping template is a workaround, it is
only that.

推荐阅读