Amazon EC2 (Elastic Compute Cloud)

Amazon EC2 (Elastic Compute Cloud)

·

14 min read

An Elastic Compute Cloud (EC2) is a Virtual Machine in the cloud, you can Run this cloud server whenever you want and can be used when you need to deploy your own Servers in the cloud. You Can Use Amazon ec2 to launch an Instance, configure Security Networking, and Manage storage.

EC2 provides scalable computing capacity in the Amazon Web Services (AWS) cloud, eliminating the need for upfront investment in hardware. It is useful for web hosting and data processing.

✅ Pros:

  1. Elastic Web-Scale Computing: Allows you to quickly scale capacity, up or down, as computing requirements change.

  2. Secure: Amazon EC2 works in conjunction with Amazon VPC to provide security and robust networking functionality.

  3. Flexible: You can choose among multiple instance types, operating systems, and software packages.

❗️ Cons:

  1. The cost can ramp up depending on usage and instance types.

  2. Needs careful management to avoid security risks.

  3. 🎯Use Case: Netflix, a popular streaming service, uses EC2 to scale its infrastructure to handle traffic patterns in real-time, providing an uninterrupted streaming experience to its users.

Uses Cases

- Web Hosting: Host websites and web applications.
- Application Hosting: Run applications, including those with varying resource requirements.
- Data Processing: Process large datasets or run analytics workloads.
- Development and Testing: Create development and testing environments.
- Machine Learning: Train and deploy machine learning models.
- Databases: Run database servers like MySQL or PostgreSQL.
- Content Delivery: Distribute content through Content Delivery Networks (CDNs).
- High-Performance Computing: Perform complex calculations and simulations.
- Game Servers: Host multiplayer games.
- Desktop Virtualization: Offer virtual desktops to users.
- IoT: Process data from IoT devices.
- Batch Processing: Execute batch jobs and data transformations.

What does 2 stand for in EC2?

'2' signifies the two continuous C's such as Compute, and Cloud in EC2 of AWS.

EC2 officially launched in 2006

What are key pairs?
Key pairs are secure login information for your instances/virtual machines. To connect to the instances, we use key pairs that contain a public key and a private key.

  1. Public Key:

    • Stored on AWS EC2 instance.

    • Used for encryption and authentication.

    • Shared openly.

    • Helps verify user identity.

  2. Private Key:

    • Stored securely on the local machine.

    • Used for decryption and access.

    • Should not be shared.

    • Allows SSH access to EC2 instance

  3. Usage:

    • The public key is associated with the EC2 instance.

    • The private key is used to SSH into the instance.

    • Enables secure communication and access.

What are the pricing models for instance?

The different pricing models for EC2 instances are as below,
• On-demand
• Reserved
• Spot
• Scheduled
• Dedicated

✅𝐎𝐧-𝐝𝐞𝐦𝐚𝐧𝐝 I𝐧𝐬𝐭𝐚𝐧𝐜𝐞: It is cheap for a short time but not when taken for a long time.

✅𝐒𝐩𝐨𝐭 I𝐧𝐬𝐭𝐚𝐧𝐜𝐞: Spot Instances are cost-effective EC2 instances that allow you to use spare AWS capacity at a significantly lower price. You bid on the price you are willing to pay for these instances, and when your bid meets or exceeds the current Spot price, your instances run.

✅𝐑𝐞𝐬𝐞𝐫𝐯𝐞𝐝 I𝐧𝐬𝐭𝐚𝐧𝐜𝐞: Reserved instances are the instances in which you can reserve a fixed capacity of EC2 instances. In reserved instances, you will have to get into a contract of 1 year or 3 years.

Scheduled Instance: Scheduled Instances allow you to reserve EC2 capacity to run instances on a recurring schedule, such as daily or weekly. You can specify the start time, end time, instance type, and other parameters for your scheduled instances.

Dedicated Instances is an Internet hosting option in which a physical server is dedicated to a single business customer. The customer has complete control over the machine, so they can optimize it for their unique requirements, including performance and security.

EC2-Types

Amazon Elastic Compute Cloud (Amazon EC2) offers a wide range of instance types optimized to fit different use cases and workloads. These instance types vary in terms of CPU, memory, storage, and network capabilities. Here are some of the common EC2 instance types:

