EC2 - Elastic Compute Cloud
Learn what Amazon EC2 is, how it works, and how to get started with your own virtual servers in the cloud.
Table of contents
Introduction
Amazon EC2 (Elastic Compute Cloud) is a service from AWS that lets you create and run virtual servers, making it easy to host applications without needing to buy physical hardware. With EC2, you can quickly scale your computing power up or down to handle changing needs and traffic spikes, and you only pay for what you use. This flexibility simplifies web hosting and helps you manage resources efficiently.
What happens when you request a virtual server from AWS ?
AWS has its physical servers all around the world. It sends your request to its hypervisor, which then creates a virtual partition and grants you access to it.
AWS assigns an IP address to your instance. This can be a public IP if you want it accessible from the internet or a private IP for internal communication within the VPC.
You can access the instance using SSH (for Linux) or RDP (for Windows) based on the credentials and keys you provided.
AWS calculates the cost based on the instance type, usage hours, and any additional services or storage you use. Billing is typically on an hourly or per-second basis, depending on the instance type.
What You Get with the EC2 Free Tier
- 750 Hours of t2.micro or t3.micro Instances per Month
What does 750 hours per month mean?
There are about 730 hours in a 30-day month (24 hours/day × 30 days).
So, 750 hours means you can run a single instance continuously for the entire month (since 750 hours covers the whole month).
or You can also split the 750 hours across multiple instances. For example:
1 instance running for the whole month = 750 hours
2 instances running for half the month = 2 × 375 hours = 750 hours
3 instances running for 10 days = 3 × 240 hours = 720 hours (which is under 750, so it’s free)
Types of instances
we have variety of instance types, each optimized for different use cases.
General Purpose Instances
Use cases : Applications that require a balance of compute, memory, and networking resources. They are ideal for web servers, small to medium-sized databases, and development environments.
example : t2, t3
Compute Optimized Instances
compute-bound applications that benefit from high performance processors.
Ideal for tasks such as batch processing, high-performance web servers, and scientific modeling.
example : c5, c7
Memory Optimized Instances
Designed for workloads that require a lot of memory. They are suitable for high-performance databases, in-memory caches, and big data applications.
example : r5, r6
Storage Optimized Instances
Offer high storage throughput
ideal for applications that requirelow-latency access to storage. Suitable for NoSQL databases, data warehousing, and distributed file systems.
Accelerated Computing Instances
Uses GPUs or other accelerators to provide high-performance computing for tasks such as machine learning, data analysis, and graphics-intensive applications.
used for 3D encoding
High Performance Computing (HPC) Instances
- optimized for high-performance computing applications that require high throughput and low-latency networking. I
Instance sizes - based on Traffic size
Micro - low traffic websites
Small - low to moderate
Medium - small database, medium traffic , microservices
Large - medium database , large traffic
Xlarger - machine learning
2xlarge - large scale application , big data processing
States of EC2 Instance
Start
Stop
Reboot
Hibernate
Terminate
Difference between stop and hibernate
stop | Hibernate |
similar to shutdown Pc | sleep mode |
The data on EBS volumes attached to the instance is preserved. However, any data stored in instance is lost | Hibernation saves the contents of the instance’s RAM to an EBS volume before shutting down. |
When you restart a stopped instance, it undergoes a full boot process. This means it will get a new IP address unless it has an Elastic IP assigned. | On restart, the instance resumes from where it slept. |
use case : when you want to shut down an instance temporarily and you don’t need to preserve the instance’s in-memory state. It’s typically used for cost savings when an instance is not in use. | use case : when you need to preserve the in-memory state of your instance for quicker restart and recovery, |
AMI - Amazon Machine Image
An Amazon Machine Image (AMI) serves as the foundation for launching instances within AWS EC2. It is a pre-configured template that contains the necessary information to start an instance, including the operating system, software packages, configurations, and even data.
When launching an instance, you must select an appropriate AMI that aligns with your desired configuration and requirements. AWS offers a wide range of AMIs, including various operating systems
Ex : Ubuntu AMI, Amazon linux 2 AMI
Components of AMI:
OS
Root volume (EBS) - default comes with 8gb
Application/ database servers
Software / libraries
Key Pair
The key pair is set of cryptographic keys used for securely accessing and managing EC2 instances.
used to authenticate when connecting to the instance. (ssh connection)
When creating an instance, either generate a new key pair or select an existing one. The private key will be downloaded to your local system, while the public key will be saved on the instance.
Change the permissions of the downloaded private key file on your system.
chmod 600 key.pem
Network Settings / Security Groups
VPC (Virtual Private Cloud): Select the VPC where you want the instance to reside. A VPC is a logically isolated network within AWS.
Subnet: Choose the subnet within the VPC where the instance will be launched. (Public or Private Subnets) this allows you to group resources based on security and operational needs.
Auto-assign Public IP
Automatically assigns a dynamic public IP address to your instance when it is launched in a public subnet.
Nature: The public IP is temporary and can change if the instance is stopped and started again. It is managed by AWS and is not persistent beyond the lifecycle of the instance.
Security Groups: Security groups act as virtual firewalls to control inbound and outbound traffic. You can either create a new security group or select an existing one.
IAM Role: Optionally assign an IAM (Identity and Access Management) role to the instance. This allows the instance to access AWS services based on the permissions defined in the IAM role.
Private DNS Hostname: Optionally configure whether the instance should receive a private DNS hostname. This is useful for instances that need to communicate with each other within a VPC using DNS.
Practical demo
Click on "Create Instance".
Select an AMI (Amazon Machine Image), for example, Ubuntu.
Create a new key pair or select an existing one.
Configure Network settings : choose VPC, subnet, and security groups.
Change the permissions of the key pair file on your local systemClick on Create instance
chmod 600 key.pem
Connect to your instance via SSH
ssh -i key.pem ubuntu@ip_address