Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 991
sashoalm Avatar asked Nov 11 '17 17:11

sashoalm


2 Answers

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

like image 121
John Hanley Avatar answered Nov 12 '22 01:11

John Hanley


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.

like image 34
Ashan Avatar answered Nov 11 '22 23:11

Ashan