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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With