18 Apr 2012

The usefulness of EC2 user-data scripts

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).

Fig:1 - Instance launch with User Data field.

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) :

export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get upgrade -q -y
wget http://sourceforge.net/projects/zpanelcp/files/releases/6.1.1/zpanel-6.1.1.tar.gz
mkdir /etc/zpanel
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.


Anonymous said...

Where do you upload the script? to S3?

RichBos said...

Hello 'anonymous'.

When you run through the launch process there's an option to either input text into the text field or upload your script as a file (checkout the screenshot).

Anonymous said...

I was unable to get the script up and running(your sample one). I inputte it as a .sh file and submitted it as a file instead of a text. i cannot find the directory for zpannel as well when i try to locate it. am i doing something wrong here? just curious to test the 'user data' field out before i start writing my own. thanks in advance if you can help me with this situation.

RichBos said...

Hi, I am sorry you are having problems. This post is quite old now and ZPanel has since been upgraded to version 10, as such the wget location for the version 6 script no longer exists and the procedure for the installation is outdated.

Here is the installation script and How-To for ZPanelX - http://www.zpanelcp.com/download/

Post a Comment