1. General Purpose Instances (T-Series):

  • Use Cases: These instances are suitable for a broad range of workloads, including web servers, development environments, and small to medium databases. They are cost-effective for applications with varying workloads.

  • Instance Types:

    • t2.nano, t2.micro, t2.small, t2.medium, t2.large, t2.xlarge, t2.2xlarge, t3.nano, t3.micro, t3.small, t3.medium, t3.large, t3.xlarge, t3.2xlarge, t4g.nano, t4g.micro, t4g.small, t4g.medium, t4g.large, t4g.xlarge, t4g.2xlarge
  • Key Features: Burstable CPU performance, suitable for workloads with periodic high CPU demands.

2. Compute Optimized Instances (C-Series):

  • Use Cases: These instances are optimized for compute-intensive workloads like high-performance web servers, batch processing, and scientific modeling.

  • Instance Types:

    • c4.large, c4.xlarge, c4.2xlarge, c4.4xlarge, c4.8xlarge, c5.large, c5.xlarge, c5.2xlarge, c5.4xlarge, c5.9xlarge, c5.12xlarge, c5.18xlarge, c5.24xlarge, c6g.nano, c6g.micro, c6g.small, c6g.medium, c6g.large, c6g.xlarge, c6g.2xlarge
  • Key Features: High CPU performance, ideal for compute-bound applications.

3. Memory Optimized Instances (R-Series and X-Series):

  • Use Cases: Memory-optimized instances are best suited for in-memory databases, big data processing, and applications that require substantial RAM.

  • Instance Types:

    • r3.large, r3.xlarge, r3.2xlarge, r3.4xlarge, r3.8xlarge, r4.large, r4.xlarge, r4.2xlarge, r4.4xlarge, r4.8xlarge, r4.16xlarge, r5.large, r5.xlarge, r5.2xlarge, r5.4xlarge, r5.8xlarge, r5.12xlarge, r5.16xlarge, r5.24xlarge, x1.16xlarge, x1.32xlarge, x1e.xlarge, x1e.2xlarge, x1e.4xlarge, x1e.8xlarge, x1e.16xlarge, x1e.32xlarge

4. Storage Optimized Instances (I-Series and D-Series):

  • Use Cases: Storage-optimized instances are designed for applications that require high-speed, low-latency storage, such as NoSQL databases and data warehousing.

  • Instance Types:

    • i3.large, i3.xlarge, i3.2xlarge, i3.4xlarge, i3.8xlarge,i3.16xlarge, i3.metal, d2.xlarge, d2.2xlarge, d2.4xlarge, d2.8xlarge
  • Key Features: High-speed, high-capacity storage for data-intensive applications.

5. Accelerated Computing Instances (P-Series, G-Series, F-Series, and Inf1):

  • Use Cases: These instances are equipped with GPUs or specialized hardware for tasks like machine learning, graphics rendering, and video encoding.

  • Instance Types:

    • p2.xlarge, p2.8xlarge, p2.16xlarge, p3.2xlarge, p3.8xlarge, p3.16xlarge, p4d.24xlarge, g3s.xlarge, g3.4xlarge, g3.8xlarge, g3.16xlarge, g4dn.xlarge, g4dn.2xlarge, g4dn.4xlarge, g4dn.8xlarge, g4dn.12xlarge, g4dn.16xlarge, g4ad.4xlarge, f1.2xlarge, f1.4xlarge, f1.16xlarge, inf1.xlarge, inf1.2xlarge, inf1.6xlarge
  • Key Features: Specialized hardware for tasks that require GPU or FPGA acceleration.

Keywords

- Compute-Optimized: Ideal for CPU-intensive tasks and applications.
- Memory-Optimized: Best suited for memory-intensive workloads.
- Storage-Optimized: Designed for applications requiring high storage capacity and I/O performance.
- Accelerated Computing: Equipped with GPUs, they are used for machine learning, graphics rendering, and scientific simulations.
- General-Purpose: Versatile instances for a variety of workloads.
- Burstable Performance: T2 and T3 instances provide a baseline level of CPU performance with bursts for occasional higher workloads.
- Micro Instances: Small and low-cost instances, suitable for testing and development.

- T2, T3: Burstable performance instances.
- M5: General-purpose instances with balanced resources.
- C5: Compute-optimized instances.
- R5: Memory-optimized instances.
- P3: GPU-accelerated instances for machine learning and high-performance computing.
- I3: Storage-optimized instances.

When does your instance show a retired state?

Retired state is only available in Reserved instances. Once the reserved instance reserving time (1 yr/3 yr) ends it shows the Retired state

