0

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!

1 Answers1

1

Referencing another answer here for the formatting of strings in yaml files. Block styles with block chomping indicator (>-, |-, >+, |+)

You can control the handling of the final new line in the string, and any trailing blank lines (\n\n) by adding a block chomping indicator character:

>,  |: "clip": keep the line feed, remove the trailing blank lines.
>-, |-: "strip": remove the line feed, remove the trailing blank lines.
>+, |+: "keep": keep the line feed, keep trailing blank lines.

For all the bash I run in cloudformation, I just use '!Sub | ' and then place the bash code on the next line like below:

UserData:
    Fn::Base64: !Sub |
      #!/bin/bash -xe
      # Pre-Bootstrap Configuration
      yum update -y
      yum install -y aws-cfn-bootstrap git docker
      usermod -a -G docker ec2-user
      systemctl enable docker
      systemctl start docker
      curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose
      chmod +x /usr/bin/docker-compose

Just ensure your indentation is correct.

Petey_Woof
  • 79
  • 4
  • That looks a good deal more simple that using the Fn::Join. I'll have to try that methodology. But thank you for the explanation of the chomping indicators. That was very helpful! – Dan Carrington Sep 21 '22 at 20:53