Search code examples
amazon-web-servicesyamlaws-cloudformation

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


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?


Solution

  • The intrinsic function Fn::Sub (YAML !Sub) substitutes variables (such as the ${AWS::StackName} variable in your example) 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