Amazon EC2 Basics: A Beginner's Guide (Part 1)

Amazon Elastic Compute Cloud (EC2) is the backbone of AWS's cloud computing offerings. It allows users to rent virtual machines (called EC2 instances) and run applications without managing physical servers. In this detailed guide, we’ll break down the essential EC2 concepts so that you can comfortably start working with EC2, even as a beginner. By the end, you’ll have a hands-on understanding with step-by-step labs included.

1. What is Amazon EC2?

Amazon EC2 (Elastic Compute Cloud) is a web service that provides scalable computing capacity in the cloud. You can think of EC2 as a way to rent virtual computers that run on AWS's infrastructure. These virtual computers (or instances) allow you to deploy and manage applications in a secure and scalable manner.

Benefits of EC2:

  • Elasticity: Automatically scale your resources up or down based on demand, optimizing costs.

  • Cost-Effective: Choose from various pricing models, such as On-Demand, Spot, and Reserved instances.

  • Customizable: You have full control over the instance's OS, networking, and security settings.

  • Global Reach: EC2 instances can be deployed across different regions and Availability Zones for high availability and fault tolerance.

2. EC2 Instance Types

EC2 instances come in various types optimized for different use cases. AWS provides instance families that are tailored for general-purpose, compute-intensive, memory-intensive, and storage-heavy tasks.

Instance Type Families:

  1. General Purpose Instances
    Examples: T3, T3a, T2, M5, M6g
    General-purpose instances offer a balance of compute, memory, and networking resources, making them a good fit for a variety of workloads like web servers, small databases, and development environments.

  2. Compute Optimized Instances
    Examples: C5, C6g
    These instances are designed for compute-heavy tasks that require high processing power, such as batch processing, scientific modeling, and high-performance web servers.

  3. Memory Optimized Instances
    Examples: R5, R6g, X1e
    These instances are ideal for memory-intensive applications, like large databases (e.g., in-memory databases like Redis or SAP HANA) and big data analytics workloads.

  4. Storage Optimized Instances
    Examples: I3, D2
    Storage optimized instances are designed for workloads that require high, sequential read and write access to large datasets, such as NoSQL databases and data warehousing.

How to Choose the Right Instance Type:

Selecting an instance type depends on the specific needs of your application. If you’re not sure which one to use, start with a T3 (General Purpose) instance, which is part of the free tier for new AWS users.


3. EC2 User Data

EC2 instances allow you to provide a startup script (known as User Data) that will execute automatically when the instance launches. This script can install software, update the operating system, or configure settings.

Why Use User Data?

  • Automation: You can automate repetitive tasks such as installing a web server or configuring application settings.

  • Simplicity: User data helps configure instances with minimal manual intervention.

  • Custom Startup: Customize your EC2 instances at the time of launch without logging into them manually.

Example of User Data Script:

Here’s a simple example that automatically installs the Apache web server on an Amazon Linux instance:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd

This script updates the system, installs the Apache web server, and starts it automatically. You can provide this script while launching the instance, and it will execute as soon as the instance boots up.

Lab: Launching an EC2 Instance with User Data

Let’s go through the process of launching an EC2 instance using User Data:

  1. Step 1: Launch Instance

    • In the AWS Management Console, go to EC2 Dashboard and click Launch Instance.

    • Select Amazon Linux 2 AMI (free-tier eligible).

  2. Step 2: Choose Instance Type

    • Choose t2.micro (free-tier eligible). This is ideal for beginners since it’s free to use under AWS's free tier.
  3. Step 3: Configure Instance

    • Under Advanced Details, find the User Data field.

    • Paste the above User Data script that installs Apache.

  4. Step 4: Add Storage

    • Keep the default 8 GB SSD.
  5. Step 5: Configure Security Group

    • Create a new security group, allowing HTTP (Port 80) and SSH (Port 22).
  6. Step 6: Review and Launch

    • Review the details and launch the instance. Don’t forget to download or select an existing SSH keypair to access your instance.
  7. Step 7: Access the Instance

    • After the instance is running, copy its Public IP and open it in a browser. You should see the default Apache web page.

4. Security Groups

Security Groups are like virtual firewalls that control inbound and outbound traffic for your EC2 instances. Each EC2 instance can be assigned one or more security groups, which define rules to allow or deny specific types of traffic.

Inbound and Outbound Rules:

  • Inbound Rules: Control incoming traffic to your instance (e.g., HTTP, SSH).

  • Outbound Rules: Control outgoing traffic from your instance (e.g., connecting to a database or another service).

