Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two variables in Cloudformation Fn::Sub in UserData

I am able to add 1 variable but unable to add second, I am a sys admin, and not that much knowledgeable about YAML

UserData:  
    Fn::Base64: !Sub  
       - |+  
        #!/bin/bash -xe  
        NEW_HOSTNAME=${test}  
       - test:   
             Fn::FindInMap: [Regions, !Ref "AWS::Region", Name]   

I would like to add another FindInMap variable after test, but I am unable to.

like image 718
david bob Avatar asked Oct 30 '25 21:10

david bob


2 Answers

You can do it like this:

UserData:
  Fn::Base64: !Sub
    - |
      #!/bin/bash -xe
      foo=${foo}
      baz=${baz}
    - foo: !FindInMap [FooMap, Foo, Value]
      baz: !FindInMap [FooMap, Baz, Value]

It could also be formatted as:

UserData:
  Fn::Base64: !Sub
    - |
      #!/bin/bash -xe
      foo=${foo}
      baz=${baz}
    - {
        foo: !FindInMap [FooMap, Foo, Value],
        baz: !FindInMap [FooMap, Baz, Value]
      }

See also docs for the Fn::FindInMap function.

Note that I removed the |+ - which is a YAML feature and says to keep the trailing newlines. It isn't really required here.

like image 114
Alex Harvey Avatar answered Nov 02 '25 14:11

Alex Harvey


UserData: 
              Fn::Base64: !Sub
               - |
                  #!/bin/bash
                  NEW_HOSTNAME=${hostName}
               -  {
                  share: !FindInMap [Regions, !Ref "AWS::Region", SHARE],
                  hostName: !Join ["", [!Ref Name, !FindInMap [Regions, 
                                                         !Ref"AWS::Region",USERDATA] ] ]
                  }
like image 36
david bob Avatar answered Nov 02 '25 13:11

david bob