[WARNING: MAY BE OUT OF DATE]
Thanks to MOOC student Jeremy Banks for these instructions!
Amazon Elastic Compute Cloud (EC2) is part of Amazon Web Services, an infrastructure-as-a-service public cloud platform. We've prepared an Amazon Machine Image (AMI) that lets you deploy an ESaaS-ready computer in their cloud.
You have three options for how to pay for this service:
- The Amazon Web Services Free Tier gives new users 1 year of access to a Micro instance, which is sufficient to do the coursework, but may be sluggish.
- You can pay per hour (from US$0.03 per hour) to get a dedicated Small instance, which would be faster.
- If you are using this book in a course, your instructor may be able to get a donation of AWS educational credits from Amazon and allocate some of those for student use, or your instructor may have a centralized AWS account that you can use instead of creating your individual account. Check with your instructor before creating an individual account. (We have no connection to Amazon and cannot secure donations for you.)
Setting up your AWS account (one-time)
- Login to your AWS account, or if you don't have an AWS account already, go to http://aws.amazon.com/ and click "Create Account" (the right of the page). You will need a valid credit card to create an account, though you should not be charged if you use only use a Micro EC2 instance (more information here, under the "Elastic Compute Cloud" section)
- Make sure you are signed up for the EC2 service (Elastic Compute Cloud) and EBS service (Elastic Block Store) in AWS.
- You will need a Secure Shell (ssh) client on your computer to connect to your instance. Mac OS X and Linux already include the standard ssh command-line tool. For Windows users we recommend installing the free PuTTY application. See the Appendix of Engineering Software as a Service for more information on what ssh is and how to set it up.
Launching your EC2 Instance
- Log in to the Amazon Web Services (AWS) Console at http://console.aws.amazon.com/.
- Select the EC2 service.
- Select the US East (N. Virginia) region (us-east-1) from the menu in the top-right.
- Select the Images: AMIs section on the left.
- Change the filter from Owned by Me to Public Images, and enter the ID
ami-db655cb2. The saasbook image should appear in the list. Confirm that it's selected, then press the Launch button.
- Select the instance size you want. A Small instance is sufficient for this work, and inexpensive but not free. A Micro instances qualifies for Amazon Free Tier usage, but some users have found Micro instances too slow; if you start with a Micro, you can resize it to a Small instance while it is stopped—see "Using and Stopping your Instance" for more details.) Once you've chosen an instance size, select Review and Launch, then Launch.
- Key Pairs: (See the Appendix of Engineering Software as a Service for more details about how Key Pairs work and advice on managing them)
- If you have used AWS before and have an existing AWS Key Pair, you may choose that option.
- If you have an existing key pair on your home computer that you'd like to use, upload its public key to AWS by following these instructions. Note that if you use this option, you must upload your keypair to the same region as the one in which you launch your instance.
- Otherwise, you can create a new keypair by selecting the Create a new key pair, entering a name, then pressing Download Key Pair to save the
.pem file. Don't lose this file, since without it you cannot log in to your instance, and you cannot re-download it from Amazon later.
- Press the Launch Instances button.
- Congratulations! You have now launched your EC2 Instance!
Connecting to Your Instance
- Log in to the AWS Console at http://console.aws.amazon.com/.
- Select the EC2 service, then select the Instances section on the left.
- Select your instance from the list, and note the public IP address listed in the bottom pane.
You will log in to your instance in a way that allows you to run your browser (to connect to your SaaS apps) on your local computer. In particular, we will set it up so that traffic directed to localhost:3000 (as described in Chapter 3 of ESaaS) will be forwarded or tunnelled to port 3000 on your instance via the same connection you use for logging in.
To connect with tunnelling on Mac OS X or Linux, type the following command in a terminal window, replacing yourkey with the filename of your keypair and yourinstance to the instance's IP address or name reported by AWS, such as ec2-99-99-99-99.compute-1.amazonaws.com:
ssh -L *:3000:localhost:3000 -i yourkey.pem ubuntu@yourinstance
(This means: "on the local side (*), take traffic destined to port 3000, forward it over the connection, and when it gets to the other side, forward it to localhost port 3000.")
To connect with tunnelling using PuTTY for Windows, follow these instructions, using 3000 as the Source Port and localhost:3000 as the destination (and of course, ignoring the part about running MySQL Administrator!)
You are now logged in to your instance!
Ruby will be ready to go when you need it, with some packages you'll use for assignments pre-installed.
1.9.3-p484 :001 > require 'themoviedb'
1.9.3-p484 :002 > puts "Hello world"
Stopping Work or Terminating Your Instance
When you finish a work session, you should use the Instance Actions menu on the EC2 Instances section
of the AWS Management Console to stop it, so you don't continue to accumulate usage charges.
If you STOP an instance, the virtual disk connected to it persists, so you usually want this option. Usage charges for persisting the disk will apply once you have exhausted the limits of the Free tier. You can easily restart and re-connect to the instance later with all your work intact.
If you TERMINATE an instance, the virtual disk connected to it may be destroyed, depending on the settings of the AMI used to launch the instance. (AWS will warn you if this is the case, because if the EBS volume is destroyed, everything on it is lost forever, and you will need to repeat the “Launching an Instance” procedure above to re-create it.) Since the Free tier includes 750 compute-hours per month for a full year, you should not need to terminate the instance while taking this course. Even if you do, though, you should be using Git and GitHub to back up your important work to the cloud, as described in Appendix A of the book.
While your machine is stopped, you can also select "Change Instance Type" from the "Actions" menu in order to increase your instance size to small (in case you found micro to be insufficient).
Advanced topic: Connecting directly to a SaaS app on your EC2 instance
Normally, you would only connect to the SaaS app running on your instance via localhost:3000. This will work because in the ssh connection command above, we set up a tunnel from port 3000 on your computer to port 3000 of the EC2 instance.
If you want to actually deploy your app on EC2 (which we don’t recommend, as there’s a lot of other setup you need to do as well), be aware that the default security configuration of an EC2 instance will not allow you to connect to a SaaS app running on it via HTTP on the standard ports 80 and 443. That is, you cannot run a browser on your local computer and point it at a SaaS app on your EC2 image. While our suggestion is to do your production deployments exclusively on Heroku, if you really want to deploy on EC2 there are two ways to get around this:
- Recommended: change the default security settings to allow HTTP and HTTPS access, as described in Amazon's EC2 documentation or these Stack Exchange posts.
- Harder: The other is to run an X server on your local computer, such as XMing for Windows or XQuartz for Mac OS X, and allow the browser to run on the EC2 instance but display its graphics directly on your computer. The VM information page on the book's website explains how the ssh command line for logging into your instance should be changed if you want to try this option.
The X Window System originated at MIT in 1984 as a way to provide GUI facilities that would work across different operating systems and hardware. X11-based programs send graphical output to and receive user input from an X server program via TCP/IP. Confusingly, because the X server needs to draw on the screen and receive user input from the mouse and keyboard, it often runs on the client computer, with the X client running on a server computer in the cloud!