Important Ports to Know:

  • Port 22 (SSH): Used for secure shell access to Linux instances.

  • Port 80 (HTTP): Used for serving web traffic (non-encrypted).

  • Port 443 (HTTPS): Used for secure web traffic (encrypted).

  • Port 3389 (RDP): Used for remote desktop protocol (Windows instances).

Lab: Setting Up a Security Group

  1. Step 1: Create a Security Group

    • In the EC2 Dashboard, click Security Groups and then click Create Security Group.

    • Name it something like "WebServerSG".

  2. Step 2: Configure Inbound Rules

    • Add inbound rules for:

      • SSH (Port 22) from your IP (or anywhere if testing).

      • HTTP (Port 80) from anywhere (for web traffic).

  3. Step 3: Apply Security Group

    • You can assign this security group to an instance during launch or after the instance is running by modifying its settings.

5. EC2 Instance Connect

EC2 Instance Connect is an AWS service that allows you to securely connect to your EC2 instance using SSH directly from your web browser, without needing to configure an SSH client locally.

Advantages:

  • No SSH Client Required: You don’t need to install or configure an SSH client.

  • Quick Access: Connect from any browser using the AWS Management Console.

  • Secure: AWS handles key management, reducing security risks.

Lab: Connect to an Instance Using EC2 Instance Connect

  1. In the EC2 Dashboard, select the running instance.

  2. Click Connect at the top of the screen.

  3. Choose EC2 Instance Connect as the connection method and click Connect.

  4. A terminal will open in your browser, providing SSH access to your instance.

This is useful for quickly accessing instances without worrying about key pairs or manually configuring SSH clients.


6. EC2 Purchasing Options

AWS provides flexible pricing options for EC2 instances to optimize costs based on your use case.

Purchasing Options:

  1. On-Demand Instances:
    Pay by the second or hour for instances with no long-term commitment. This is the simplest and most flexible pricing model, suitable for unpredictable workloads or short-term testing.

  2. Reserved Instances:
    Commit to using an instance for a one- or three-year period in exchange for significant savings. This is ideal for steady-state workloads, like databases or applications with predictable traffic.

  3. Spot Instances:
    Purchase unused EC2 capacity at a discount of up to 90%. These instances are great for batch processing or workloads that are flexible with their timing but may be interrupted if AWS needs the capacity.

  4. Savings Plans:
    A flexible pricing model that offers lower prices in exchange for a one- or three-year usage commitment, similar to Reserved Instances but more flexible.

Lab: Launching an On-Demand Instance

To launch an On-Demand instance, follow the steps in the previous lab under "Launching an EC2 Instance with User Data."


7. Spot Instances and Spot Fleet

Spot Instances allow you to purchase unused EC2 capacity at a significantly lower price, but they come with the risk that AWS may terminate them when the capacity is needed elsewhere.

Spot Fleet is a group of Spot and On-Demand Instances that EC2 automatically manages for you. You can define a target capacity and let AWS manage the provisioning and termination of instances to maintain that capacity.

Spot Instances Use Cases:

  • Batch processing

  • Big data analysis

  • Test environments

  • High-compute tasks that can tolerate interruptions

Lab: Launch a Spot Instance

  1. Step 1: Launch Instance

    • From the EC2 Dashboard, click Launch Instance.
  2. Step 2: Choose AMI and Instance Type

    • Select the same instance type and AMI (Amazon Linux 2, t2.micro).
  3. Step 3: Request Spot Instances

    • Under Purchasing Options, check Request Spot Instances.

    • Set your maximum price or let AWS use the default (market price).

  4. Step 4: Launch Instance

    • Proceed with configuration, adding your user data script and security group.

    • Click Launch to start the Spot Instance.


Conclusion

This first part of the EC2 blog series has introduced the foundational concepts of EC2, such as instance types, security groups, user data, and different purchasing options. You’ve also learned how to launch an EC2 instance and work with both On-Demand and Spot Instances using hands-on labs.

In the next part, we’ll explore more advanced topics like Elastic IPs, EBS volumes, Auto Scaling, and Load Balancing.

Stay tuned for Part 2!

1
Subscribe to my newsletter

Read articles from Siddhartha Gaurav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Siddhartha Gaurav
Siddhartha Gaurav

I'm a passionate DevOps engineer with a knack for streamlining development workflows and ensuring seamless deployment pipelines. With experience in managing cloud infrastructure, implementing DevOps best practices, and leveraging automation tools, I thrive on tackling complex challenges and driving efficiency in software delivery. Let's connect and explore how I can contribute to your projects!