问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/57306067/is-it-possible-to-configure-different-api-gateway-stages-with-different-lambda-v