O'Reilly logo

Python and AWS Cookbook by Mitch Garnaat

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Executing Custom Scripts upon Instance Startup

Problem

You need to run custom scripts each time a new instance is started.

Solution

Use the user_data feature to pass scripts and/or data to your newly started instance.

Discussion

Once you get beyond the experimentation phase with EC2, you will quickly find that in addition to just starting an instance, you want to prepare that instance for a particular task. Why would you be starting an instance if you didn’t have an important job for it to perform? It turns out there are two main schools of thought regarding how best to accomplish this:

Custom images

This approach focuses on starting with a plain vanilla image and then configuring all of the required software necessary to perform the desired task. Once everything is installed and configured, you create a new image based on this fully configured image, and that becomes the image that you use. When an instance based on this image is started, it has everything it needs to perform its task.

Startup time configuration

This approach also starts with a plain vanilla image, but all of the installation and configuration is done when the instance starts up. This could be accomplished with scripts and/or data that are passed the instance at startup time, or it could use a more sophisticated, centralized configuration service like Puppet or Chef.

Deciding which is right for you is a trade-off. Custom images provide quick start time and fewer dependencies but are less flexible. Each time you want to update ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required