Create a gist now

Instantly share code, notes, and snippets.

Ubuntu CloudFormation Tools installation snippet. Modified version of http://krunchtime.it/setting-up-bootstrapping-using-cfn-init-for-aws-ubuntu-ec2-instance
"UserData": {
"Fn::Base64": { "Fn::Join":["", [
"#!/bin/bash -ex\n",
"apt-get update\n",
"apt-get -y install python-setuptools\n",
"mkdir aws-cfn-bootstrap-latest\n",
"curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1\n",
"easy_install aws-cfn-bootstrap-latest\n",
"/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, " --resource WebServer", " --region ", { "Ref": "AWS::Region" }, "\n",
"\n",
"/usr/local/bin/cfn-signal --exit-code $? '", { "Ref" : "WaitHandle" }, "'\n"
]]}
}
@franklanganke

I got this working by adding a comma at the end of line 9 and defining the "WaitHandle" earlier in the template.

@kixorz
Owner
kixorz commented Oct 5, 2015

Thanks! I'll update it.

@dougireton

Another example which uses pip instead of easy_install and works behind a proxy:

    "UserData": {
          "Fn::Base64": {
            "Fn::Join": ["", [
              "#!/bin/bash\n",
              "set -o errexit; set -o nounset; set -o pipefail\n",
              "echo 'Acquire::http::proxy \"http://myproxy.example.com:8080 \";' > /etc/apt/apt.conf.d/80proxy\n",
              "echo 'Acquire::https::proxy \"http://myproxy.example.com:8080 \";' >> /etc/apt/apt.conf.d/80proxy\n",

              "# Install AWS cfn-bootstrap utilities\n",
              "apt-get update\n",
              "apt-get -y install python-pip\n",
              "pip install --proxy http://myproxy.example.com:8080 https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",

              "/usr/local/bin/cfn-init",
              " --stack ", { "Ref": "AWS::StackName" },
              " --resource MyLogicalResourceName",
              " --configsets bootstrap-chef",
              " --region ", { "Ref": "AWS::Region" }, "\n"
            ]]
          }
        }
@lukeoliff

Another more basic example with easy install for Debian 8.

    "UserData": {
      "Fn::Base64": { "Fn::Join":["", [
        "#!/bin/bash\n",
        "apt-get update\n",
        "apt-get -y install python-setuptools\n",
        "easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
        "cfn-init --stack ", { "Ref": "AWS::StackName" }," --resource LaunchConfig --region ", { "Ref": "AWS::Region" }, "\n",
        "cfn-signal -e $? ","--stack ", { "Ref": "AWS::StackName" }," --resource WebServerGroup --region ", { "Ref": "AWS::Region" }, "\n"
      ]]}
@wtfiwtz
wtfiwtz commented Oct 12, 2016 edited

On Ubuntu 16.04.1 LTS this works:

#!/bin/bash -xe

mkdir -p /tmp/aws-cfn-bootstrap-latest

curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-1.4-8.tar.gz | tar xz -C /tmp/aws-cfn-bootstrap-latest --strip-components 1

curl https://s3-ap-southeast-2.amazonaws.com/bucket/cfn-hup.service -o /etc/systemd/system/cfn-hup.service

apt-get update

apt-get install -y python-pip libssl-dev libffi-dev

pip install pyopenssl ndg-httpsclient pyasn1

pip install --upgrade /tmp/aws-cfn-bootstrap-latest

cp /tmp/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
/etc/init.d/
update-rc.d cfn-hup defaults
chmod 755 /etc/init.d/cfn-hup


A sample cfn-hup.service file is here: adamreeve/asp.net-docker@96f86bf

... although you might need to set the launch path to /usr/local/bin/cfn-hup

Another issue was that I had to disable the cfn-hup service setup - maybe Cloudformation's cloud-init and cfn-init is not compatible with systemd. You can install the upstart-sysv Ubuntu package to switch back to upstart although I had other difficulties with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment