DEV Community

Cover image for AWS EC2 Guide for Beginners
Harsh Mishra
Harsh Mishra

Posted on

AWS EC2 Guide for Beginners

Introduction to AWS EC2

Amazon Elastic Compute Cloud (EC2) is a core service provided by AWS that offers scalable virtual servers in the cloud. These servers, known as EC2 instances, allow you to run applications without worrying about hardware. EC2 provides flexibility in choosing operating systems, instance types (CPU, memory, storage), and even the location (region) of your servers. EC2 is perfect for deploying applications, hosting websites, or running backend services. With the ability to scale up or down, you only pay for what you use, making it an ideal choice for beginners and enterprises alike.

Creating an EC2 Instance

Amazon EC2 allows you to create virtual servers, known as instances, in the cloud. Follow this step-by-step guide to quickly create and launch your first EC2 instance.

Step-by-Step Guide

  1. Go to the EC2 Dashboard

    • Log into your AWS Management Console.
    • Search for EC2 in the services menu and click to open the EC2 Dashboard.
    • On the dashboard, click Instances from the left-hand sidebar, then select Launch Instance.
  2. Name and Tags

    • Under Name and Tags, name your instance, e.g., My First EC2 Instance.
  3. Select an Amazon Machine Image (AMI)

    • Under Application and OS Images, select Ubuntu Server 24.04 LTS.
  4. Choose an Instance Type

    • Under Instance Type, select t2.micro. It’s eligible for the free tier and comes with 1 vCPU and 1 GiB memory, sufficient for small workloads or testing.
  5. Configure Key Pair (for Login)

    • AWS uses key pairs for secure SSH access to your instance.
    • Under Key Pair (login), select Create a new key pair and name it, e.g., My EC2 Key.
    • Choose the PPK format for PuTTY users and download the private key file (.ppk) to your local machine.
  6. Network Settings

    • By default, EC2 will configure the network settings. Ensure the VPC is set to default, and Auto-assign Public IP is enabled, allowing internet access.
  7. Configure Security Group (Firewall)

    • AWS automatically creates a security group for your instance.
    • Add the following rules:
      • Allow SSH traffic from Anywhere (0.0.0.0/0) to allow connection via PuTTY.
      • Allow HTTP traffic from Anywhere (0.0.0.0/0) to enable web traffic.
  8. Configure Storage

    • By default, EC2 provides 8 GiB of EBS General Purpose (SSD) storage, which is sufficient for most beginner projects.
  9. Review and Launch

    • Review the instance settings, including the AMI (Ubuntu), instance type (t2.micro), and security groups.
    • Click Launch Instance to start the instance.
    • You’ll see a confirmation screen stating that your instance is launching. Click View Instances to check its status.

Connecting to Your EC2 Instance Using PuTTY

Open PuTTY:

  • Enter your instance’s Public IP Address in the Host Name field.
  • Under Connection > SSH > Auth, browse for your .ppk key file.
  • Click Open to connect to the instance.
  • Log in using the username ubuntu.

Connecting via EC2 Instance Connect

Alternatively, you can connect using EC2 Instance Connect:

  1. In the EC2 Dashboard, select your running instance.
  2. Click Connect at the top, then choose EC2 Instance Connect.
  3. Click Connect again to open a terminal session in your browser.

With these steps, you’ve successfully launched and connected to your EC2 instance!

Installing Apache Web Server on EC2 Instance

After successfully connecting to your EC2 instance using PuTTY or EC2 Instance Connect, follow these steps to set up an Apache web server and deploy a simple HTML page.

Step-by-Step Commands

  1. Once logged into your EC2 instance, enter the following command to update the package lists:
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install the Apache web server:
   sudo apt install -y apache2
Enter fullscreen mode Exit fullscreen mode
  1. Start the Apache service:
   sudo systemctl start apache2
Enter fullscreen mode Exit fullscreen mode
  1. Enable Apache to start on boot:
   sudo systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode
  1. Create a simple HTML file to test your setup:
   echo "<h1>Hello World from AWS EC2</h1>" | sudo tee /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Verifying the Setup

Once these commands are executed:

  • Open a web browser and enter your EC2 instance’s Public IP Address.
  • You should see the message: "Hello World from AWS EC2" displayed on the page, confirming that Apache is running successfully.

