Blog

3 Jan 2020

How to Create an EC2 Instance on AWS?

What is compute?

Compute means basically calculation. And a system that does calculation or processing is we call as a computing machine or Computer.

Compute machines may be a small IoT device like Amazon Alexa or google home or maybe a smartphone or laptop or a blade server. We select the proper compute machine as per use case. Like anybody doesn’t bye a blade server for regular day to day tasks. Or you can’t use your laptop for serving high workload like web hosting. Every use case has a separate type of compute machine.

In the same way, whenever you want to do computation on the cloud, let’s say AWS you also have many choices. And the popular options are like

  1. EC2 for the regular virtual machines.
  2. ECS for Docker workloads
  3. EKS for Kubernetes workload.
  4. Elastic beanstalk for hosting a website without worrying about Operating System.
  5. AWS Lambda for executing a single program.

In this blog, I am going to talk explain EC2 i.e. elastic compute cloud service of AWS.

What is EC2?

EC2 or Elastic compute cloud an AWS service used to create regular virtual machines as we create on or desktop machines using VMware or VirtualBox or any other application.

Lets first understand what is actually a virtual machine.

The virtualization technology was invented for improving utilization of hardware. For using single hardware for installing multiple operating systems and using them at a time. Virtual machine are like over regular Windows or Linux systems but just difference is that they are using virtual hardware instead of physical hardware.

The architecture of a virtual machine is like

How to Create an EC2 Instance on AWS? 1

A hypervisor is a virtualization software like VMware or virtual box. If the production system is using virtualization technology then, in that case, they are not having an Operating system over hardware.  In that case, the architecture will look like this.

How to Create an EC2 Instance on AWS? 2

This is called a Type 1 hypervisor where hypervisor providing basic functionality of the operating system itself.

AWS is using XEN and KVM hypervisor for EC2 service.   

For creating a virtual machine what component you will need.

  1. Virtual CPU (VCPU)
  2. Virtual RAM
  3. Storage
  4. Operating System Image.

All components are provided by AWS EC2 service.

How to Create an EC2 Instance on AWS?

Let’s create your first EC2 instance for a website hosting.

Login to your AWS console on https://aws.amazon.com/. If you don’t have an account then you can signup for a free account and for that you will need a credit card for verification. I am assuming that you have AWS account.

Follow the steps for launching the EC2 instance.

  1. Click on services and search for EC2How to Create an EC2 Instance on AWS? 3 
  2. Now you are on EC2 dashboard which will look likeHow to Create an EC2 Instance on AWS? 4
  3. Click on Running instances or select Instances from the left side menu.How to Create an EC2 Instance on AWS? 5
  4. Click on Launch Instance
  5. Now you are on AMI (Amazon Machine Images) page where you can select the operating system for your instance(virtual machine).
  6. We are going to use Linux o/s for web hosting.
  7. So select first AMI which isAmazon Linux 2 AMI (HVM), SSD Volume Type
  8. After Clicking on select you are on page where you can select hardware capacity for your virtual machine.
  9. Select t2.micro that is freely available on trial account.
  10. Select Next: Configure Instance Details.
  11. So now you are on page where you can configure Virtual machines like its network, ip and other things.
  12. For now, scroll down to the end of the page And click on Advance detail. You will get to see a small section of Userdata. Which works like post-installation. Here we are going to add a bash script for HTTP package installation and service activation. Paste the following script under user data.#!/bin/bashyum update -yyum install httpd -ycd /var/www/htmlecho “<h1> Welcome to my first web server </h1>” > index.htmlsystemctl start httpdsystemctl enable httpd
  13. Click on Next: Add Storage.
  14. By default, it takes 8Gb of storage for Linux machines and 30 Gb for windows machines.
  15. 8 Gb is enough  for us so just click
  16. Next: Add Tags.
  17. Add the following tags
    • I
      • Key – Name 
      • Value – WebVM
    • II
      • Key – Role
      • Value – Webserver
        Tags are key-value pairs used for the identification of resources or grouping of resources with the unique keys.
  18. If it is done then just click on Next: Configure Security group.
  19. A security group is like a firewall where all inbound traffic is by default all denied. And we have to open the following port.
    1. ssh for remote access
    2. ICMP for ping test
    3. HTTP for web access.
  20. Give a name for the security group “WebSG”
  21. Click on Add rule and add these three rules 
    1. SSH
    2. HTTP
    3. ALL ICMP IPV4
  22. Make source anywhere for all rulesHow to Create an EC2 Instance on AWS? 6
  23. Click on Review and Launch.   
  24. And then Launch.
  25. You will get a popup like this from the drop-down menu select “ Create a new key pair” Provide a name for the key and click on Download Key and the  Launch Instance.
  26. And congrats you successfully launch your first instance on AWS.
  27. Scroll down and click on View Instances.
  28. Wait for some time for changing Instance State to Available and status check to 2/2
  29. Copy the public IP from the description and paste to the new page of the browser you will get your webpage running.How to Create an EC2 Instance on AWS? 7
  30. The next task is Connect to Linux instance.

Connect to Linux instance using Mac/Linux

  1. Open terminal
  2. Navigate to the directory where the key is downloaded.
  3. Change the permission to 400
    Chmod 400 webvm.pemWhere webvm.pem is a key name.
  4. Use this command for remote access
  5. It will ask you for fingerprint first-time press yes
  6. And you are in.How to Create an EC2 Instance on AWS? 8

For windows users, it is a little bit tricky.

Because popular application putty maybe you are familiar with does not support .pem key format.

So the first task will be converted .pem key to .ppm key.

Follow the steps.

  1. Download putty.
  2. Open Putty-gen
  3. Click on the file and load private key
    Change the file type to all files from the right side bottom corner.
    Select URL and click on Open.How to Create an EC2 Instance on AWS? 9
  4. Click on Save Private key.How to Create an EC2 Instance on AWS? 10
  5. Click Yes for saving the file without a passphrase.
  6. And close putty gen and open putty.How to Create an EC2 Instance on AWS? 11
  7. On the left side under connection select + sign of SSH.
  8. And then select authHow to Create an EC2 Instance on AWS? 12
  9. Click on Browse and select the key.
  10. Again click on session from the left side menu
  11. Enter the IP address and open the session.