Amazon web services “阶段”;开发已经存在”;对于AWS无服务器API,使用Cloudformation

Amazon web services “阶段”;开发已经存在”;对于AWS无服务器API,使用Cloudformation,amazon-web-services,aws-lambda,amazon-cloudformation,aws-serverless,Amazon Web Services,Aws Lambda,Amazon Cloudformation,Aws Serverless,我需要通过AWS::Serverless::Api进行自定义 堆栈创建出现错误“开发人员已存在” 我的模板代码: AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Resources: MyApi: Type: AWS::Serverless::Api Properties: Name: my-service StageName: dev MyA

我需要通过AWS::Serverless::Api进行自定义

堆栈创建出现错误“开发人员已存在”

我的模板代码:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: my-service
      StageName: dev

  MyApiDeployment:
    Type: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref MyApi
      StageName: dev

  MyStage:
    Type: AWS::ApiGateway::Stage
    DependsOn: MyApiDeployment
    Properties:
      StageName: dev
      RestApiId: !Ref MyApi
      DeploymentId: !Ref MyApiDeployment

  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: MyAssembly::MyNamespace::MyHandler
      Runtime: dotnetcore2.1
      Events:
        ApiRoot:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /
            Method: ANY
输出错误:

MyStage                                  CREATE_FAILED                            dev already exists
目标是从同一模板文件中的另一个资源引用Stage

  MyMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      BasePath: my-path
      RestApiId: !Ref MyApi
      Stage: !Ref MyStage

发生错误是因为您尝试两次创建同一资源。通过在AWS::Serverless::Api资源[MyApi]上指定阶段名称,您正在创建该阶段

因为,不必为API网关指定阶段;但是对SAM来说是这样。请尝试删除您的舞台资源[MyStage]并重新部署

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: my-service
      StageName: dev

  MyStage:
    Type: AWS::ApiGateway::Stage
    DependsOn: MyApiDeployment
    Properties:
      StageName: dev
      RestApiId: !Ref MyApi
      DeploymentId: !Ref MyApiDeployment

我在这个论坛上找到了解决方案:

阶段属性中的引用必须带有
!引用MyApi.Stage
,以及not,并按字符串命名

正确代码:

  MyMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      BasePath: my-path
      RestApiId: !Ref MyApi
      Stage: !Ref MyApi.Stage

问题是我需要在同一模板中的另一个资源中引用此阶段。如何引用隐式创建的Stage?很简单,创建一个参数,并使api资源stagename和apigateway basemapping都引用该参数。在你的例子中,所有的!Ref stagename仍然引用逻辑名称。例如!Ref MyStage==devIt工作了,非常感谢!StageName和Stage都在dotnet lambda deploy serverless--template parameters命令行中作为参数接收外部字符串值。在某些情况下,错误仍然发生,似乎StageName和Stage不是同一类型。您是否实际执行了输出值以检查为什么/有什么不同,所有人都认为这应该是一样的,除非你用部署来改变你的阶段。