Search code examples
amazon-web-servicesaws-cloudformationnaming

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"

Solution

  • 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" ]]