我有一个用于我的应用程序的SAM模板。每次我使用新的API网关阶段名称部署SAM模板时,它都会替换先前创建的阶段。
因此,找到了这篇文章,它帮助我发布了指向不同lambda版本的不同版本。https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variobles-to-manage-lambda-functions/
但是,为此,我必须在部署后手动更改API网关。那么,有什么办法可以使用AWS SAM?
例如,考虑以下云形式模板提取:
ProxyMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: ANY
RestApiId: !Ref Api # AWS::ApiGateway::RestApi defined elsewhere
ResourceId: !Ref ProxyResource # AWS::ApiGateway::Resource defined elsewhere
AuthorizationType: NONE #auth is done at the public API layer
Integration:
# client request passed through as-is. "Lambda proxy integration"
Type: AWS_PROXY
Uri: !Join
- ''
- - 'arn:aws:apigateway:'
- !Sub ${AWS::Region}
- ':lambda:path/2015-03-31/functions/${!stageVariables.FunctionArn}/invocations'
IntegrationHttpMethod: ANY
PassthroughBehavior: WHEN_NO_MATCH
就像在示例中一样,这应该允许我使用在运行时确定的lambda函数来创建一种方法。但是,当我这样做时,部署模板时会出现以下错误:
ProxyMethod CREATE_FAILED Invalid lambda function (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; ....
如何通过CloudFormation定义 API网关方法的目标lambda函数由阶段变量确定?
可以通过包括大多数完整的lambda arn并将其简单添加名称来解决此处的问题。CloudFormation可能会通过正则表达式运行整个字符串,当它看到${!stageVariables.FunctionArn}
而不是arn:aws:lambda:blablabla
时,它会失败。