Kirk Broadhurst
Kirk Broadhurst

Reputation: 28738

Prepend StackName to Cloudformation resources

I want to have multiple stacks based on a single CloudFormation template, but I'm getting naming conflicts. The simplest way to resolve this would seem to be prepending (or appending) the StackName to each of the repeated resources, e.g. my lambda functions or roles.

AWS talks about AWS::StackName in the 'Template Reference' section of the documentation, but there's no clear demonstration of how to do this.

How can I prepend the StackName to a CloudFormation resource?

MyLambdaFunction
  Type: "AWS:Serverless::Function"
  Properties:
    FunctionName: AWS::StackName + "-myLambdaFunction"

Upvotes: 3

Views: 1185

Answers (3)

Dan Hook
Dan Hook

Reputation: 7088

The YAML version:

RoleName: !Sub ${AWS::StackName}-InstanceRole

Upvotes: 1

guest
guest

Reputation: 637

I prefer using Fn::Sub, because I believe that it's easier to read than the alternatives:

"RoleName": { "Fn::Sub" : "${AWS::StackName}-InstanceRole" },

Upvotes: 8

rdas
rdas

Reputation: 21305

You need to Ref the pseudoparameter and use the Fn::Join method to construct the name

MyLambdaFunction
  Type: "AWS:Serverless::Function"
  Properties:
    FunctionName: !Join [ "", [ {"Ref": "AWS::StackName"}, "-myLambdaFunction" ]]

Upvotes: 2

Related Questions