Varun Gawande
Varun Gawande

Reputation: 1120

CloudFormation deploying implicit AWS::APIGateway::Deployment even after explicitly defining one

Trying to deploy a simple HelloWorld API using CloudFormation/SAM.
I have to defined an AWS::Serverless::Api, AWS::ApiGateway::Resource, AWS::ApiGateway::Method and a AWS::ApiGateway::Deployment. But during sam deploy I see that another AWS::ApiGateway::Deployment is implicitly created, one that is deployed before the API Method.

The API related resources deployed:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation                                       LogicalResourceId                               ResourceType                                    Replacement                                   
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Add                                           HelloWorldAPIDeployment5332c373d4               AWS::ApiGateway::Deployment                     N/A                                           
+ Add                                           HelloWorldAPIDeployment                         AWS::ApiGateway::Deployment                     N/A                                           
+ Add                                           HelloWorldAPIMethod                             AWS::ApiGateway::Method                         N/A                                           
+ Add                                           HelloWorldAPIResource                           AWS::ApiGateway::Resource                       N/A                                           
+ Add                                           HelloWorldAPIStage                              AWS::ApiGateway::Stage                          N/A                                           
+ Add                                           HelloWorldAPI                                   AWS::ApiGateway::RestApi                        N/A 

How can I stop the implicit AWS::ApiGateway::Deployment(HelloWorldAPIDeployment5332c373d4) from being created?
It causes the following error:

CREATE_FAILED                                   AWS::ApiGateway::Deployment                     HelloWorldAPIDeployment5332c373d4               Resource handler returned message: "The REST  
                                                                                                                                                API doesn't contain any methods (Service:     
                                                                                                                                                ApiGateway, Status Code: 400, Request ID:     
                                                                                                                                                02f3dcab-0126-4d16-8c19-27fb1e05c381,         
                                                                                                                                                Extended Request ID: null)" (RequestToken:    
                                                                                                                                                d75d527f-da4f-78a5-3000-be3e156ccc7a,         
                                                                                                                                                HandlerErrorCode: InvalidRequest)

Here's the relevant template code:

HelloWorldAPI:
    Type: AWS::Serverless::Api
    Properties: 
      Name: HelloWorldApi
      StageName: !Ref StageName

  HelloWorldAPIResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: !Ref HelloWorldAPI
      ParentId: !GetAtt HelloWorldAPI.RootResourceId
      PathPart: hello

  HelloWorldAPIMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizationType: NONE
      HttpMethod: GET
      ResourceId: !Ref HelloWorldAPIResource
      RestApiId: !Ref HelloWorldAPI
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations
          # - Arn: !GetAtt HelloWorldFunction.Arn

  HelloWorldAPIDeployment:
    Type: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref HelloWorldAPI
      StageName: !Ref StageName
    DependsOn:
      - HelloWorldAPIMethod

Upvotes: 2

Views: 1535

Answers (1)

Marcin
Marcin

Reputation: 238727

You can't switch that off. From docs:

When an AWS::Serverless::Api is specified, AWS Serverless Application Model (AWS SAM) always generates an AWS::ApiGateway::RestApi base AWS CloudFormation resource. In addition, it also always generates an AWS::ApiGateway::Stage and an AWS::ApiGateway::Deployment resource.

If you want to keep using AWS::Serverless::Api you have to use the auto-generated AWS::ApiGateway::Deployment, rather then your own. You do this by appending .Deployment to the name:

!Ref <api‑LogicalId>.Deployment

Alternatively, don't use AWS::Serverless::Api at all, as you seem you are creating everything from scratch anyway.

Upvotes: 0

Related Questions