我使用SAM规范配置了与API网关的lambda代理集成,并通过传递X-Amz-Invocation-Type异步调用lambda:“'Event'”标题
ApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: "2.0"
info:
title:
Ref: AWS::StackName
description: API endpoint for invoking LambdaFunction
version: 1.0.0
paths:
/items:
post:
consumes: ["application/json"]
produces: ["application/json"]
responses: {}
x-amazon-apigateway-integration:
type: "aws_proxy"
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
passthroughBehavior: "when_no_match"
httpMethod: "POST"
requestParameters:
integration.request.header.X-Amz-Invocation-Type: "'Event'"
问题是lambda返回空响应(调用异步),因此API Gateway会抛出以下错误
Wed Nov 14 08:03:14 UTC 2018:由于配置错误导致执行失败:格式错误的Lambda代理响应Wed Nov 14 08:03:14 UTC 2018:方法已完成,状态:502
这种行为有望吗?我是否必须明确定义回复?我不想总是扔200,因为我也想发送错误的请求和未经授权的错误。什么是避免这个问题的解决方案?
SAM模板正在使用Lambda代理集成部署API,因此无法异步调用代理集成的Lambda,因为响应将为空,而不是在有效的return format for Lambda proxy integration中。
因此,要从API网关异步调用Lambda,请使用aws(自定义或非代理)类型集成,设置X-Amz-Invocation-Type:'Event'标头。
您还可以authorize access to your API Gateway返回未经授权的访问错误。