AWS EC2: Complete Guide to Virtual Servers on Ubuntu

Launch, access, and manage your first cloud server: EC2 instances, storage, IPs, and security for novices.

Estimated Time: Approximately 75 - 120 minutes (includes setup and practical exercises)

Overview: What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) is a web service that provides resizable compute capacity in the cloud. Essentially, it allows you to rent virtual servers (called "instances") from Amazon and run your applications on them, just like you would on a physical server in a data center.

EC2 gives you complete control over your computing resources, letting you choose the operating system, processor, memory, and storage you need. You pay only for the capacity you actually use, making it incredibly flexible and cost-effective.

Key EC2 Concepts:

  • Instance: Your virtual server, running an operating system (e.g., Ubuntu, Windows).
  • AMI (Amazon Machine Image): A pre-configured template that includes the operating system and other software packages, used to launch instances.
  • Instance Type: Defines the CPU, memory, storage, and networking capacity of your instance (e.g., `t2.micro`, `m5.large`).
  • Region & Availability Zone (AZ): AWS global infrastructure divided into geographic Regions, each with multiple isolated AZs for high availability.
  • Security Group: A virtual firewall that controls inbound and outbound traffic for your instance(s).
  • Key Pair: A set of cryptographic keys (public and private) used to securely log into your instance using SSH.
  • Elastic IP (EIP): A static, public IPv4 address that you can associate with your instance. It remains the same even if you stop and start your instance.
  • EBS (Elastic Block Store) Volume: Network-attached block storage that acts like a hard drive for your EC2 instance.
Estimated Time

75 - 120 minutes

(Includes setting up IAM, launching an instance, accessing via SSH/PuTTY, and configuring storage/IPs.)

Experience Level

Novice

No prior AWS or server administration experience is required, but basic computer literacy is assumed.

System Requirements & Prerequisites

  • AWS Account: An active Amazon Web Services (AWS) account. If you don't have one, sign up for the AWS Free Tier.
  • IAM User: An AWS Identity and Access Management (IAM) user with programmatic access and appropriate EC2 permissions (e.g., `AmazonEC2FullAccess` for this guide, but `EC2 Read/Write` for specific actions is better).
  • Local Machine: Your personal computer (Windows, macOS, or Linux) with an internet connection.
  • SSH Client:
    • Linux/macOS: Built-in SSH client in your terminal.
    • Windows: PuTTY and PuTTYgen installed (or the new OpenSSH client built into Windows 10/11 if you prefer).

Step-by-Step Instructions

Step 1: Set Up AWS IAM User for EC2 Access (If not already done)

It's best practice to use an IAM user with specific permissions rather than your AWS root account.

  1. Log in to the AWS Management Console: Go to console.aws.amazon.com.
  2. Navigate to IAM: Search for "IAM" in the search bar and select it.
  3. Create a New User:
    • In the IAM dashboard, click `Users` in the left navigation pane.
    • Click `Add users`.
    • Enter a `User name` (e.g., `ec2-admin-user`).
    • For `AWS credential type`, select `Access key - Programmatic access`. Click `Next`.
  4. Attach Permissions:
    • On the `Set permissions` page, select `Attach policies directly`.
    • Search for `AmazonEC2FullAccess` and select the checkbox. (For production, narrow this down to specific actions and resources).
    • Click `Next`.
  5. Review and Create:
    • Review the user details and permissions.
    • Click `Create user`.
  6. Save Credentials:
    • On the `Retrieve credentials` page, you will see the `Access key ID` and `Secret access key`.
    • Download the .csv file or copy these keys immediately and store them securely. This is your ONLY chance to view the Secret access key.

Step 2: Launch an EC2 Instance (Your Virtual Server)

