We have launched a free Basic Kubernetes Course. Click here to enrol now! Kubernetes is a container orchestration platform used by enterprises worldwide and Kubernetes developers are in high demand!
This tutorial walks through the steps of creating an Amazon Elastic Cloud Compute (EC2) instance. We will create a unix instance that is available in the free tier. If you are new to Amazon EC2, please check this introduction article.
Steps to create an Amazon Elastic Cloud Compute (EC2) instance.
Step 0 : EC2 Launch Instance page
The first step is to go to the EC2 dashboard. Once you login to Amazon EC2 you should be able to see a link called EC2 in the welcome page or the Services page. Click on that link and you should see this page :
This page gives an overview of the current instances in your account, current storage volumes used and some other services that we will see later on. For now, let’s just go ahead and create an instance. Click on “Launch Instance” to create your first instance. The next step is to create the type of machine that you want to create.
Step 1 : Amazon EC2 select the Amazon Machine Image (AMI)
You can create Amazon EC2 instances with various type of operating systems. Step 1 asks you to select the operating system that you want the EC2 instance to have. There is a checkbox on the left that allows you to select the instances available in the free tier. On this page, you would have noticed that the first kind of operating system is Amazon Linux AMI. This provides amazon managed UNIX that is updated regularly; has repository access to certain softwares such as mysql, apache etc.; has included packages that ease integration with other amazon instances such as the Amazon CLI. Let’s use this for our tutorial
There are other tabs on this page that allow you to create instances from images in the Amazon marketplace. we will look at them in a later tutorial.
Step 2 : Amazon EC2 – Choose an instance type
In this step we select the hardware configuration that we want for the server. There are five different classes of hardware configuration – General Purpose, Compute Optimized, GPU Compute, Memory Optimized and Storage Optimized. They all serve different purposes. For example, you would use a compute optimized server if you have a web application that receives a high amount of traffic. Within each class, you can select a server depending on the number of cores it has and the memory (RAM). Some classes of servers such as General purpose do not have any storage space and you have to add them later on. This storage space is called Elastic Block Storage. Let’s select the ‘free tier eligible’ type for this tutorial. It has 1 cpu and 1G of memory.
Step 3 : Amazon EC2 – Configure Instance details
The first option allows you to create more than one instance or make it a part of auto scaling group so that amazon can add or remove similar instances based on the work load. We will look at this later, for now we need one instance and no auto scaling group.
Amazon AWS has a payment option where you can bid for spot instances. This is hardware that amazon allows you to use when their hardware is underutilized. It could be much cheaper than the normal rates. However, you have no control over when amazon would create that instance and when it would delete it. We don’t use it for this tutorial.
We can select the VPC and the network that it belongs to. Chose the defaults now and in a later tutorial we will look at VPCs.
You could add a role to the AWS instance so that it comes with a pre-defined set of permissions. Note that the role cannot be added later on so if you need one, add it now. For this tutorial, we don’t add anything.
You can specify whether the instance should be stopped on shutdown or terminated. We will select terminated.
The next step is to add storage to the server
Step 4 : Amazon EC2 – Add Block Storage
With the General Purpose servers you can add as much block storage as you require. Block storage is just like a hard disk. There are three flavors of block storages – General Purpose SSD, Provisioned IOPS SSD and magnetic. For our use General purpose will suffice.
Step 5 : Amazon EC2 – Tag Instance
You can apply custom tags to instances. These tags are useful when you want to generate usage or billing reports based on certain criteria. For example, we can tag the instances with departments and then its easy to generate usage or billing report for a particular department.
Step 6 : Amazon EC2 – Configure Security Group
Amazon provides security groups that are like firewall rules. Security groups are an easy way to provide access to various ports of the machine. In our example we just want to allow access to port 22 for SSH. we can allow access from all machines or from a particular IP. The interface provides a dropdown in the first column that provides an easy way to select access rules for various protocols. Once we creat a security group, we can reuse it for other servers. In our example, we haven’t created any security group yet, so select the check box that says ‘Create a new security group’. If you have already created a group previously then reuse that if it has ssh enabled. We call it ‘MyEC2SecurityGroup’. Click on ‘Review and Launch
Step 7 : Amazon EC2 – Review Instance launch
This step allows you to review your configurations. You will see a message if your security group allows ssh from all machines. For production machines, we generally allow access from a particular IP only. For this tutorial, its ok to ignore that message. click on ‘launch’ to launch your first instance
Step 8 : Amazon EC2 – Key pair
Amazon uses a key pair file to login to the server. When you hit launch, you will be presented with a popup that asks you to use an existing key pair or create a new one. If you have already created a server, you can use the key pair that you created for that server or you can opt to create a new one.
We put in the name “MyKey” and click on Download Key Pair. Save the file somewhere on your machine. Note the path where you stored the key pair. Click on “Launch Instances” once you have downloaded the file. You Should see a message that says that your instances are being created.
If you go back to the EC2 dashboard you should see that the number of EC2 instances has increased.
Step 9 : Amazon EC2 – Login to the server
Now that we have created the server lets see how to login to the server. Open up a terminal window and go to the folder where you have downloaded the key pair. If you use windows, you might have to use putty to ssh. This tutorial assumes Unix. The first thing to do is to change the permission level for the key pair file. We do that using this command
chmod 400 MyKey.pem
To login to the server use this command :
ssh -i "MyKey.pem" ec2-user@<yourserver>
sudo shutdown now
Terminate the instance from the console. Note that when you delete the instance the EBS volume that is used as the root volume is also deleted, however any other volume that you add will not be deleted.
That finishes this tutorial on creating the first EC2 instance. In the next tutorial we look at how to use the Amazon CLI to create instances.