Reputation: 341
I need customize default Stage generated by AWS::Serverless::Api.
The stack creation is getting error "dev already exists".
My template code:
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
Outputs the error:
MyStage CREATE_FAILED dev already exists
The goal is to make reference to Stage from another resource in the same template file.
MyMapping:
Type: AWS::ApiGateway::BasePathMapping
Properties:
BasePath: my-path
RestApiId: !Ref MyApi
Stage: !Ref MyStage
Upvotes: 1
Views: 3273
Reputation: 341
I found the solution on this forum: https://github.com/awslabs/serverless-application-model/issues/192#issuecomment-520893111
The reference in the Stage property must be with !Ref MyApi.Stage
, and not with name by string.
Correct code:
MyMapping:
Type: AWS::ApiGateway::BasePathMapping
Properties:
BasePath: my-path
RestApiId: !Ref MyApi
Stage: !Ref MyApi.Stage
Upvotes: 2
Reputation: 1039
Error is occurring because you are trying to create the same resource twice. By specifying the stage name on the AWS::Serverless::Api resource [MyApi] you are creating that stage.
As per the documentation, stage does not have to be specified for API Gateway; however does for SAM. Try removing your stage resource [MyStage] and redeploying.
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
Upvotes: 0