DEV Community

Cover image for AWS - EC2
Gokul Kannan
Gokul Kannan

Posted on

AWS - EC2

Assume we have multiple applications and we need to deploy those in a server so that those applications can be accessed by people from anywhere through internet. Now lets deploy all those in a single server. By doing this, we make all those applications to share all the resources among them.
The resources here means the RAM, storage and the underlying Operating System.

Although there are drawbacks like there is no isolation for the applications, still all the applications can run individually by using the shared resources. Now, lets say there is a lot of requests coming into one of the application and it starts using most of the shared resources. So, if one of the application takes lot of the resources for its process then that impacts the performance of all the other applications.

This is a major drawback in deploying all the applications into a single server. So what are the other options to overcome this problem. Lets have individual servers for each applications, so there won't be any such shared usage. This would definitely solve our problem but at what cost? The procurement of physical servers and maintenance costs would be very high making this option as not a viable one.

To resolve this issue, there came the concept of Virtual Machines (VMs).

What is a Virtual Machine?

In a Virtual Machine, each application runs on its own operating system while sharing a single physical machine (Bare Metal Machine). Here this machine would be segregated as separate virtual (imaginary) machines with its own OS. And the underlying RAM in that physical machine would be split among these virtual machines.
Now these different OS will be called as Image. It means the item that is used to run the OS or known as the .iso file.

Benefits of Virtual Machines

  • Multiple operating systems can run on a single physical machine..
  • Better isolation between applications.
  • Lower hardware and maintenance cost

EC2

In AWS, one of the service is there to create these VMs and use it for various purposes like static website hosting, deploying applications, running DB servers or for a batch processing. This service is called as EC2 (Elastic Compute Cloud). Similarly all other cloud providers have this service like in Azure VM, Digital Ocean-Droplets. Even though the terminologies differs, the underlying concept remains the same.

Why EC2?

Complete Control : You can install any software, operating system (Linux, Windows, macOS), or application stack you need.

Elasticity : Based on your usage, it can scale automatically to handle peak traffic and shrink them when demand drops, avoiding wasted resources.

Customizations : Based on your requirement you can customize the VM instance which would be a best fit for your workload.

Pricing Model

On-Demand: When you just need it for a short period, you can opt for this model. If there is any use case like for testing, where you need it for a limited time, then you can use this model.

Reserved Instances: When you need a VM for many years, then you can opt for this and you would receive significant discounts (up to 72%) compared to On-Demand pricing in exchange. Good option for production use cases.

Spot Instances: Here you can take advantage of unused AWS compute capacity at great discounts (up to 90%). (for Video Rendering AI model training). AWS can take it down at anytime with prior notification.

Savings Plan : This is like a mutual agreement where I am going to use the AWS services, so you can ask for concession to get an EC2 instance. This is more flexible than reserved instance.

Categories of EC2

You can configure the machine based on amount of CPU and RAM you needed.

  • Based on RAM - Eg: t3.micro, t3.small
  • CPU based - Eg : c6i.large
  • Memory Optimized - Eg: r6i.large

Now you got your EC2, which is a bare metal machine with RAM, CPU. But how do you consume this. So, here we need AMI.

AMI - Amazon Machine Image - Image means OS

We need an OS for our Virtual Machine. EC2 supports two different sets of OS.

  • Public AMI - Ubuntu, windows, mac, linuxmint, Amazon OS.
  • Private AMI - May be an organization would have a proprietary OS. Even this is supported by AWS.

Accessing EC2 in AWS

You can access the EC2 through your console and launch it using the "Launch Instance" option.

When you set up an EC2 instance, you have to configure different things as followed:

Name and Tags: The Name and tags are just to identify the VM based on its purpose and this tags would be helpful in managing many resources under your AWS account. Its just a key-value pair along with a resource type which is based on the purpose

Name and Tags for EC2

Amazon Machine Image (AMI): This is like a template that contains the operating system, initial software, and root configurations needed to launch the server.

AMI

Instance Types: The specific hardware configuration (CPU, RAM, storage, networking capacity).

Instance Types

Key Pairs: Secure cryptographic credentials used to securely log into your Linux or macOS instances (typically using SSH). Here AWS generates a public key and a private key. The private key is given to you as a .pem file once this key pair is generated and while launching the server(VM), the public key gets stored over there by AWS. Only with that private key, you would be able to communicate with the server through SSH.

You can imagine the public key is the lock for your house and the private key is the key for that lock.
Now only with that key you can open your lock. This way we are making sure only with the valid key, you would be able to enter your house(VM). This way of secure connection is possible using the openssh tool.
When we have this tool in your machine and another server(machine), you can create this key pairs and put the public key into that server's allowed list or authorized keys and now you can connect to that server using its IP and this private key.

Key Pairs

Key Pairs Creation

Security Groups: A virtual firewall that controls what inbound and outbound network traffic is permitted to and from your instance.

Security Groups

VPC

For any services that you are running in AWS, there is one default item called as VPC gets created. It means Virtual Private Cloud. You can imagine/consider this VPC like a room and lets say now you are bringing an EC2(Computer) machine into this. And this room is having an Internet connection. Now you have to pay the rent for both the room and the EC2 machine.

VPC explanation

Once you have selected all the configurations, you can launch your instance. You can click on that "Connect" and using the SSH client, follow those commands to connect to you server.

EC2

EC2

Once you connected, you can try running a webserver and host a simple application and try accessing through your browser.

If its an ubuntu server, try running the following command before installing anything like web servers or any other packages.

"sudo apt update"

You can try installing a web server like nginx. To install run the below command :

sudo apt install nginx

Start the Web Server :

sudo systemctl start nginx

You'll see something like :
"Active: active (running)"

Now you should be able to see the nginx webserver's default page in browser when you try to hit the public IP of your EC2 instance, which you could get it from the AWS console. Here in browser you are trying to access the server using http.
So, if you are not able to see the nginx webserver's default page, it means that http port is not enabled in your security group rules. You can enable it by getting into your security group and click on "Edit inbound Rules". And add the rule for HTTP.

Security Group

Add Rule

Now if you access the EC2's public IP through browser, it connects to this port 80 configured in the security group and shows the default nginx server's web page.

Nginx Web Server

You can also, create a simple application and make it listen to any port (Eg: port 8000) and we can make this nginx server as a reverse proxy so that when the browser sends a request to Nginx on port 80, it forwards the request internally to the port 8000 where your application is running and it loads application's page in the browser.

We have seen how to create a AWS EC2 and tried running a webserver on it. Now you must have a clear foundational understanding of how to create EC2, connecting to it through SSH and running a webserver on it.

One additional info- once you have created a new AWS account, you can see list of activities to do and on completing each, you could earn additional AWS credits. Here you can start with "Launching an instance using EC2".

Activities to earn AWS credits

Completion of one Activity

Top comments (0)