# AWS Elastic Cloud Compute : EC2

## Introduction

Amazon Elastic Compute Cloud(EC2) is a virtual server that can be used to deploy applications. It is a highly available, scalable and configurable service that can support all kinds of needs based on CPU, Memory, and storage requirements. With EC2, users have access to a vast selection of instance types, operating systems, and software configurations, enabling them to tailor their virtual servers to meet the unique demands of their applications. Additionally, EC2 offers features such as load balancing, auto-scaling, and comprehensive security controls to enhance the reliability, availability, and security of applications deployed in the cloud. 

Let's dive into some of the basic concepts of EC2.

## Basic concepts

### AMI 

Amazon Machine Image is a template that contains a software configuration (for example, an operating system, an application server, and applications). One can launch instances of EC2 based on this image. 

### Instance types 

Instance type means the type of EC2 instance launched. This determines the hardware of the virtual server in terms of CPU, Memory, Storage

![image source : https://www.veritis.com/blog/choosing-the-right-aws-ec2-instance-type-for-your-application/](https://cdn.hashnode.com/res/hashnode/image/upload/v1696688575673/4bee9f31-09dc-4985-baed-2a464254ef08.png align="center")

* **General Purpose Instances (e.g., t2, m5):** These instances are well-balanced and are suitable for a wide range of applications. They provide a good balance of CPU, memory, and network performance. T2 instances are burstable, allowing for temporary spikes in CPU performance. 
    
* **Compute-Optimized Instances (e.g., c5):** These instances are designed for compute-intensive workloads that require high CPU performance. They are excellent for tasks like batch processing, scientific simulations, and data analysis. 
    
* **Memory-Optimized Instances (e.g., r5):** These instances are optimized for memory-intensive applications such as large databases, in-memory caching, and big data analytics. They offer a high ratio of RAM to CPU. 
    
* **Storage-Optimized Instances (e.g., i3, d3):** Storage-optimized instances are geared towards applications that require high-speed, low-latency storage. They are ideal for database servers, data warehousing, and distributed file systems. 
    

We have used T2.micro instance below which is a general-purpose instance. 

### Purchasing options for EC2 

AWS provides multiple options based on which an instance is billed. 

* **On-demand Instances**. : Pay by the second. You pay for the time you keep your instances in a `running` state. The price is fixed.
    
* **Reserved Instances**: Buying an instance for a term of 1 or 3 years gives a discounted value for the instance. Use this instance when you have long term requirements.
    
* **Spot Instances**: these are the instances that are made available to you from spare EC2 instances. This makes their cost significantly lower. The rate is decided on an hourly basis depending on the supply and demand of EC2 instances in the Availability zone. It's a good choice if you can be flexible about when your applications run and if your applications can be interrupted. For example, Spot Instances are well-suited for data analysis, batch jobs, background processing, 
    
* **Dedicated hosts**:  a physical server with EC2 instance capacity fully dedicated to the customer's use. It allows you to use your existing per-socket, per-core, or per-VM software licenses, including Windows Server, Microsoft SQL Server etc 
    
* **On-demand capacity Reservations**: enable you to reserve compute capacity for your Amazon EC2 instances in a specific Availability Zone for any duration. Capacity Reservations mitigate against the risk of being unable to get On-Demand capacity in case there are capacity constraints. 
    

### Networking with EC2 

EC2 instances are launched in a subnet of Virtual Private Cloud, that is configured while creating the instance. AWS assigns a private network interface that acts as a virtual card.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696688854460/148fa31d-a684-431c-b94f-3d212ac44e36.png align="center")

* While launching an instance you need to specify the Region and Availability zone, where your instance would be launched.
    
* Instances receive a private IP address from the pool of IP addresses of subnet and are assigned to this primary network interface. These are not reachable from the Internet.
    
* A public IP address needs to be configured if you want to assign it one. These are reachable from the internet. When you launch an instance in a default VPC, AWS assigns it a public IP address by default. The public IP address is assigned from Amazon's pool of public IP addresses and released back when the instance is not in a running state.
    
* If you require a persistent public IP address that can be associated with instances, use an Elastic IP address instead. 
    
* Elastic Network Interface: can include primary, and secondary private IP addresses, one or more public IP addresses, and security groups. You can create and configure network interfaces and attach them to instances in the same Availability Zone. You can attach it to an instance, detach it from an instance, and attach it to another instance
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696688386096/953c596b-d44d-4be7-b49a-3b6b1f6ccc24.png align="center")
    
* Network bandwidth: Instance bandwidth specifications apply to both inbound and outbound traffic for the instance. For example, if an instance specifies up to 10 Gbps of bandwidth, that means it has up to 10 Gbps of bandwidth for inbound traffic and up to 10 Gbps for outbound traffic. The available network bandwidth of an instance depends on the number of vCPUs that it has .
    

### Security 

Security of EC2 instances is of utmost importance because it runs the user's critical application and holds data. Following are some of the key aspects of ensuring security 