This is where you create your virtual server. We'll launch an Ubuntu instance, which is excellent for web applications.

  1. Navigate to EC2: From the AWS Console, search for "EC2" and select it. In the EC2 Dashboard, click `Launch instance` (or `Instances` in the left pane, then `Launch instances`).
  2. Step 1: Choose an Amazon Machine Image (AMI):
    • Search for `Ubuntu Server`.
    • Select `Ubuntu Server 22.04 LTS (HVM), SSD Volume Type` (or 20.04 LTS). Ensure it's marked `Free tier eligible`.
  3. Step 2: Choose an Instance Type:
    • Select `t2.micro` (or `t3.micro` if available in your region and free tier eligible). This is generally free tier eligible and sufficient for testing.
    • Click `Next: Configure Instance Details`.
  4. Step 3: Configure Instance Details:
    • Network: Keep default VPC.
    • Subnet: Select any available subnet (choosing different Availability Zones for high availability).
    • Auto-assign Public IP: Ensure `Enable` is selected. This gives your instance a temporary public IP to connect to.
    • Keep other settings as default for now. Click `Next: Add Storage`.
  5. Step 4: Add Storage:
    • The default root volume (8GB General Purpose SSD `gp2`) is usually fine for Ubuntu. This is where the operating system resides.
    • You can increase the size if needed (e.g., 20GB), but ensure it's still within Free Tier limits if applicable.
    • Click `Next: Add Tags`.
  6. Step 5: Add Tags:
    • Click `Add Tag`.
    • Key: `Name`, Value: `MyWebServer` (or any descriptive name). Tags help you organize your resources.
    • Click `Next: Configure Security Group`.
  7. Step 6: Configure Security Group:
    • Select `Create a new security group`.
    • Security group name: `my-ssh-sg` (or similar).
    • Description: `SSH access for my web server`.
    • Add rule:
      • Type: `SSH`
      • Protocol: `TCP`
      • Port Range: `22`
      • Source: `My IP` (AWS will automatically detect your current public IP address). **This is the most secure option for SSH.** If your IP changes frequently, you'll need to update this rule. For broad testing, you *could* select `Anywhere` (`0.0.0.0/0`), but **this is NOT recommended for production.**
    • Click `Review and Launch`.
  8. Step 7: Review Instance Launch:
    • Review all your settings. If everything looks good, click `Launch`.
  9. Step 8: Create a New Key Pair:
    • In the pop-up, select `Create a new key pair`.
    • Key pair name: `my-ec2-key` (or similar).
    • Click `Download Key Pair`. **Save this `.pem` file immediately and securely! This is your private key, and you cannot download it again.**
    • Click `Launch Instances`.

Step 3: Accessing Your EC2 Instance (Linux/macOS Terminal - SSH)

If you're using a Linux or macOS machine, you'll use the built-in `ssh` client in your terminal.

  1. Locate Your Private Key: Ensure the `my-ec2-key.pem` file you downloaded is in a secure, known location (e.g., your home directory `~/` or `~/.ssh/`).
  2. Set Correct Permissions: SSH requires your private key file to have restricted permissions (`read-only` for the owner).
  3. chmod 400 /path/to/my-ec2-key.pem
  4. Connect via SSH:
    • Open your terminal.
    • Find your instance's `Public IPv4 address` in the EC2 Console.
    • The default username for Ubuntu AMIs is `ubuntu`.
    ssh -i /path/to/my-ec2-key.pem ubuntu@YOUR_EC2_PUBLIC_IP

Basic Terminal Commands to Test Your Connection:

  • `pwd`: Print working directory.
  • `ls -l`: List directory contents.
  • `uname -a`: Display system information.
  • `df -h`: Show disk space usage.
  • `free -h`: Show memory usage.

Step 4: Accessing Your EC2 Instance (Windows - PuTTY)

