I'm trying to configure the UserData Property for an EC2 instance in Cloudformation and, when I look at the AWS example, it is very confusing.
The example I'm looking at is from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-signal.html
Specifically, the YAML snippet shows the following example:
AWSTemplateFormatVersion: 2010-09-09
Description: Simple EC2 instance
Resources:
MyInstance:
Type: 'AWS::EC2::Instance'
Metadata:
'AWS::CloudFormation::Init':
config:
files:
/tmp/test.txt:
content: Hello world!
mode: '000755'
owner: root
group: root
Properties:
ImageId: ami-a4c7edb2
InstanceType: t2.micro
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -x
- |
# Install the files and packages from the metadata
- yum install -y aws-cfn-bootstrap
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource MyInstance '
- ' --region '
- !Ref 'AWS::Region'
- |+
- |
# Signal the status from cfn-init
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource MyInstance '
- ' --region '
- !Ref 'AWS::Region'
- |+
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Now...the main thing that confuses me is, why do some lines not use quotes and other do? And why are some lines separated with a "|" character and other are not? Also, I can't find anything to explain what the "|+" means.
Is there anyone who can help explain this example? I'm trying to run some of my own commands in UserData, but I can't figure out when to use quotes or when to use | or |+ and all that and I'd rather not try every single possible combination before getting it right.
Thanks in advance!