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"
I prefer using Fn::Sub, because I believe that it's easier to read than the alternatives:
"RoleName": { "Fn::Sub" : "${AWS::StackName}-InstanceRole" },
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" ]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With