1

I'm bootstrapping a EC2 Windows 2016 instance via CloudFormation Metadata using cfn-init.

My goal is to create a .properties file on the instance, where each property line must come on a separate line. I have tried with both the approaches below. But everytime, instead of creating multiple lines, all the lines are concatenate.

Sample file I'm trying to create:

INSTALLER_UI=SILENT

CONTROLLER_PORT=\"8080\"

LISTEN_PORT=\"7070\"

But the file that gets created every time:

INSTALLER_UI=SILENTCONTROLLER_PORT="8130"LISTEN_PORT=\"7913\"

Cloudformation template code using !Sub

Metadata:
  AWS::CloudFormation::Init:
    config
      - 01InstallMyAgent
    01_InstallMyAgent:
      files:
        "D:\\installers\\Agent\\agent.properties":
           content: !Sub |+
             INSTALLER_UI=SILENT
             CONTROLLER_PORT="8130"
             LISTEN_PORT=\"7913\"

Cloudformation template code using !Join

Metadata:
  AWS::CloudFormation::Init:
    config
      - 01InstallMyAgent
    01_InstallMyAgent:
      files:
        "D:\\installers\\Agent\\agent.properties":
          content: !Join ["\n",
            [
              "INSTALLER_UI=SILENT",
              'CONTROLLER_PORT=\"8130\"',
              'LISTEN_PORT=\"7913\"',
            ],
          ]

I've also tried join with "" as the delimiter and appended "\n" to the end of each line but absolutely no luck.

Question 2: Also pls. suggest a way to test the above code locally somehow. Right now I only have to execute the stack, RDP into the EC2 instance and check the file contents, only to see that my small change has failed. Any way to test this out without having to actually create the instance. Thanks...

Question 3: Are there any repository of Windows cloudformation samples. Because that is very hard to find. Almost all the samples I find are for linux.

Thanks a lot !!

  • This link will be helpful to understand yaml formatting. https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines – SRJ Jan 12 '21 at 08:23

1 Answers1

1

You can use the literal style which is denoted by the | indicator. Here's an example from one of our templates:

        UserData:
          Fn::Base64: !Sub |
            Content-Type: multipart/mixed; boundary="==BOUNDARY==" 
            MIME-Version: 1.0 

            --==BOUNDARY== 
            MIME-Version: 1.0 
            Content-Type: text/x-shellscript; charset="us-ascii"

            #!/bin/bash
            yum install -y aws-cli
            AUTH_DATA=$(aws secretsmanager get-secret-value --secret-id stk-docker-repository-stage --region ${AWS::Region} --query SecretString --output text)
            cat <<EOT >> /etc/ecs/ecs.config
            ECS_DISABLE_IMAGE_CLEANUP=false
            ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION=2m
            ECS_IMAGE_CLEANUP_INTERVAL=10m
            ECS_IMAGE_MINIMUM_CLEANUP_AGE=10m
            ECS_NUM_IMAGES_DELETE_PER_CYCLE=5
            ECS_RESERVED_MEMORY=32
            ECS_ENGINE_AUTH_TYPE=docker
            ECS_ENGINE_AUTH_DATA={"stk40-docker-stage-dev-local.bahnhub.tech.rz.db.de":$AUTH_DATA}
            EOT

            --==BOUNDARY==
            Content-Type: text/x-shellscript; charset="us-ascii"

            #!/bin/bash
            #upgrade der EC2
            yum upgrade -y
            yum install -y wget
            yum install -y git
            yum install -y aws-cli
            yum install -y jq

            <cut>            
            --==BOUNDARY==--
lexicore
  • 42,748
  • 17
  • 132
  • 221