Creating an EC2 Instance on AWS: A Step-by-Step Guide

Photo by CHUTTERSNAP on Unsplash

Creating an EC2 Instance on AWS: A Step-by-Step Guide

Introduction

In this detailed tutorial, we will walk you through setting up an Amazon Elastic Compute Cloud (EC2) instance and installing Ubuntu on it as we explore the amazing advantages of utilizing the capabilities of the cloud through Amazon Web Services (AWS). We'll look into the factors that make using EC2 instances in the cloud fundamental for your applications and projects. To guarantee a smooth experience, most of the steps will be completely explained with screenshots.

1. Why Choose the AWS Cloud for EC2 Instances

Before we dive into the practical steps, let's discuss the advantages of hosting your virtual machines in the cloud. The AWS cloud offers:

InviteReferrals Technology Infrastructure powered by AWS

  • Scalability: Easily adjust resources to meet your application's demands.

  • Cost Efficiency: Pay only for what you use, eliminating the need for extensive on-premises infrastructure.

  • Global Reach: Access AWS's extensive network of data centers to reduce latency and reach a broader audience.

  • Security: Benefit from AWS's robust security features and best practices.

2. Choosing the Right Region

Now that you understand the benefits of cloud computing, let's select the optimal AWS region to maximize your cloud experience. AWS has data centers worldwide, and each region is essentially a separate geographic area. This choice impacts the performance and latency of your services. Choose a region that aligns with your target audience or use case.

3. Creating Your EC2 Instance

Now, it's time to kickstart the process of creating your EC2 instance. Follow these steps to configure instance details, including naming, tags, selecting the right Amazon Machine Image (AMI), and the instance type selection.

  • Navigate to EC2: In the AWS Management Console, navigate to the EC2 section.

  • Launch an Instance: Click on the "Launch Instance" button to start the instance creation process.

  • Instance Name and Tags: After clicking "Launch Instance," you'll be guided to configure more details. Name your instance according to your preferences. This name can be anything you find fitting for your project.

  • Amazon Machine Image (AMI): Following instance naming, you'll be prompted to select an Amazon Machine Image (AMI). For this tutorial, we'll be using Ubuntu as our operating system. Ensure you choose the Ubuntu AMI that best suits your needs. Typically, the latest LTS (Long Term Support) release is a reliable choice for most projects.

  • Instance Type: EC2 instances come in various types, each designed to excel in specific tasks. When selecting an instance type, consider factors such as CPU, memory, and network performance. Here are some popular instance types and their typical use cases:

    • t2.micro: Ideal for lightweight applications, testing, and small websites. It offers a balance of CPU and memory resources.

    • m5.large: Great for general-purpose workloads, web servers, and applications requiring higher CPU performance. It provides a good balance of resources.

    • c5.xlarge: Excellent for compute-intensive tasks, databases, and applications that demand high CPU power.

    • r5.large: Perfect for memory-intensive applications, such as in-memory databases and big data analytics.

4. Creating a Key Pair

Key pairs ensure secure access to your EC2 instance. Generate a new key pair and securely store the private key (.pem) for future use.

5. Security Groups

Security groups are crucial for controlling inbound and outbound traffic to your EC2 instance. You can think of them as virtual firewalls. Here's how to configure them effectively:

  • Inbound Rules: Define rules for incoming traffic. For SSH access, allow traffic on port 22 from your IP address (e.g., "SSH (Port 22) - My IP"). You can also configure rules for HTTP (port 80), HTTPS (port 443), or any other specific ports your application requires.

Configuring security groups ensures your EC2 instance is accessible only to authorized users and services while maintaining a high level of security.

6. Storage

Customize your storage settings to define the size and requirements of your EC2 instance's storage volumes.

7. Advanced Details - User Data

During the instance launch, you can take advantage of a feature called "User Data" to automate tasks by running scripts. In this tutorial, we'll utilize User Data to perform an essential task: updating your Ubuntu packages automatically.

#!/bin/bash
sudo apt update
sudo apt upgrade -y

8. Connecting to Your EC2 Instance

Once your EC2 instance is up and running, you're ready to connect to it. In this section, we'll walk you through the process, including how to find your instance's IP address, check its status, and establish an SSH connection.

1. Finding Your Instance's IP Address

  • Step 1: Go back to the AWS EC2 dashboard in your AWS Management Console.

  • Step 2: Under the "Instances" section, you'll see a list of your instances. Locate the one you just created.

  • Step 3: In the instance details, you'll find the "Public IPv4 address." This is the IP address you'll use to connect to your instance.

2. Checking Instance Status

  • Step 1: In the same instance details section, you can check the "Instance State." It should be in the "running" state when your instance is ready for connection.

  • Step 2: Optionally, you can also check the "Status Checks" to ensure that your instance has passed both "Instance Status Checks" and "System Status Checks." These checks indicate that your instance is healthy.

3. Establishing an SSH Connection

  • Step 1: Open your terminal or SSH client on your local machine.

  • Step 2: Use the following SSH command to connect to your EC2 instance (replace <path-to-your-key.pem> with the path to your private key file and <your-instance-ip> with the instance's Public IPv4 address):

      ssh -i <path-to-your-key.pem> ubuntu@<your-instance-ip>
    

    For example:

      ssh -i ~/Downloads/my-key.pem ubuntu@54.123.456.789
    
  • Step 3: If prompted to confirm the connection, type "yes" and press Enter.

  • Step 4: You should now be connected to your EC2 instance via SSH, and you'll have access to the Ubuntu command line.

9. Changing the Hostname

Managing multiple EC2 instances becomes more efficient when you customize their hostnames. A hostname provides a clear and identifiable name for your instance, making it easier to distinguish and manage your virtual machines. In this section, we'll guide you through the process of changing your EC2 instance's hostname.

  • Changing the Hostname

    While connected to your EC2 instance via SSH, we are going to change the hostname to "PRODUCTION" using the following commands:

      sudo hostnamectl set-hostname PRODUCTION
    
  • Verifying the Hostname Change

    After setting the hostname, we can verify the change by running the hostname command:

      ubuntu@ip-10-0-1-83:~$ hostname
      PRODUCTION
    
  • Restarting the Bash Shell

    To apply the hostname change without rebooting the entire EC2 instance, we can execute the following command to restart the Bash shell:

      /bin/bash
    

Conclusion

You've successfully created an EC2 instance on AWS running Ubuntu, configured key settings, updated packages, and changed the hostname for easier management. Happy cloud computing!