How to Address AWS EC2 instances?
Public Domain Name System (DNS) name: When you launch an instance AWS creates a DNS name that can be used to access the
Public IP: A launched instance may also have a public IP address This IP address is assigned from the address reserved by AWS and cannot be specified.
Elastic IP: An elastic IP address (EIP) is a public IP address that you can purchase and use as an independent resource.

What are the types of volumes for EC2 instances?
• There are two types of volumes,
• Instance store volumes
• EBS – Elastic Block Stores

What are EBS volumes?
EBS stands for Elastic Block Stores. They are persistent volumes that you can attach to the instances. With EBS volumes, your data will be preserved even when you stop your instances, unlike your instance store volumes where the data is deleted when you stop the instances. AWS released a feature called multi-attach, which allows EC2 instances to share a single EBS volume for up to 16 instances.

What are Instance store volumes?

An instance store provides temporary block-level storage for your instance. This storage is located on disks that are physically attached to the host computer. Instance store is ideal for temporary storage of information that changes frequently, such as buffers, caches, scratch data, and other temporary content.

What are the types of volumes in EBS?
Following are the types of volumes in EBS,
• General purpose
• Provisioned IOPS
• Magnetic
• Cold HDD
• Throughput optimized

What is an auto-scaling and what are the components?
Auto scaling allows you to automatically scale up and scale down the number of instances depending on the CPU utilization or memory utilization. There are 2 components in Auto scaling, they are Auto-scaling groups and Launch Configuration

What are the benefits of auto-scaling?
• Better fault tolerance
• Better availability
• Better cost management

What is an AMI?

AMI stands for Amazon Machine Image. AMI is a template used to create virtual servers (EC2 instances) in the AWS environment. You must specify an AMI when you launch an instance. You can launch multiple instances from a single AMI when you require multiple instances with the same configuration.

Three AMI Source

  1. Provided by AWS

  2. AWS Marketplace:

  3. Customized AMI-Created by Us

Which are the types of AMIS provided by AWS?
Answer: Instance Store backed, EBS Backed

What is shared AMI?
A shared AMI is an AMI that a developer created and made available for others to use. One of the easiest ways to get started with Amazon EC2 is to use a shared AMI that has the components you need and then add custom content. You can also create your own AMIs and share them with others.

What is an EIP?
An elastic IP address (EIP) is a public IP address that you can purchase and use as an independent resource. When you want to have a static IP address for your instances when you stop and restart your instances, you will be using an EIP address.

Introduction to Security Groups:

• Security Groups are fundamental to network security in AWS. It can be attached to many EC2 Instances. Security groups act as virtual firewalls for your instances, controlling inbound and outbound traffic. They can be associated with instances and control their network access. SGs are used to control access to instances based on protocols, ports, and IP addresses, enhancing security and compliance with network access policies.

Example:

When the user is trying to access the website is called Inbound traffic, When the application/website is trying to access outside the internet is called outbound traffic.

• Security groups only contain rules

• Security group rules can be referenced by IP or by security group

AWS blocks outbound traffic on port 25 (SMTP) of all EC2 instances and Lambda functions. To send outbound traffic on port 25, request that AWS remove the restriction

When I try to launch an ec2 instance I get the Service limit exceeded, how to fix the issue?
AWS offers a service limit of 20 running instances per region, to fix the issue we need to contact AWS support to increase the limit based on the requirement

What are the target groups for AWS?

Target groups route requests to individual registered targets, such as EC2 instances, using the protocol and port number that you specify

You have an EC2 instance that has an unencrypted volume. Do you want to create another Encrypted volume from this unencrypted volume? Which of the following steps can achieve this?

Create a snapshot of the unencrypted volume (applying encryption parameters), copy the Snapshot, and create a volume from the copied snapshot.

What is Status Checks in AWS Ec2?

System Status checks will look into problems with an instance that needs AWS help to resolve the issue. When we see a system status check failure, you can wait for AWS to resolve the issue, or do it by yourself.
• Network connectivity
• System power
• Software issues Data Centre’s
• Hardware issues
• Instance Status Checks – Instance Status checks will look into issues that need our involvement to fix the issue. if the status check fails, we can reboot that particular instance.
• Failed system status checks
• Memory Full
• Corrupted file system
• Kernel issues

How EC2 Instances can be resized?
EC2 instances can be resizable (scale up or scale down) based on requirement

What are the steps to mount EBS?

The process to mount EBS to EC2 instance
• Df –k
• mkfs.ext4 /dev/xvdf
• Fdisk –l
• Mkdir /my5gbdata
• Mount /dev/xvdf /my5gbdata

