Getting Started with Amazon EC2

tux

12 Oct '24

Amazon Web Service (AWS), Microsoft Azure and Google Cloud Platform; these are the dominant cloud providers in the world of cloud computing. Cloud computing gained prominence at the start of the 21st century, driven by significant advancements in storage and processing power. Today, AWS is the market leader in cloud computing. Interestingly, Amazon is also the market leader in e-commerce, making it one of the rare companies which holds the lead in two different industries.

Coming back to AWS, one of the services which stand out among its offerings is Elastic Cloud Computer (EC2). EC2 is a "web service that provides sizable compute capacity in the cloud. It is designed to make web-scale computing easier for developers" (AWS). In layman's terms, it is like renting a server in the cloud. With this virtual server, you can perform various computing tasks such as running computations or hosting applications. The term 'Elastic' highlights that the server's capacity can be easily scaled up or down as needed.

The beauty of cloud computing is it has made the so-called renting of servers accessible to the public, no longer exclusive to big corporations. So, in this article, we are going to explore setting up an EC2 instance and connect to it locally via Secure Shell (SSH). In addition, we are going to use the SSH connection to install Docker in the EC2 instance. Prepare a cup of coffee and let's begin :)

Set up EC2 instance in AWS

  1. Navigate to EC2

    In AWS, navigate to EC2 by typing "EC2" in the search bar and selecting it.

  2. Launch Instance

    Launch a new instance by clicking the orange "Launch Instance" button.

  3. Choose an Amazon Machine Image (AMI)

    After naming the instance, choose an AMI. The AMI is the operating system for the instance. For a simple Linux flavour, choose Amazon Linux 2, which is a Linux operating system from AWS.

  4. Choose an instance type

    The instance type refers to the configuration for CPU, memory, storage, and networking capacity. Each instance type is designed for different use cases. Select t2.micro which is eligible for free-tier.

  5. Add Storage

    Add storage based on your needs. The default root volume (usually 8 GB) is good for most basic uses.

  6. Configure security group

    Set up a security group to define firewall rules. Allow inbound SSH traffic (for Linux). You can also choose to allow specific IP addresses for improved security.

  7. Select / create Key Pair

    A Key Pair is a set of cryptographic keys that is used to connect securely to EC2 instances. It consists of a public key and a private key. Choose an existing key pair or create a new key pair to connect to the instance. AWS will install the public key on your instance, while the private key is retained by the user. If a new key pair is created, download the private key file (.pem) and store it safely in local computer. This is required to SSH into the instance.

  8. Create Instance

    Review the configurations once more and click the "Launch" button to create the instance.


Connect to EC2 Linux instance using an SSH client

In a terminal, connect to the instance by using the command below. It has the path to the private key file (.pem) which was created / selected while creating the EC2 instance. Replace 'instance-user-name' with 'ec2-user' for Amazon Linux 2. The 'instance-public-dns-name' value can be found in AWS console.


    ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name


Once connected to the EC2 instance, you can perform various tasks such as installing Docker, which will allow you to run various containers. To install Docker, as it involves a few steps, one can create a shell script with the following:


        #!/bin/bash
        # Update package list
        sudo yum update -y
        
        # Install Docker from Amazon Linux repository
        sudo yum install -y docker
        
        # Start Docker service
        sudo systemctl start docker
        
        # Enable Docker to start on boot
        sudo systemctl enable docker
                
    

It updates all installed packages in the system that uses YUM before installing docker. It then starts Docker immediately and also configures the system to start the Docker service automatically when the system boots up, ensuring that Docker is always running whenever the machine is running.

Make the script executable (assuming script name is install_docker.sh)


    chmod +x install_docker.sh


Execute the script to install Docker:


    sudo ./install_docker.sh


Just a minor step to check if Docker is installed:


    docker --version
    

Conclusion

In this article, we have learnt how to create an EC2 instance, connect to it with SSH and install Docker in the created instance using the terminal. The video below provides a comprehensive overview of the implementations discussed above, offering detailed insights and practical demonstrations. Till next time...