This process sets up a basic Apache web server on your EC2 instance and verifies it by serving a simple HTML page.

Note on Accessing Your EC2 Instance

When accessing your EC2 instance via a web browser, ensure you are not using HTTPS if you haven’t set up SSL/TLS for your server. By default, Apache runs on HTTP and listens on port 80.

Example:

  • Public IP Address: 54.123.45.67
  • Public DNS: ec2-54-123-45-67.compute-1.amazonaws.com

To view your web page, enter the following URL in your browser:

  • HTTP: http://54.123.45.67 or http://ec2-54-123-45-67.compute-1.amazonaws.com

Avoid using https:// unless you have configured HTTPS on your Apache server.

Understanding Security Groups in AWS EC2

Security Groups serve as virtual firewalls for your EC2 instances, regulating inbound and outbound traffic to and from your instances. They are crucial for securing your EC2 environment by controlling access based on specified rules.

Example Security Group Configuration

Security Group Name: launch-wizard-3

Security Group ID: sg-xxxxxxxxxxxxxxxxx

Description: Created for EC2 instance setup

VPC ID: vpc-xxxxxxxxxxxxxxxxx

Inbound Rules:

  • HTTP:

    • Type: HTTP
    • Protocol: TCP
    • Port Range: 80
    • Source: 0.0.0.0/0 (Allows access from any IP address, enabling web traffic to your instance)
  • SSH:

    • Type: SSH
    • Protocol: TCP
    • Port Range: 22
    • Source: 0.0.0.0/0 (Allows SSH access from any IP address, enabling you to connect to your instance for management)

Outbound Rules:

  • By default, all outbound traffic is allowed, which is typically suitable for most scenarios.

Configuring your security group properly ensures that your EC2 instance is accessible for web traffic while maintaining secure access for management and administrative purposes. Adjust these rules as needed to fit your specific use case.

Guide to Elastic IPs and Network Interfaces in AWS EC2

Elastic IPs

Elastic IPs are static IP addresses designed for dynamic cloud computing. They provide a fixed IP address that can be associated with any instance in your AWS account, allowing for easy remapping if an instance fails or needs to be replaced.

Steps to Allocate and Associate an Elastic IP:

  1. Allocate an Elastic IP:

    • Go to the EC2 Dashboard in the AWS Management Console.
    • Click on Elastic IPs under the Network & Security section.
    • Click Allocate Elastic IP address.
    • Confirm by clicking Allocate.
  2. Associate the Elastic IP:

    • Select the newly allocated Elastic IP from the list.
    • Click Actions and choose Associate Elastic IP address.
    • Select the instance you want to associate it with from the dropdown list.
    • Click Associate.

Benefits:

  • Static IP: Ensures your instance retains a consistent IP address.
  • Failover: Easily remap to a different instance if needed.

Network Interfaces

Network Interfaces are virtual network cards that allow EC2 instances to connect to your VPC. Each interface can be attached to one instance and has its own private IP address.

Steps to Create and Attach a Network Interface:

  1. Create a Network Interface:

    • Go to the EC2 Dashboard.
    • Click on Network Interfaces under the Network & Security section.
    • Click Create network interface.
    • Configure the settings such as VPC, subnet, and private IP address.
    • Click Create.
  2. Attach the Network Interface to an Instance:

    • Select the network interface from the list.
    • Click Actions and choose Attach.
    • Select the instance you want to attach it to.
    • Click Attach.

Benefits:

  • Multiple IPs: Assign multiple IP addresses to an instance for enhanced networking.
  • Advanced Networking: Useful for complex networking setups, including multiple IP addresses or private networking.

Both Elastic IPs and Network Interfaces provide flexibility and enhanced networking capabilities for your EC2 instances, helping you manage and scale your cloud infrastructure effectively.

Guide to EC2 Volumes and Snapshots

Volumes

Volumes in AWS EC2 are block storage devices that provide persistent storage for your data, independent of the instance's lifecycle. The most common type is EBS (Elastic Block Store), which offers various performance and cost options like General Purpose SSD and Provisioned IOPS SSD.

Key Points:

  • Persistence: Data remains on the volume even if the instance is stopped or terminated.
  • Flexibility: Volumes can be attached to or detached from instances as needed.