On Windows, PuTTY is a popular SSH client. It requires your `.pem` key to be converted to its own format, `.ppk`.

  1. Download PuTTY and PuTTYgen:
    • If you don't have them, download the `putty.exe` and `puttygen.exe` executables from the official PuTTY website: www.putty.org.
    • Save them to a convenient location (e.g., a folder on your Desktop).
  2. Convert `.pem` to `.ppk` using PuTTYgen:
    • Open `puttygen.exe`.
    • Click `Load`. Navigate to where you saved `my-ec2-key.pem`. You might need to select `All Files (*.*)` in the file dialog to see it.
    • A pop-up will appear saying the key was successfully imported. Click `OK`.
    • Click `Save private key`.
    • PuTTYgen will warn you about saving without a passphrase. For this beginner guide, you can click `Yes` to proceed without one. **For production environments, always set a strong passphrase!**
    • Save the `.ppk` file (e.g., `my-ec2-key.ppk`) in a secure location.
  3. Connect with PuTTY:
    • Open `putty.exe`.
    • Session Category:
      • `Host Name (or IP address)`: `ubuntu@YOUR_EC2_PUBLIC_IP` (replace `YOUR_EC2_PUBLIC_IP` with your instance's public IP).
      • `Port`: `22` (default for SSH).
    • SSH Authentication:
      • In the left pane, navigate to `Connection > SSH > Auth`.
      • Click `Browse` and select your `my-ec2-key.ppk` file.
    • Save Session (Optional, but Recommended):
      • Go back to the `Session` category.
      • Enter a name in `Saved Sessions` (e.g., `My EC2 Server`).
      • Click `Save`. Now you can just load this session next time.
    • Open Connection: Click `Open`.
    • If a security alert appears about the host key, click `Accept` (after verifying the fingerprint if you're cautious).

Step 5: Attaching Additional EBS Storage (Volume)

Your EC2 instance comes with a root volume, but often you'll need more dedicated storage for data, databases, or applications. EBS volumes are highly available, persistent block storage devices.

  1. Create an EBS Volume (AWS Console):
    • In the EC2 Dashboard, navigate to `Elastic Block Store > Volumes` in the left pane.
    • Click `Create Volume`.
    • Volume Type: `gp3` (General Purpose SSD) is a good default.
    • Size: Enter `10 GiB` (or your desired size). Ensure it's within Free Tier limits if applicable.
    • Availability Zone: **Crucially, select the SAME Availability Zone as your EC2 instance.** (You can find your instance's AZ on its `Description` tab in the EC2 Console).
    • Tags: Add a `Name` tag (e.g., `MyDataVolume`).
    • Click `Create Volume`.
  2. Attach the Volume to Your Instance (AWS Console):
    • Once the volume is `available` (may take a moment), select it in the Volumes list.
    • Click `Actions > Attach Volume`.
    • Instance: Start typing your instance name (`MyWebServer`) or ID and select it.
    • Device name: Note the suggested device name (e.g., `/dev/sdf`). This might be slightly different on Ubuntu (e.g., `/dev/nvme1n1` for newer instances or `/dev/xvdf` for older). We'll verify in the next step.
    • Click `Attach Volume`.
  3. Format and Mount the Volume (Linux Terminal):
    • Connect to your EC2 instance via SSH/PuTTY.
    • List Block Devices: Identify the newly attached volume.
      lsblk
    • Format the Volume (Create a Filesystem):
      sudo mkfs -t ext4 /dev/xvdf
    • Create a Mount Point: This is the directory where your volume will be accessible.
      sudo mkdir /data
    • Mount the Volume:
      sudo mount /dev/xvdf /data
    • Verify Mount:
      df -h
    • Make Mount Persistent (Automount on Reboot):
      • Get the Volume's UUID: This is more reliable than using `/dev/xvdf`.
        sudo blkid /dev/xvdf
      • Edit `/etc/fstab`: This file tells your system what filesystems to mount at boot.
        sudo nano /etc/fstab
      • Add the following line to the end of `/etc/fstab` (replace with your actual UUID):
        UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /data ext4 defaults,nofail 0 2
      • Save (`Ctrl+O`, `Enter`) and exit `nano` (`Ctrl+X`).
      • Test `fstab` Entry:
        sudo mount -a

Step 6: Allocating and Associating an Elastic IP (Static Public IP)

By default, when you stop and start an EC2 instance, its public IP address changes. An Elastic IP (EIP) is a static public IP that you can associate with your instance, ensuring its IP address remains the same, even if the instance is stopped or restarted.

  1. Allocate a New Elastic IP (AWS Console):
    • In the EC2 Dashboard, navigate to `Network & Security > Elastic IPs` in the left pane.
    • Click `Allocate Elastic IP address`.
    • Keep default settings (Amazon's pool of IPv4 addresses).
    • Click `Allocate`.
  2. Associate the Elastic IP with Your Instance (AWS Console):
    • Once allocated, select the new Elastic IP in the list.
    • Click `Actions > Associate Elastic IP address`.
    • Resource type: `Instance`.
    • Instance: Start typing your instance name (`MyWebServer`) or ID and select it.
    • Click `Associate`.
  3. Verify the Change:
    • Go back to `EC2 Dashboard > Instances`.
    • Select your instance. The `Public IPv4 address` on its `Description` tab should now be your newly associated Elastic IP.

Step 7: Configuring Security Groups (Opening Ports for Web Server)

If you plan to run a web server (like Apache or Nginx, which you might install after this guide), you'll need to open ports 80 (HTTP) and 443 (HTTPS) in your Security Group.

  1. Edit Your Security Group (AWS Console):
    • In the EC2 Dashboard, navigate to `Network & Security > Security Groups` in the left pane.
    • Select the security group you created earlier (`my-ssh-sg`).
    • Click the `Inbound Rules` tab, then `Edit inbound rules`.
  2. Add HTTP Rule:
    • Click `Add rule`.
    • Type: `HTTP`
    • Source: `Anywhere-IPv4` (`0.0.0.0/0`). This allows web access from anywhere.
  3. Add HTTPS Rule:
    • Click `Add rule`.
    • Type: `HTTPS`
    • Source: `Anywhere-IPv4` (`0.0.0.0/0`).
  4. Click `Save rules`.

Step 8: Terminating Your EC2 Instance (Important for Cost)

When you're finished with your instance, it's crucial to terminate it to avoid incurring charges. Remember the difference between `Stop` and `Terminate`.

  1. Terminate the Instance (AWS Console):
    • In the EC2 Dashboard, go to `Instances`.
    • Select your instance (`MyWebServer`).
    • Click `Instance state > Terminate instance`.
    • Confirm the termination.
  2. Release Elastic IP (if applicable):
    • If you allocated an Elastic IP (Step 6) and no longer need it, go to `Elastic IPs`.
    • Select the Elastic IP.
    • Click `Actions > Release Elastic IP address`.
  3. Delete EBS Volumes (if applicable):
    • By default, the root EBS volume is deleted with the instance. However, additional volumes you created (like `/data` in Step 5) are often configured to `Persist` on termination.
    • To delete them, go to `Volumes`.
    • Select any volumes you no longer need.
    • Click `Actions > Delete Volume`.

Final Verification Checklist

Confirm your understanding and successful completion of the guide:

  • IAM User Created: You used an IAM user (not root) to perform actions.
  • Instance Launched: An Ubuntu EC2 instance is running and passed status checks.
  • Key Pair Secure: Your `.pem` (and `.ppk` if Windows) file is saved securely.
  • SSH Access: You can successfully connect to your instance via SSH (Linux/macOS) or PuTTY (Windows).
  • EBS Volume Mounted: An additional EBS volume is attached, formatted, and mounted persistently (if you followed Step 5).
  • Elastic IP Associated: Your instance has a static Public IP (if you followed Step 6).
  • Security Group Configured: SSH access is allowed from your IP, and HTTP/HTTPS ports are open (if you followed Step 7).
  • Resources Terminated: You successfully terminated all resources to avoid charges.

Conclusion & Next Steps

Congratulations! You've successfully launched, accessed, and configured your first virtual server on AWS EC2. This comprehensive guide has equipped you with the foundational knowledge to harness the power of cloud computing.

From here, your cloud journey can lead in many exciting directions:

  • Deploy Web Applications: Install a LAMP or LEMP stack (as in our LAMP guide) and host your website or application.
  • Monitoring & Logging: Implement AWS CloudWatch for monitoring your instance's performance and application logs.
  • Auto Scaling: Learn how to automatically scale your EC2 instances up or down based on demand, improving availability and cost-efficiency.
  • Load Balancing: Distribute incoming traffic across multiple instances using an Elastic Load Balancer (ELB) for fault tolerance and performance.
  • Managed Databases (RDS): For production databases, consider using Amazon RDS (Relational Database Service) instead of running MySQL directly on EC2.
  • Other AWS Services: Explore how EC2 integrates with other AWS services like S3 (for storage), Lambda (serverless functions), and VPC (virtual private clouds).

Need Expert AWS Cloud Solutions or Management? Contact Us!