这种情况通常发生在API网关代理集成的Lambda函数中。
为了访问嵌入式事件负载中的变量值,Lambda函数需要在事件变量中包含映射定义。
以下是一个Lambda函数示例,它可以处理来自API网关的事件负载,并从该负载中获取嵌入变量的值:
import json
def lambda_handler(event, context):
# Extracting the variable from the event
my_var = event['requestContext']['elb']['targetGroupArn']
# Processing the variable
result = my_var.split("/")[-1]
# Creating the response
response = {
"statusCode": 200,
"body": json.dumps({
"message": f"The extracted variable is: {result}"
})
}
return response
要在API网关中正确代理此函数,需在集成响应中包含以下定义:
{
"mappings": {
"requestContext.elb.targetGroupArn": "$context.elb.targetGroupArn",
}
}
这显示了响应映射,将事件变量requestContext.elb.targetGroupArn映射到API网关代理中的$ context.elb.targetGroupArn。
也就是说,API网关代理应该像这样配置才能使此功能起作用: