Beginners guide to launching EC2 Instances

This post describes basics of launching EC2 instance on Amazon Web Services. EC2 (Elastic Compute Cloud)is one of the core components of AWS, it allows you to create a virtual server in short period of time.

Just to be clear, AWS and other cloud services can be more expensive than other hosting providers, but it provides a huge number of features and knowing how to use these features might be a good addition to your resume. In short, I wouldn’t recommend it for your own small projects, check Linode or DigitalOcean, but again, it all depends on the use case.

If you haven’t used AWS before it might be a bit confusing at first, this guide will walk you through the basic setup (with best practices in mind), and some other future posts will explore other AWS areas. I won’t go through registration for AWS, as it should be straight forward (but make sure you secure your account), once you’ve registered and logged in you will see AWS dashboard.

Amazon provides a wizard on launching instances, but it’s very limiting in what you can change. The dashboard above might be slightly different to what you see, but find a link to EC2 either in Services menu or All Services submenu in the top part of the page.

EC2 Dashboard will show at a glance what’s going on in the AWS region, I have EU Central (Frankfurt) here. Click on “Launch Instance”.

The first step will list instances you can launch, you can check the one that suits you, but I prefer personally prefer Ubuntu. In the future, you can build your own AMIs so they get pre-configured to your own setup, which can be useful and time consuming.

Next step is to select the size of your instance. There are lots of different items you can select, and some are more tuned for performance than others. If you just starting your project, most likely you’ll need instance no bigger than type t2.small.

Next step is to configure it, and where it gets more complicated. In this post we’ll just launch 1 instance without Auto Scaling (you can configure EC2 so it launches more servers for you when for example CPU stays at 60-80% for 10-15 minutes)
Spot instances won’t be covered here, but they allow you to bid on unused AWS resources, which can be cheaper than on demand instances.
Network, for simplicity we will use the default VPC network. VPC is Virtual Private Cloud - virtual network where you can launch your instances and you can configure how these networks can work together.
IAM role - if you have created IAM roles you can select one here, otherwise they will be launched as the user who is launching it (you). IAM roles can be very useful if you have a number of different servers with different tasks, but for the beginning it’s not required.
Shutdown behaviour - what to do with the instance when you shut it down, you can terminate it if it was just a temporary server.
The rest you can leave as a default.

Add storage to your instance, you can configure it the way you want, but I’d advise adding /, /tmp and /var as separate volumes.

You can tag your instance, so it can be easy to find it and configure security and other policies. For example, you might want to add an environment tag to separate production servers from staging or testing.

Security groups is basically a filter for your instance. In this case we will select SSH to be available to your IP only, and also add HTTP and custom TCP Rule, add port 5000. You can also configure how other servers and security groups can access your servers, so you can separate data servers with your application logic servers.

Review everything and click Launch.

You will be prompted to create a new key pair, which will be used to login into the server. Make sure you don’t lose this key, as it might take some skills and downtime to replace it on the server.

When everything is completed you will see this page. with the instance ID, click on it to see it in the EC2 Dashboard, when the status changes to upgraded, you can SSH into it.

Accessing the instance

The pem key you’ve downloaded need to have different permissions so you can SSH into the box, change it with chmod 0400 <path to the key> and they SSH into the box. You can find the IP if you click on the instance in EC2 Instances list, run ssh -i <path to the file> ubuntu@<ip of the instance>

Run small script to see it works ok

sudo apt-get update
sudo apt-get install language-pack-en
curl https://bootstrap.pypa.io/get-pip.py | sudo python
sudo pip install flask
vim app.py

Add following code to the file (in vim to edit the file tap Esc button and tap i (input).

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    print 'hello'

To exit vim tap Esc key and type :wq (write and quit)

Test it works

FLASK_APP=app.py flask run --host=0.0.0.0

Open your browser and check <ip of the box>:5000 you should see 'hello’

For the reference sudo pip install flask is a bad behaviour, and do it in your products, I’ll cover why in one of the blog posts, subscribe to my newsletter and get notified when new posts get live.

By default each time you stop the instance IP address for that instance is released, meaning next time you launch the instance, it will have a different IP address.

Show Comments