The Ubuntu and Debian EC2 images published on http://alestic.com allow you to send in a startup script using the EC2
user-data parameter when you run a new instance. This functionality is useful for automating the installation and configuration of software on EC2 instances.
The basic rule followed by the image is:
If the instance
user-datastarts with the two characters
#!then the instance runs it as the root user on the first boot.
The “user-data script” is run late in the startup process, so you can assume that networking and other system services are functional.
If you start an EC2 instance with any user-data which does not start with
#! the image simply ignores it and allows your own software to access and use the data as it sees fit.
This same user-data startup script functionality has been copied in the Ubuntu images published by Canonical, and your existing user-data script should be portable across images with little change. Read a comparison of the Alestic and Canonical EC2 images.
Here is a sample user-data script which sets up an Ubuntu LAMP server on a new EC2 instance:
#!/bin/bash set -e -x export DEBIAN_FRONTEND=noninteractive apt-get update && apt-get upgrade -y tasksel install lamp-server echo "Please remember to set the MySQL root password!"
Save this to a file named, say,
install-lamp and then pass it to a new EC2 instance, say, Ubuntu 9.04 Jaunty:
ec2-run-instances --key KEYPAIR --user-data-file install-lamp ami-bf5eb9d6
Please see http://alestic.com for the latest AMI ids for Ubuntu and Debian.
Note: This simplistic user-data script is for demonstration purposes only. Though it does set up a fully functional LAMP server which may be as good as some public LAMP AMIs, it does not take into account important design issues like database persistence. Read Running MySQL on Amazon EC2 with Elastic Block Store.
Since you are passing code to the new EC2 instance, there is a very small chance that you may have made a mistake in writing the software. Well maybe not you, but somebody else out there might not be perfect, so I have to write this for them.
The stdout and stderr of your user-data script is output in
/var/log/syslog and you can review this for any success and failure messages. It will contain both things you
echo directly in the script as well as output from programs you run.
Tip: If you add
set -x at the top of a bash script, then it will output every command executed. If you add
set -e to the script, then the user-data script will exit on the first command which does not succeed. These help you quickly identify where problems might have started.
Amazon EC2 limits the size of user-data to 16KB. If your startup instructions are larger than this limit, you can write a user-data script which downloads the full program(s) from somewhere else like S3 and runs them.
Though a shell is a handy tool for writing scripts to install and configure software, the user-data script can be written in any language which supports the shabang (#!) mechanism for running programs. This includes bash, Perl, Python, Ruby, tcl, awk, sed, vim, make, or any other language you can find pre-installed on the image.
If you want to use another language, a user-data script written in bash could install the language, install the program, and then run it.
Setting up a new EC2 instance often requires installing private information like EC2 keys and certificates (e.g., to make AWS API calls). You should be aware that if you pass secrets in the user-data parameter, the complete input is available to any user or process running on the instance.
There is no way to change the instance user-data after instance startup, so anybody who has access to the instance can simply request http://169.254.169.254/latest/user-data
Depending on what software you install on your instance, even Internet users may be able to exploit holes to get at your user-data. For example, if your web server lets users specify a URL to upload a file, they might be able to enter the above URL and then read the contents.
Though user-data scripts are my favorite method to set up EC2 instances, it’s not always the appropriate approach. Alternatives include:
Manually ssh in to the instance and enter commands to install and configure software.
Automatically ssh in to the instance with automated commands to install and configure software.
Install and configure software using (1) or (2) and then rebundle the instance to create a new AMI. Use the new image when running instances.
The ssh options have the benefit of not putting any private information into the user-data accessible from the instance. They have the disadvantage of needing to monitor new instances waiting for the ssh server to accept connections; this complicates the startup process compared to user-data scripts.
The rebundled AMI approach and building your own AMI approach are useful when the installation and configuration of your required software take a very long time or can’t be done with automated processes (less common than you might think). A big drawback of creating your own AMIs is maintaining them, keeping up with security patches and other enhancements and fixes which might be applied by the base image maintainers.
Note to AMI authors: If you wish to add to your EC2 images the same ability to run user-data scripts, feel free to include the following code and make it run on image startup:
Thanks to RightScale for the original idea of EC2 images with user-data startup hooks. RightScale has advanced startup plugins which include scripts, software packages, and attachments, all of which integrate with the RightScale service.
Thanks to Kim Scheibel and Jorge Oliveira who submitted code used in the original ec2-run-user-data script.
What do you use EC2 user-data for?