Snapshots

Snapshots are backups of your volumes taken at a specific point in time and stored in Amazon S3. They are essential for data backup, recovery, and for migrating volumes between different availability zones.

Steps to Use Snapshots:

  1. Taking a Snapshot of the Root Volume:
    • Create Snapshot:
      • Go to the EC2 Dashboard.
      • Click on Volumes under Elastic Block Store.
      • Select the root volume of your instance.
      • Click Actions and choose Create Snapshot.
      • Provide a description and click Create Snapshot.
  • Create an AMI from the Snapshot:

    • After creating the snapshot, go to Snapshots under Elastic Block Store.
    • Select the snapshot, click Actions, and choose Create Image from Snapshot.
    • Go to AMIs under Images in the EC2 Dashboard.
    • You will find the newly created AMI. Select it and click Launch Instance to start a new instance using this image.
  • Example:

    If you take a snapshot of the root volume of the EC2 instance we created earlier and then start a new instance from the AMI created from that snapshot, the new instance will have the same setup as the original. You will see the output "Hello World from AWS EC2" directly, as the HTML code file will already be installed and served by Apache.

  1. Creating and Using a Snapshot of a Non-Root EBS Volume:
    • Create Snapshot:
      • Select the non-root EBS volume from the Volumes list.
      • Click Actions and choose Create Snapshot.
      • Enter a description and click Create Snapshot.
  • Create a New Volume from Snapshot:

    • Go to Snapshots under Elastic Block Store.
    • Select the snapshot and click Actions.
    • Choose Create Volume and configure the volume settings (e.g., size, availability zone).
    • Click Create Volume.
  • Attach the New Volume:

    • Attach the newly created volume to your instance if needed.

Important Note on Non-Root EBS Volumes

Creating Snapshots of Non-Root Volumes:

  • Warning: Creating a snapshot of a non-root EBS volume and using it to create an image might lead to issues if the volume does not contain an operating system. The instance may fail to start, as it relies on the root volume to boot up. Ensure that you are only creating images from volumes with complete operating systems if you intend to launch new instances from them.

Using snapshots allows you to effectively manage data backups, recover volumes, and migrate them across availability zones or regions. This flexibility ensures that your AWS infrastructure remains robust and adaptable to various needs.

Guide to AMI Images

Amazon Machine Images (AMIs) are pre-configured templates used to create instances in AWS EC2. An AMI includes the operating system, application server, applications, and related configurations, making it a powerful tool for launching consistent and scalable instances.

Benefits of AMIs:

  • Consistency: Ensures all instances have the same configuration.
  • Efficiency: Quickly launch new instances with pre-defined settings.
  • Scalability: Easily scale applications by launching new instances from the AMI.

Steps to Create and Use an AMI

  1. Create an AMI from an Existing Instance:
    • Select the Instance:
      • Go to the EC2 Dashboard.
      • Click on Instances and select the instance you want to create an AMI from.
  • Create Image:
    • Click Actions, then navigate to Image and Templates, and select Create Image.
    • Provide a name and description for the AMI.
    • Optionally, configure additional settings such as volume snapshots if needed.
    • Click Create Image to start the process.
  1. Launch a New Instance from an AMI:
    • Go to AMIs:
      • Navigate to AMIs under Images in the EC2 Dashboard.
      • Select the AMI you created from the list.
  • Launch Instance:

    • Click Launch Instance.
    • Configure the instance settings according to your requirements (e.g., instance type, network settings).
    • Click Launch to start a new instance using the selected AMI.
  • Example:

    Suppose you have an instance with Apache installed and configured to serve a "Hello World" HTML page. After creating an AMI from this instance, you can launch a new instance from the AMI. The new instance will have Apache pre-installed and the same "Hello World" page served, replicating the setup of the original instance seamlessly.

Key Points to Remember:

  • Consistency: Make sure that any configuration changes or installations are completed on the instance before creating the AMI if you want those changes included.
  • Efficiency: AMIs are especially useful for scaling out applications or setting up environments that mirror production systems.

By using AMIs, you can efficiently manage and scale your EC2 instances, ensuring that your cloud infrastructure remains robust and consistent across different environments.

Top comments (0)