AWS Cloudformation – Bootstrapping an Ec2 instance with Userdata

In a serie of blog articles I’ll take a closer look at AWS Cloudformation. Read more about what AWS Cloudformation is and how to design templates in my previous blog articles AWS Cloudformation – Templates, stacks and change sets and AWS Cloudformation – Designing Templates.

In this blog article I’ll get more into detail about customizing the OS of your instance during the deployment of a template and how this is all managed by Cloudformation helper scripts.

Bootstrapping an Ec2 instance with Userdata

The cloud-init package is an open-source application built by Canonical that is used to bootstrap Linux images in a cloud computing environment. EC2 instances contains a customized version of cloud-init. It enables you to specify actions that should happen to your instance at boot time. You can pass desired actions to cloud-init through the user data fields when launching an instance.

You can use AWS CloudFormation to automatically install, configure, and start applications on Amazon EC2 instances. Doing so enables you to easily duplicate deployments and update existing installations without connecting directly to the instance, which can save you a lot of time and effort. Userdata is a property of the EC2 instance resource type.

Ec2Instance:
  Type: AWS::EC2::Instance
  Properties:
    UserData:
      !Base64 |
      #!/bin/bash
      yum -y update
      ..

A basic bootstrap script runs only on the first boot of the instance and is specifically useful to make sure your freshly installed instance has the latest updates, required packages or configurations etc. The UserData properties needs to be 64base encoded and starts with #! and the interpreter. It’s basically shell scripting with some limitations. It’s not interactive so there is no direct feedback. You can redirect your output to an file like this.

exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

This line has to be at the top of your Userdata section. This way you have your debug logging available on the instance you just deployed.

Also, a drawback of this approach is that the Userdata section can become a little messy after a while. To solve this Cloudformation provides a metadata section where you can describe any implementation details regarding your resource. Additionally, python based helper scripts helps you interact with Cloudformation and access the specific metadata of a resource declared in the template.

CloudFormation helper scripts

CloudFormation includes a set of helper scripts (cfn-init, cfn-signal, cfn-get-metadata, and cfn-hup) that are based on cloud-init. You call these helper scripts from your AWS CloudFormation templates to install, configure, and update applications on Amazon EC2 instances that are in the same template.

cfn-init

Reads and interprets metadata to execute AWS::CloudFormation::Init. This script is called in your UserData section.

cfn-signal

The cfn-signal helper script signals AWS CloudFormation to indicate whether Amazon EC2 instances have been successfully created or updated. If you install and configure software applications on instances, you can signal AWS CloudFormation when those software applications are ready.

cfn-get-metadata

Can be used to retrieve metadata based on a specific key.

cfn-hup

The cfn-hup helper is a daemon that detects changes in resource metadata and runs user-specified actions when a change is detected. This allows you to make configuration updates on your running Amazon EC2 instances through the UpdateStack API action. We already seen this helper script in action in Elastic beanstalk.

As already mentioned the Userdata can become a bit messy. This can be solved by using metadata in the resource section where you already declared the EC2 instance. This will be explored in more depth next time.

Vincent Lamers

Vincent Lamers, Linux-consultant @ AT Computing

Onderwerpen
Actieve filters: Wis alle filters
Loading...