Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 288
Kirk Broadhurst Avatar asked Dec 21 '25 21:12

Kirk Broadhurst


2 Answers

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

"RoleName": { "Fn::Sub" : "${AWS::StackName}-InstanceRole" },
like image 188
guest Avatar answered Dec 24 '25 11:12

guest


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" ]]
like image 42
rdas Avatar answered Dec 24 '25 09:12

rdas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!