AWS CloudFormation Init

From Bonus Bits
Jump to: navigation, search

Description

This article gives information on Amazon Web Services CloudFormation Bootstrap or Init script. The following applies to RHEL, CentOS and Ubuntu. Amazon Linux uses a different cloud-init setup that is a little better.


Example Template Syntax

Bash Commands

"Mappings": {
    "bootstrap": {
      "rhel": {
        "warmebs": "echo 'ACTION: Performing EBS Warming' && dd if=/dev/xvda of=/dev/xvda conv=notrunc bs=1M status=progress",
        "installpackages": "yum install -y curl wget mlocate ca-certificates openssl vim",
        "installchefdk": "yum install -y https://packages.chef.io/stable/el/7/chefdk-0.12.0-1.el7.x86_64.rpm",
        "installbastionpackages": "yum install -y git wireshark nmap mysql"
      },
      "ubuntu": {
        "warmebs": "echo 'ACTION: Performing EBS Warming' && dd if=/dev/xvda of=/dev/xvda conv=notrunc bs=1M status=progress",
        "installpackages": "apt-get update && apt-get install curl wget cloud-utils ca-certificates vim",
        "installbastionpackages": "apt-get install mysql openssl"
      }
    }
  }

UserData

This is where you piece together your script under the properties of an EC2 Instance resource or LaunchConfiguration.

"UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "", [
                "#!/bin/bash\n",
                {"Fn::FindInMap": ["bootstrap", "rhel", "warmebs"]},
                "\n",
                {"Fn::FindInMap": ["bootstrap", "rhel", "installpackages"]},
                "\n",
                {"Fn::FindInMap": ["bootstrap", "rhel", "installbastionpackages"]},
                "\n",
                {"Fn::FindInMap": ["bootstrap", "rhel", "installchefdk"]},
                "\n"
              ]
            ]
          }
        }


Cloud Init Log

The cloud initialization logs are located here: /var/log/cloud-init.log