gpt4 book ai didi

amazon-web-services - 即使在显式定义之后,CloudFormation 也会部署隐式 AWS::APIGateway::Deployment

转载 作者:行者123 更新时间:2023-12-03 07:16:27 25 4
gpt4 key购买 nike

尝试使用 CloudFormation/SAM 部署简单的 HelloWorld API。
我必须定义 AWS::Serverless::ApiAWS::ApiGateway::ResourceAWS::ApiGateway::MethodAWS::ApiGateway::Deployment。但在 sam 部署期间,我看到隐式创建了另一个 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,您必须使用自动生成的 AWS::ApiGateway::Deployment,而不是您自己的。您可以通过将 .Deployment 附加到名称来完成此操作:

!Ref <api‑LogicalId>.Deployment

或者,根本不要使用 AWS::Serverless::Api,因为您看起来无论如何都是从头开始创建一切。

关于amazon-web-services - 即使在显式定义之后,CloudFormation 也会部署隐式 AWS::APIGateway::Deployment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71121441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com