Search code examples
aws-cloudformationamazon-cognitoaws-userpools

How to dynamically create Resource (UserPool) name by concatenating parameter value and string in AWS CloudFormation YAML template?


I am trying to create an AWS CloudFormation template using YAML. I add a UserPool resource as follows. The user pool name & id should be obtained via a parameter value i.e., if the value of parameter paramUserPoolName is 'Sample', then:

UserPoolName = Sample

UserPool Resource Name = SampleUserPool i.e., concatenated value of 'paramUserPoolName + UserPool'

Parameters:
  paramUserPoolName:
    Type: String
Resources:
  <I need 'paramUserPoolName + UserPool' here >:
    Type: 'AWS::Cognito::UserPool'
    Properties: {
        "UserPoolName": paramUserPoolName
    }

How can I dynamically create a resource id in CloudFormation template?

PS:

The following worked:

    Resources:
     SampleUserPool:
      Type: 'AWS::Cognito::UserPool'
      Properties:
       UserPoolName: !Sub ${paramUserPoolName}UserPool

Solution

  • Use !Sub for that. You can also use !Join, but !Sub is easier.

    Parameters:
      paramUserPoolName:
        Type: String
    Resources:
        Type: 'AWS::Cognito::UserPool'
        Properties:
            UserPoolName: !Sub ${paramUserPoolName}UserPool