7

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
MAK
  • 1,915
  • 4
  • 20
  • 44
  • At least regarding piont 1): No you may not know. There is no such thing as a parameter in YAML, that kind of interpretation is up to the program that loads the YAML. There is also no functionality in YAML to concatenate scalars. Whether you may know point 2) I don't know, but if you are interested in learning **how** to do that, you should ask that, and not whether it is possible/you are allowed to know it (which can only be **correctly** answered by yes or no). – Anthon Apr 04 '19 at 09:45
  • I removed the non-relevant stuff from your post, otherwise there would be good motivation to close it as a dupclicate of https://stackoverflow.com/questions/5484016/how-can-i-do-string-concatenation-or-string-replacement-in-yaml – Anthon Apr 04 '19 at 09:47

1 Answers1

13

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
kichik
  • 33,220
  • 7
  • 94
  • 114