5

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
  • Please add your template code to your question. Commented Jul 29, 2020 at 19:13

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.