CloudFormation 部署隐式 AWS::APIGateway::Deployment 即使在显式定义一个之后

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

尝试使用 CloudFormation/SAM 部署一个简单的 HelloWorld API。
我必须定义一个 AWS::Serverless::ApiAWS::ApiGateway::ResourceAWS::ApiGateway::Method 和一个 AWS::ApiGateway::Deployment。但是在 sam deploy 期间,我看到另一个 AWS::ApiGateway::Deployment 被隐式创建,一个部署在 之前 API 方法。

部署的API相关资源:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 

如何停止创建隐式 AWS::ApiGateway::Deployment(HelloWorldAPIDeployment5332c373d4)?
它会导致以下错误:

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)

相关模板代码如下:

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

您无法将其关闭。来自 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.

如果您想继续使用 AWS::Serverless::Api,您必须使用 auto-generated AWS::ApiGateway::Deployment,而不是您自己的。您可以通过将 .Deployment 附加到名称来执行此操作:

!Ref <api‑LogicalId>.Deployment

或者,根本不要使用 AWS::Serverless::Api,因为您似乎是从头开始创建所有内容。