Is it possible to configure different API Gateway stages with different lambda versions using AWS SAM

…衆ロ難τιáo~ 提交于 2021-01-27 05:32:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!