问题描述
我有一个适用于我的应用程序的SAM模板.每次我使用新的API Gateway阶段名称部署SAM模板时,它都会替换先前创建的阶段.
因此,找到了这篇文章,可以帮助我发布指向不同lambda版本的不同版本. https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/
但是,为此,我必须在部署后手动更改API Gateway.那么,有什么方法可以使用AWS SAM做到这一点?
例如,考虑以下CloudFormation模板摘录:
ProxyMethod:类型:AWS :: ApiGateway :: Method特性:HttpMethod:任何RestApiId:!Ref Api#在其他地方定义的AWS :: ApiGateway :: RestApiResourceId:!Ref ProxyResource#AWS :: ApiGateway ::在其他地方定义的资源AuthorizationType:NONE #auth在公共API层完成一体化:#个按原样传递的客户请求."Lambda代理集成";类型:AWS_PROXYUri :!加入-''--'arn:aws:apigateway:'-!Sub $ {AWS :: Region}-':lambda:path/2015-03-31/functions/$ {!stageVariables.FunctionArn}/invocations'IntegrationHttpMethod:任意传递行为:WHEN_NO_MATCH
这应该允许我使用在运行时确定的Lambda函数创建方法,就像在示例.但是,这样做时,在部署模板时出现以下错误:
ProxyMethod CREATE_FAILED无效的lambda函数(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException; ....
如何通过CloudFormation定义 一种API网关方法,其目标Lambda函数由阶段变量确定?
可以通过包括大多数完整的lambda ARN并仅对名称进行标记来解决此问题.CloudFormation可能通过正则表达式运行整个字符串,并且当它看到 $ {!stageVariables.FunctionArn}
而不是 arn:aws:lambda:blablabla
时,它将失败.>
I have a SAM template for my application. Each time I deploy my SAM template with a new API Gateway stage name, it replaces the previously created stage.
So, found this article which helps me in releasing different versions pointing to different lambda versions.https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/
But, for this, I have to change API Gateway manually after deployment.So, Is there any way that I can do this using AWS SAM ?
For example, consider the following CloudFormation Template extract:
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
This should allow me to create a Method with the Lambda function determined at runtime, just like in the example. However, when I do that, I get the following error when deploying the template:
ProxyMethod CREATE_FAILED Invalid lambda function (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; ....
How can I define via CloudFormation an API Gateway method whose destination Lambda function is determined by a stage variable?
The problem here can be resolved by including most of the full lambda ARN, and tokenising just the name. CloudFormation probably runs the whole string through a regex, and when it sees ${!stageVariables.FunctionArn}
instead of arn:aws:lambda:blablabla
then it fails.
这篇关于是否可以使用AWS SAM使用不同的lambda版本配置不同的API Gateway阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!