Amazon EC2 HowTo

I’ve recently started working with Amazon’s Elastic Compute Cloud (EC2) for my personal project and decided to capture the answers to some of the beginner questions that I worked through. Amazon provides a great deal of documentation, but I believe the organization of their documentation could use some work.

Before you begin working with EC2, I recommend you start by reading the Overview of Amazon Web ServicesArchitecting for the AWS Cloud: Best Practices and Getting Started with EC2.  These documents will give you some fundamental background to help you on your way to using EC2.

Here are the answers to some basic start-up questions:

1) How do I launch an instance of an Amazon Machine Image (AMI)?

You can find the answer here.  Launching an instance of an AMI gives you a running “machine” that conforms to the configuration of the specific AMI, on which you can then deploy your applications.  You can find a list of available AMIs here.

2) How do I connect to my machine instance?

You’ve started your instance, now you want to open a terminal session to it from your local workstation.  You’ll need to use SSH with the key file you generated when you launched your instance.  You can find a detailed explanation here.  The SSH command will look like this:

ssh -i keyfile.pem username@hostname

The root user login has been disable, so instead each image has a default username that you must use to SSH in to the instance.  I have found that the default user name sometimes pops up in a dialogue box when you launch the instance, but more often than not it doesn’t and you have to hunt for it.  For example, the user name for Amazon’s Linux image can be found in their user guide.  Alternatively, if you just try to SSH into your instance using “root” for the username, the connection will fail and respond with the required username.  It’s the fastest way I’ve found to find the right username, but if someone knows where I can find it in the AWS management console, please let me know.

3) How do I load my own software on the instance I started?

Amazon’s documentation isn’t very clear on this.  You’ll need to use SCP to upload files from your local machine to your running instance.  The command to upload a source file on your local machine to a target file on your running instance will look like this:

scp -i keyfile.pem source-file-name username@hostname:target-file-name

You’ll use the key file that you generated when you launched your instance (the same one you used for your SSH session).  The default username is the same one you used to establish an SSH session, described above.  The host name (public DNS) is available in the AWS management console, described in the metadata for your running instance.

4) Once I’ve gotten my AMI configured the way I want it, how do I save it for future use?

Amazon’s documentation is somewhat lacking on this subject.  It can be a little difficult to decipher the difference between an S3-backed instance and anEBS-backed instance or to understand the use cases that would motivate you to choose one over the other.  According to Amazon’s documentation:

An Amazon EC2 instance can be launched from an AMI backed by Amazon EBS or from an AMI backed by Amazon S3. Instances launched from AMIs backed by Amazon EBS use Amazon EBS volumes as their root devices. Instances launched from AMIs backed by Amazon S3 use an instance store as the root device (e.g., / or C:\).

There’s a bit of ambiguity here.  What does it mean to say that an S3-backed instance has an instance store as the root device?  After reading moredocumentation, what I think this means is that the state of the S3-backed images is strictly transient.  Once you terminate the instance, any changes you make to the instance configuration are lost.  For S3-backed instances, the root device persistence is on the local disk (instance store) of the machine on which the instance is running, which is only available for the life of the instance.  Conversely, when you stop an EBS-backed instance, its state is automatically persisted to an EBS volume.  Stopping an instance is not the same as terminating an instance.  A terminated instance cannot be restarted.  So, if you want to make changes to your instance configuration and keep it around for future use, you must create an EBS-backed instance, and you should only stop it, don’t terminate it.

Having said all of that, what you really want to do is to create a new AMI based on the running instanced you’ve configured.  Although there is a browser-based management console that will let you start instances of existing AMIs, it looks like one must use the command line API tools to save your configured instance..  Once you’ve created the new AMI, you can then instantiate it from the console at a future time.  The command to create the image from the instance follows below.

ec2-create-image instance_id [--name name, --description description, --no-reboot]

The instance_id can be retrieved from the web console for the running instance that you want to save as your new AMI.  The documentation for all of the command line tools can be found here.  It takes a little bit of effort to get your local environment set up to run the command line tools, but the documentation is clear and straightforward, so there’s no point in repeating it here.

Feel free to contact me if you have comments or questions.

Comments

  1. Pingback: Getting Started with Amazon’s Elastic Compute Cloud « Engineering Notebook

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">