This is the first post in a series on how to set up infrastructure with Amazon Web Servies (AWS)
While the services offered by Amazon Web Services (AWS) are extremely useful, there is quite a learning curve when it comes to AWS and its dizzying amount of jargon. Although this post does not aim to make you an AWS expert, it will help you set up a standard Ubuntu VM in the Amazon cloud in only a few minutes.
Sign up for Amazon Cloud Services
To create VMs in the Amazon cloud, you first need to create an Amazon account.
Go to aws.amazon.com and create an account.
You will be asked to enter billing information as part of the sign-up process, but you won’t be charged for anything if you stay within the free usage tier.
You can find out more about the free usage tier on the official Amazon AWS documentation:
Create an EC2 instance
Once you have signed in to the AWS console, you will notice the staggering amount of services offered by Amazon. However, to create a VM, you just need to navigate to the EC2 page (EC2 is essentially Amazon’s way of saying VM) and follow these steps:
1 – Create VM
- On the EC2 dashboard, click Launch Instance. This creates a new VM.
2 – Select Machine
- Select an Amazon Machine Image that the VM will use. For the purposes of this tutorial we chose a 64-bit Ubuntu Server 14.04 LTS (HVM) AMI.
3 – Instance Type
- Choose an Instance Type for the VM. We selected t2.micro for this tutorial because it’s free.
4 – Instances
- On the next page, all the default settings are fine. Set the number of instances to 1 and check that Request Spot Instances is not selected. Note that the public IP of your EC2 instance will change every time it is rebooted, and this cannot be changed unless you assign an Elastic IP to your EC2 instance. However, the Public DNS entry will stay the same and points to the public IP.
5 – Storage
- On the next page you’ll add storage to your VM. The t2.micro instance type uses EBS, which is Amazon’s cloud storage option. Specify the amount of storage that you want. You can increase this amount later or add another EBS volume to the instance.
6 – Instance Name
- You will need to give your instance a name on the next page. The name doesn’t really matter, so you can give it whatever name you want.
7 – Security Groups
- Next, you need to configure a security group for the instance. The security group specifies which connections will be allowed. This can be changed at any time, so for now create a new security group and allow incoming SSH connections from your IP address.
8 – Key Pair
- Now you are ready to launch the instance. You will be prompted for a key pair that will be used to access your instance. Create a new key pair and give it a name. Download the key pair and save it on your computer. Make sure you do this because you won’t get a chance to download it again.
9 – Launch Instance
- Click Launch Instances and it will be up and running in a minute or so.
SSH into your EC2 Instance
Go to the EC2 dashboard and select Running Instances. This will bring up a list of your EC2 instances. When you select an instance it will display some information about it. Find the Public DNS entry of your instance.
Run the following command to connect to your instance via SSH, where name-of-key.pem is the name you gave to the key pair file you created and DNS is the Public DNS entry of the instance:
If you are unsuccessful and you see this error:
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
This means that the key file does not have the correct permissions. Run the following command to change the permissions, and then try and SSH into the instance again:
Amazon is very strict with the permissions of the key files, and they have to be 400 for the SSH connection to work.
If you still cannot SSH into your instance after doing this, double check the security group settings and ensure that there is an inbound rule that allows SSH connections from your IP address.
Configure your EC2 Instance
Now that your instance is up and running and you are connected to it, you should configure it for your needs.
Fix the Locale Error
In our experience with AWS, we realised that new Ubuntu instances always display a locale error.
locale: Cannot set LC_ALL to default locale: No such file or directory
Not only is this error annoying, we found that it also interfered with some of our perl scripts and caused them to fail, so it is better to fix it than ignore it. Because we’re a company based in South Africa, we use the en_ZA locale. To fix the error, generate the required locale:
edit the .bashrc file:
and add these two lines to the file:
While we’re setting the locale of the VM, we might as well also set the correct timezone on the VM.
The default timezone is set to UTC +0000. You can change this to your timezone by executing the following command and then selecting your time zone from the list:
Add More EBS Storage to your Instance
You might want to add more storage to your instance by adding an additional EBS volume to it.
Navigate to the EC2 Management Console and select Volumes under the Elastic Block Store heading in the bar on the left, then click on Create Volume. This will display a screen to create the volume. Select the type of storage you want and the size of the EBS volume to create. The availability zone of the EBS volume should match the availability zone of the EC2 instance, and the Snapshot ID should be left blank.
Once you click Create, you will see the volume being created in the list of volumes. When its state changes to available, right click on the volume and select Attach Volume. A screen will be displayed where you need to enter the name of the EC2 instance that you want to attach the volume to. You can leave the default value for Device, and then click on Attach.
After the volume’s state changes to in-use, you need to add the volume in the EC2 instance and mount it. Mounting an EBS volume in Ubuntu works the same way as mounting any other disk. SSH into your instance and run the following command:
This will show you all the disks attached to the instance. Find the disk you just attached (usually /dev/xvdf if this is the first EBS volume you attached) and format the file system.
Only format it if this is a new EBS volume with no data on it, otherwise you will lose everything on the volume!
Create a mount point for the EBS volume:
Now mount the formatted volume, and update fstab so that it mounts after every reboot:
That’s it! You now have a VM in Amazon’s cloud. Install whatever software you require on the server and remember to edit the inbound and outbound rules on the security group so that the various ports can be accessed.