What does "!Sub |" mean in AWS UserData field with YAML syntax?

13,621

Solution 1

The intrinsic function Fn::Sub (YAML !Sub) substitutes variables in an input string with values that you specify. In your templates, you can use this function to construct commands or outputs that include values that aren't available until you create or update a stack.

The character '|' (pipe symbol) means "Literal Style". This uses a simpler, more readable scalar style. This means that you can enter normal looking text without having to use things like "\n" to mean end of line.

Fn::Sub

YAML Spec

Solution 2

In your UserData section the !Sub function substitutes variables in the UserData string with values that you specify or with pseudo parameters like AWS::StackName and AWS::Region.

In addition the pipe symbol at the end of a line in YAML signifies that any indented text that follows after the !Sub | should be interpreted as a multi-line scalar value.

For more details refer, intrinsic function details documentation.

Share:
13,621
sashoalm
Author by

sashoalm

Updated on June 15, 2022

Comments

  • sashoalm
    sashoalm almost 2 years

    In this example from AWS docs we have a UserData field that allows a multiline string, using the following syntax:

    UserData:
      Fn::Base64: !Sub |
         #!/bin/bash -xe
         yum update -y aws-cfn-bootstrap
         /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchConfig --region ${AWS::Region}
         /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerGroup --region ${AWS::Region}
    

    What does !Sub | mean here, especially the pipe character? The corresponding JSON uses "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ instead, but in the YAML, !Sub | is used.

    Is the pipe character standing for a newline, saying lines must joined by newlines?

  • Tim
    Tim almost 4 years
    If I could double vote this answer I would. I have been going nuts trying to figure out why we were piping data from one command to another.