AWS CloudFormation provides a powerful mechanism for starting a “Stack” in AWS, using any one of the hundreds of Amazon Machine Images available.Opscode Chef is a fantastic tool for provisioning a vanilla server instance into something more useful. Sure, there are many images available with a pre-configured setup, but in reality operating systems are patched frequently, as are the hundreds of packages they include. This means the AMI images start to get stale as soon as they’re created.So, if you need a way to start the latest Ubuntu, install the specific foundation you need and keep it that way, then Chef and Cloud Formation can help.
In my next article, I’ll show how to do all this using a Cato Task.
Chef makes use of ‘recipes’ and ‘cookbooks’ to provision software on a server, and maintain a specific configuration. The Chef server is a great way to manage a big infrastructure and keep all your systems in line. But, whether you use Chef Server or not, the easy to install Chef Solo allows you to use Cookbooks on just about any OS, with little fuss.
This document makes two assumptions:
1) the reader is familiar with the basic concepts of Amazon AWS and EC2
2) the reader has at least a conceptual understanding of Opscode Chef.
The example I’ll describe below is simple. We will:
“AWS CloudFormation gives developers and systems administrators an easy way to create and manage a collection of related AWS resources, provisioning and updating them in an orderly and predictable fashion.” (See http://aws.amazon.com/cloudformation/ for details about AWS Cloud Formation.)
According to Opscode, “Cookbooks are the fundamental units of distribution in Chef.” (See http://wiki.opscode.com/display/chef/Home for all you could ever care to know about Chef.)
An AWS CloudFormation template is a JSON formatted declarative document that guides Cloud Formation to create resources in AWS. Most Cloud Formation templates include at least some Parameters (inputs to the process), Resources (a few ‘things’ to create) and ‘User Data’ (typically some shell commands or a complete script to execute when the instance is available.)
Here is a very simple example Cloud Formation template that would start up an EC2 instance, including the requisite Security Groups and IAM Access Key. This template starts a t1.micro instance in the us-east-1 region. (We’re not gonna pick it apart and explain every detail, this is just a basic working template as an example.)
So, if we feed this template to the Create Stack wizard in AWS, we will get a basic Ubuntu 10.04LTS instance. That’s nice and all, but how about some cool stuff?
AWS CloudFormation templates have the ability to install packages, create files and make configuration changes through several mechanisms: a tool called cfn-init which is available in the Amazon Linux images, and Canonical has created a bunch of AMI’s supporting their similar Cloud-init feature. Both are great tools, and if you’re good working with that smaller set of images, use the one that best fits your needs. The user data method works on any instance, and since an upcoming article will show how to do this on Eucalyptus and Openstack clouds, this example uses good old-fashioned User Data.
With the instructions on the Opscode site for installing Chef Client, I put together this bash script that:
(See the comments in the script for an explanation of what each command is doing.)
Since a CloudFormation template is a JSON document, we’ll need to escape quotes and backslashes in order to put this script in the template. Also, take a look at the “Ref” keywords near the bottom – that’s the CloudFormation template way of capturing input variables and then sticking them in the proper place. Here’s what it looks like, escaped and comments removed:
That’s it! This snip of user data will install Chef and use the WordPress cookbook to get a WordPress site up and running in minutes. This is an easy, repeatable process using Cloud Formation. Simply change the knife command, and the ‘run_list’ to install anything Chef has to offer.
Finally, here’s the entire template I used while writing this, just to save you the time!
Sorry, comments are closed for this post.