Maximum number of EC2 which can be created in VPC?
A maximum of 20 instances can be created in a VPC. we can create 20 reserve instances and request spot instances as per demand.

How can you back up your EC2 instances?

You can create snapshots of EBS volumes, which serve as backups. These snapshots can be used to create new EBS volumes or restore existing ones.

What is a Placement Group?

Placement groups help us to launch a bunch of EC2 instances close to each other physically within the same AZ. Being close physically and within the same AZ helps it take advantage of high-speed connectivity to provide low latency and high throughput access.

What is AWS Elastic Network Interface (ENI)?

• Amazon Elastic Network Interface (ENI) is a fundamental building block of networking within AWS that is attached to EC2 instances. ENIs provide a virtual network interface to your instances in a Virtual Private Cloud (VPC)

• ENI allows to have public facing web servers, web applications, and database servers in the public subnet and locks down SSH access to private management subnet. Using VPN users can connect to a private management subnet and can access it as usual.

What are the cloudwatch metrics that are available for EC2 instances?

Diskreads, Diskwrites, CPU utilization, networkpacketsIn, networkpacketsOut, networkIn, networkOut, CPUCreditUsage, CPUCreditBalance.

How do you safeguard your EC2 instances running in a VPC?

Ans: Security Groups can be used to protect your EC2 instances in a VPC. We can configure both INBOUND and OUTBOUND traffic in a Security Group which enables secured access to your EC2 instances. Security Group automatically denies any unauthorized access to your EC2 instances.

Can an EC2 instance inside your VPC connect with the EC2 instance belonging to other VPCs?

Yes, Possible. Provided an Internet Gateway is configured in such a way that traffic is bounded for EC2 instances running in other VPCs.

Some Important Questions!

How does Amazon EC2 work?

Amazon EC2 enables users to launch instances based on pre-configured Amazon Machine Images (AMIs). These instances run within virtual private clouds (VPCs) and can be configured with various resources like CPU, memory, storage, and networking.

How can you improve the availability of EC2 instances?

To improve availability, you can place instances in multiple Availability Zones (AZs) within a region. This helps ensure redundancy and fault tolerance.

How can you secure your EC2 instances?

You can enhance the security of EC2 instances by using security groups, Network ACLs, key pairs, and configuring firewalls. Additionally, implementing multi-factor authentication (MFA) is recommended for account access.

Explain the difference between public IP and Elastic IP in EC2.

A public IP is assigned to an instance at launch, but it can change if the instance is stopped and started. An Elastic IP is a static IP address that can be associated with an instance, providing a consistent public IP even after stopping and starting the instance.

How can you scale your application using EC2?

You can scale your application horizontally by adding more instances. Amazon EC2 Auto Scaling helps you automatically adjust the number of instances based on demand.

What is Amazon EBS?

Amazon Elastic Block Store (EBS) provides persistent block storage volumes for EC2 instances. EBS volumes can be attached to instances and used as data storage.

How can you encrypt data on EBS volumes?

You can encrypt EBS volumes using Amazon EBS encryption. You can choose to create encrypted volumes during instance launch or encrypt existing unencrypted volumes.

What is the difference between instance store and EBS-backed instances?

Instance store instances use ephemeral storage that is directly attached to the instance, providing high I/O performance. EBS-backed instances use EBS volumes for storage, offering persistent data storage.

What are instance metadata and user data in EC2?

Instance metadata provides information about an instance, such as its IP address, instance type, and IAM role. User data is information that you can pass to an instance during launch to customize its behavior.

How can you launch instances in a Virtual Private Cloud (VPC)?

When launching instances, you can choose a specific VPC and subnet. This ensures that the instances are launched within the defined network environment.

How can you automate the deployment of EC2 instances?

You can use AWS CloudFormation to create and manage a collection of related AWS resources, including EC2 instances. This allows you to define the infrastructure as code.

How can you achieve high availability for an application using EC2?

You can use features like Amazon EC2 Auto Scaling and Elastic Load Balancing to distribute incoming traffic and automatically adjust the number of instances to handle changes in demand.

What is Amazon Machine Learning (Amazon ML)?

Amazon ML is a service that enables you to build predictive models using machine learning technology. It's used to perform predictions on data and make informed decisions.

What is Amazon EC2 Instance Connect?

Amazon EC2 Instance Connect provides a simple and secure way to connect to your instances using Secure Shell (SSH). It eliminates the need to use key pairs and allows you to connect using your AWS Management Console credentials.