One of the most under rated features of the AWS platform have to be user-data scripts. They allow you to easily preconfigure EC2 Linux instances at launch via a script (or selection of scripts) so they're ready roll complete with all your build specifics and/or applications from first boot. Very useful, especially for anyone who's had experience of chef and puppet. You'll find the entry field at stage two of the instance launch process (Fig:1).
The user-data function utilises Cloudinit, an Ubuntu package that handles early initialisation of a cloud instance. It is installed in both the Canonical UEC Images and also in the official Ubuntu images available on EC2.
Some of the things Cloudinit configures (among other things) are:
- Setting a default locale
- Setting hostname
- Generate ssh private keys
- Adding ssh keys to user's .ssh/authorized_keys so they can log in
- Setting up ephemeral mount points
Basically, if it can be scripted a user-data/cloudinit combo can handle it, and it's amazingly powerful in its simplicity. Here's a basic example, it's something I used to pre-stage Ubuntu 10.04 EC2s for ZPanel6 installations (*UPDATE* - ZPanel6 is now discontinued) :
apt-get update && apt-get upgrade -q -y
tar -zxvf zpanel-6.1.1.tar.gz -C /etc/zpanel/
chmod +x /etc/zpanel/lib/dev/zpinstall_ubuntu.sh
This simple bash script will update the box, pull down the installation script, expand it, and set the correct permissions on the install folder. Once the box is booted I then just have to run through the quick install procedure (See previous post).
*TIP* - In practice I've found that uploading an .sh file, as oppose to just entering the script syntax in the text box, works much better, i.e it actually does work. I've never succeeded in getting the above script to run 'text only', uploading the shell script version not only works every time, but once it's uploaded you can simply reselect it for future instance launches.