Repository navigation
Description
Hey Guys,
I have the following SAM configuration, which works quite well so far:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: "Fancy Description"
Mappings:
Constants:
ServerlessService:
Version: 0.0.1
Globals:
# Sets global settings for ALL Lambda functions
Function:
Runtime: nodejs6.10
Timeout: 30
Handler: index.handle
Resources:
ServerlessService:
Type: AWS::Serverless::Api
Properties:
StageName: test
DefinitionBody:
'Fn::Transform':
Name: 'AWS::Include'
Parameters:
Location: s3://<ServerlessService-S3-Bucket>/swagger.yml
ServerlessServiceDefaultUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn: ServerlessServicetestStage
Properties:
ApiStages:
- ApiId: !Ref ServerlessService
Stage: test
Description: Default Serverless-Service Usage Plan
Quota:
Limit: 5000
Period: MONTH
Throttle:
BurstLimit: 40
RateLimit: 20
UsagePlanName: serverless-service-default-plan
ServerlessServiceFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../packages/lambda/functions/serverlessServiceFunction
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ServerlessService
Path: /path/with/{params}
Method: POSTPlease notice, that its only working, because I'm using the undocumented "feature" (or should I say hack?) as recommended here.
So have a closer look at this part:
ServerlessServiceDefaultUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn: ServerlessServicetestStage
Properties:
...Note, that the Resource ServerlessServicetestStage seems to be newly created by SAM (or what ever) and is never specified in the yaml file. Its very unintuitive to come up with this Resource name! Its really not nice, but as long as it works I don't complain.
But here is my issue: as I want to make the stage test at least a bit more flexible I tried to utilize the !Join intrinsic function.
ServerlessServiceDefaultUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn: !Join
- ''
- - 'ServerlessService'
- 'test'
- 'Stage'
Properties:
...Which fails with
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure
state Status: FAILED. Reason: Template format error: DependsOn must be a string or list of strings.
The ultimate goal is to achieve this:
ServerlessServiceDefaultUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn: !Join
- ''
- - 'ServerlessService'
- !Sub ${StageName}
- 'Stage'
Properties:
...It seems that !Join does not work on DependsOn.
I have the feeling that I'm failing to do a dirty workaround of an even dirtier workaround. What am I missing? What is the cleanest way to have a parameter like this:
Parameters:
StageName:
Description: API Stage Name
Type: String
Default: testAnd use !Sub ${StageName} where a Stage Name is needed?
I read #32 and #97. Both got closed without a proper solution of the issue that a UsagePlan is depending on an APIGateway. Help is very appreciated.
Activity
sanathkr commentedon Jan 19, 2018
Hey, so the stage resource name you are constructing is not a hack. That's how SAM constructs stage names and we won't change that behavior in future. We have documented this here. Changing a resource's name would make CloudFormation delete/re-create the resource. So SAM will never change the resource naming structure.
DependsOn does not work with intrinsic functions. This is a CloudFormation limitation that SAM cannot unfortunately circumvent. We are working on two things that will fix the problem for you:
!Ref ServerlessService.Stagewill resolve to!Ref ServerlessServicetestStageinternally. We introduced some flavor of this for Safe Lambda Deployments (ability to refer to Lambda Version created by SAM -!Ref MyFunction.Version)At the moment, I think you should continue using the DependsOn approach without the worry that this is a hack.
jakob-karnowski commentedon Aug 15, 2018
I also found out, that the logical ID of a stage is the concatenation
logicalIdOfRestApi+stageName+"Stage".However since today I experience, that the naming structure has changed to
logicalIdOfRestApi+"Stage".This is why the following works for me. My Api has the logical name
Apiand I use DependsOn withApiStage:deuscapturus commentedon Aug 22, 2019
Workaround
This seems to work.