* Network Isolation is ensured by VPC 
    
* Ensure resiliency by deploying instances across multiple physically separated and isolated availability zones. Availability Zones are more highly available, fault-tolerant, and scalable than traditional single or multiple data center infrastructures. 
    
* Set up IAM users and give them access to resources they need as per the role. 
    
* Data protection is both at rest and in transit. 
    
* Key-pairs: A key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance. Amazon EC2 stores the public key on your instance, and you store the private key.  
    
* Security groups: A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. 
    

### Storage 

Amazon provides multiple options of storage for your EC2 instance. These vary depending on the performance and durability. The major options available are

![Source: AWS](https://cdn.hashnode.com/res/hashnode/image/upload/v1696688208143/925fda39-ef15-4036-a693-0044bd6cb5d5.png align="center")

* **Elastic Block Store**: Amazon EBS provides durable, block-level storage volumes that you can attach to a running instance. You can use Amazon EBS as a primary storage device for data that requires frequent and granular updates. For example, Amazon EBS is the recommended storage option when you run a database on an instance. The EBS volume persists independent of the EC2 running status. After an EBS volume is attached to an instance, you can use it like any other physical hard drive. Multiple volumes can be attached to an instance. To keep a backup copy of your data, you can create a snapshot of an EBS volume, which is stored in Amazon S3. 
    
* **EC2 Instance Store**: Many instances can access storage from disks that are physically attached to the host computer. This disk storage is referred to as an instance store. Instance store provides temporary block-level storage for instances. The data on an instance store volume persists only during the life of the associated instance.
    
* **EFS**: You can create an EFS file system and configure your instances to mount the file system. You can use an EFS file system as a common data source for workloads and applications running on multiple instances.
    

Now we will be launching the EC2 instance and then connecting from the local system(Mac in my case).

## Launching EC2

Launching an EC2 instance requires the following steps:

Setting up EC2 

1. First of all, create a key-pair by going to the left navigation. 
    
    * If you skipped this, you will not be able to connect from the local system 
        
    * I have used .pem file for my private key and will be using SSH from Mac to connect to the EC2 instance 
        
2. Create a security group to create a firewall that defines what all traffic is allowed and what is not 
    
    * Inbound traffic 
        
        * HTTPS protocol: All traffic 
            
        * SSH: My personal IP for connecting with EC2 
            
    * Outbound traffic: All for HTTPS
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696687177114/66ffb97d-61d5-46c6-82dd-0d107e85657c.png align="center")
        
3. Launch instance. Specify the following 
    
    * Name of the instance 
        
    * OS 
        
    * AMI type 
        
    * Instance type 
        
    * A security group that was created above.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696687305417/0b6d5d3e-e621-40bb-9963-943bcb10b289.png align="center")
        

You should see your instance up and running

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696687444241/d9948362-fdbc-44ca-911d-d5915fcfb182.png align="center")

## Connecting to EC2

Connecting to the EC2 instance would require the following steps. Make sure you have SSH installed. Try running `ssh` command in the terminal. You should see something like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696687703955/66b68c8c-f17d-48e7-8aa9-bc1c737b1bf4.png align="center")

1. Info needed about EC2 
    
    * Instance Id  
        
    * public DNS name   
        
    * Default username of the AMI: ec2-user  
        
2. Locate the private key and set the permission 
    
    1. Get a fully qualified path of .pem file 
        
    2. Change permissions on the file so that only you can read it(for Macos, Linux) 
        
        `chmod 400 key-pair-name.pem` . If you fail to do so you will see the following error
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696688090363/b6a705b0-f46a-4397-9963-56fac481f6ae.png align="left")
        
    3. For windows: Open File Explorer and right-click on the .pem file. Select Properties &gt; Security tab and choose Advanced. Choose Disable inheritance. Remove access to all users except for the current user. 
        
3. Run the command to connect to your instance: 
    
    `ssh -i <keyname_path.pem> <user_name>@<public ipv4 Dns name>`
    
4. Run the `exit` command to end the connection 
    

You should see the connection set up successfully

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696687851532/e5399350-c217-477a-a05e-26bb8d1df57b.png align="center")

Now you can start transferring files to your EC2 instance.

## Conclusion

EC2 is one of the basic building blocks of any architecture in modern applications. Its highly scalable, available and resilient nature makes it extremely powerful. Gone are the days when you would spend time and resources to buy and set up your physical servers to host your applications. EC2 ensures you have the infrastructure available all the time irrespective of the ebbs and flows of traffic. It doesn't matter if you are a student, startup or scaled enterprise, EC2 has a solution to cater to your needs!!

*I share content related to software development and scalable application systems regularly on* [***Twitter***](https://twitter.com/_rahulpoonia) *as well. Do reach out to me!!*

Image source: [https://www.veritis.com/blog/choosing-the-right-aws-ec2-instance-type-for-your-application/](https://www.veritis.com/blog/choosing-the-right-aws-ec2-instance-type-for-your-application/)
