What’s a Sunday without Pale Ale

Setting up an Ubuntu EC2 instance

James Hamann
7 min readAug 14, 2017

I’ll split this in two halves, the first section will be setting up on AWS and the second section will be setting up on your Ubuntu instance.

Setting up on AWS

I’m new to all things linux so figured I’d write up my process for setting up an Ubuntu EC2 instance and installing all the necessary things needed. I’ll assume you’re set up on AWS, if not you’re going to need to create an account here.

First you’re gonna head over the EC2 Dashboard, which should look a little something like this. You’ll notice I’ve chosen London as it’s the nearest region to where I am, make sure you choose the closest available region to you as well. One thing to note, however, is that the type of EC2 instances may vary between regions, so if my list looks slightly different from yours, that’ll be why.

EC2 Dashboard

Obviously you’re going to want to hit “Launch Instance”, which will take you a list of available instances in your region. There’s a large variety to choose from, but for the purposes of this post we’ll choose Ubuntu Server 16.04 LTS, which stands for Long Term Support and ensures there’s support for the server for at least 5 years.

EC2 Distributions

The next screen you’ll be faced with goes through the different instance types, we’ll want to make sure we pick the t2.micro as it’s part of the free tier. However, it’s worth knowing roughly what each instance class is best suited for. Obviously t2.mircos are for experimenting and playing around, anything in the M class is for general purpose web apps. Upwards from there, you’ll notice the C class, which is for compute intensive applications, the R class, which is memory optimised so think lots of RAM, X is bascially an X-treme version of R and both D and I represent storage optimised instances.

Types of EC2

Before reviewing and launching, we’ll need to set up a few extra things so click Configure Instance Details, which will bring you to this page.

Configuring your EC2

All of this can be left as is, but it’s important to at least understand what some of it represents. Obviously the top option allows you to choose the number of instances you want to launch.

Spot Instances are actually kinda cool and basically allow you to bid on excess EC2 Computing Capacity at low demand times of the day. In a nut shell, you’ll set a bid price for the instance and as long as that bid price is greater than the spot price, you’ll be able to run instances. However, as soon as the spot instance price rises above your bid price, you loose your instances. This is great for applications where cost is a key factor and interruptions are ok.

IAM Roles is a whole separate topic, which I’ll probably cover in a later post, we’ll leave it as is for now.

The last section allows you to choose the shutdown behaviour, whether you want the instance to stop or be terminated straight away. The difference is that if you stop, you can still re-boot, however there’s no way back from termination. Thankfully, though, there’s also the ability to prevent accidental termination, which is pretty useful but probably not applicable for us right now. You are also able to enable CloudWatch monitoring and change the tenancy of your instance. This can be a cost effective solution if you know you’re going to need an instance for a set amount of time. It provides better rates than the normal EC2 tariff rates. Please note both these options come at an additional cost, so let’s just leave it as it is.

The next section will allow you to add storage to your instance, again for the purposes of this we’ll be ok leaving it as it is, at the default 8GB.

Adding Storage to your Instance

Next up is Tags, a great way to keep your instances organised. They work in key value pairs and can help you quickly distinguish your instances apart, especially if you have the same application running in a staging and production environment.

Adding Tags to your Instance

Next you’ll be setting up a real simple security group for your instance which specifies what traffic can be allowed into your server.

Creating/Configuring a Security Group

Obviously we’ll want to SSH in, so that’s an obvious one and we’d probably want to allow users to be able to visit the site over HTTP and HTTPS. I’ve also added PostgreSQL, if you’re going to connect to a separate database. You’ll notice the warning, which is absolutely fine, it’s just letting you know that all IP address are able to connect to your instance.

After this is complete you’ll be ready to launch. You’ll be taken to a page which allows you to review your instance if you wish. Click “Launch” and you’ll be greeted with a pop up asking to either choose or create a key pair to access your instance securely. Lets create a new one for this instance by choosing the Create a new key pair option from the dropdown list.

Generating a new Key Pair to securely access your Instance

Call it whatever you want and hit download, this will generate a key pair and download the .pem file for you. I have an SSH directory within my downloads folder, but as long as it’s accessible for you, that’s ok.

Your instance will now take a minute or two to launch and warm up, in the mean time open up your terminal and navigate to the directory where your .pem file is located. In order to be able to SSH into your instance, you’re going to need to change the permissions on the file with the following command.

$ chmod 400 mykeypairname.pem

Once your instance is all setup and ready your EC2 dashboard should look something like this.

EC2 Dashboard

Now we’re ready to connect to the instance.

Setting up on your Instance

First things first, you’re obviously going to need to SSH into your newly launched instance. Make sure you’re in the directory where your pem file is located and that you’ve changed the permissions on it as mentioned above.

For Ubuntu instances the user is called “Ubuntu”, I believe it’s different depending on what distribution you use but I know Amazon uses ec2-user instead, so just make sure the user actually exists when trying to SSH in.

The command used to ssh in is simply:

$ ssh -i YourKeyPair.pem ubuntu@YourInstancePublicIP

Your instance’s public IP address can be found on the dashboard here:

The far right column IPv4 Public IP is your instance’s public IP Address

You’ll be asked if you’re sure you want to connect, to which you’ll obviously reply yes. Once that’s done you’re in!

A good thing to do first up is to get a quick update, however before doing this you’ll need to change to the root user, as the current user doesn’t have the required permissions.

$ sudo su //changes to root user
$ apt-get update //will run an update on the instance for you

Your instance is pretty much setup to go now, bear in mind though you’re starting from scratch so depending on your tech stack you’ll need to download everything from scratch from java to PostgreSQL to everything in between.

$ apt install postgresql 
...
$ apt install default-jre-headless
...
//install everything you need for your application

That’s about it in a nutshell, obviously you’ll need to consider setting up your psql users etc… One thing I’ll mention is this setup assumes you’ll run your application and database on the same server, obviously this isn’t best practice and you should setup a separate database instance using AWS RDS or on your own by launching another EC2 instance.

Whilst quite a few of us use tools like Heroku and Beanstalk to take care of application deployment, it’s still useful to understand how everything works and how to set it up. One thing I’ll stress, though, is don’t overcomplicate if you don’t have to. Yeah I know, we’re getting into a different topic here about IAAS vs PAAS, which is a whole subject on it’s own. But, every application has it’s own requirements and both services suit different applications well, so choose whatever works and suits your application best.

Thanks for reading, hit 💚 if you like what you read and be sure to follow to keep up to date with future posts.

--

--