Launch An EC2 Instance of Ubuntu AMI , Enable SSH Port, Create and Attach Pem key and Install Docker, then pull and run default NGINX image in docker.

Launch An EC2 Instance of Ubuntu AMI , Enable SSH Port, Create and Attach Pem key and Install Docker, then pull and run default NGINX image in docker.

Table of contents

No heading

No headings in the article.

Whats is EC2 instance ?

An EC2 instance is a virtual server provided by Amazon Web Services (AWS) that can be launched in the cloud and configured with various operating systems, applications, and storage options. EC2 instances are scalable and can be used for a wide range of computing tasks, such as running web applications, processing data, hosting databases, and running enterprise applications.

What is Ubuntu AMI in EC2 ?

In Amazon Elastic Compute Cloud (EC2), an Amazon Machine Image (AMI) is a pre-configured virtual machine image that contains the information needed to launch an EC2 instance. An Ubuntu AMI is a specific type of AMI that contains the Ubuntu Server operating system pre-installed and configured.

What is pem key in EC2 ?

A PEM key in Amazon EC2 is a private key file that is used to authenticate SSH (Secure Shell) sessions with EC2 instances. It is a text file that contains a long string of characters and is typically generated when you create a new EC2 instance. You need to download the PEM key and use it to securely access your EC2 instance using SSH.

What is SSH Port and What is the use of that port number in AWS EC2 ?

SSH port is the default network port used by the Secure Shell (SSH) protocol to securely connect to a remote server. In Amazon Elastic Compute Cloud (EC2), port 22 is the default port used for SSH connections to Linux instances.

This port is used to establish a secure, encrypted connection between your local computer and the EC2 instance, allowing you to remotely manage the instance and perform administrative tasks. You can use SSH to securely transfer files, run commands, and perform other administrative tasks on your EC2 instances.

It is important to properly configure security group rules to allow SSH traffic to access your EC2 instances through port 22. You can limit access to specific IP addresses or ranges to ensure that only authorized users can connect to your instances.

Here we are going to Launch an EC2 instance of Ubuntu AMI and attach pem key and enable port 22 & port 80 and attach attach Systems manager Role to Access from AWS Console :

  1. Log in to the AWS Management Console and navigate to the EC2 Dashboard.

  2. Click on the "Launch Instance" button to start the instance creation process.

  3. In the "Choose an Amazon Machine Image (AMI)" screen, select "Ubuntu Server" as the AMI and choose the instance type that fits your needs.

  4. In the "Configure Instance Details" screen, you can customize various settings such as the number of instances, VPC, subnet, and other networking options. Choose the desired options for your use case.

  5. In the "Add Storage" screen, you can specify the size of the root volume and add any additional EBS volumes if required. Adjust as per your needs.

  6. In the "Add Tags" screen, you can add tags to your instance to help identify it. Add any desired tags.

  7. In the "Configure Security Group" screen, you will configure the inbound and outbound rules for the instance's security group. Add rules to allow traffic to ports 22 (SSH) and 80 (HTTP). You can also allow other ports as per your needs.

  8. Click on "Review and Launch" to review your settings and launch the instance.

  9. In the "Review Instance Launch" screen, you can review all the settings you've chosen. If everything looks good, click on "Launch" to launch the instance.

  10. In the "Select an existing key pair or create a new key pair" screen, select "Create a new key pair" and enter a name for the key pair. Download the PEM file and keep it in a secure location.

  11. Once the instance is launched, you can connect to it using an SSH client such as PuTTY. Use the PEM key you created to authenticate and log in to the instance.

  12. To attach an IAM role with the Systems Manager Full Access policy, navigate to the IAM Dashboard and create a new IAM role. Select the "AWS service" and choose EC2 as the use case.

  13. In the "Attach permissions policies" screen, search for and select the "AmazonSSMFullAccess" policy. This policy will give the instance full access to the Systems Manager service.

  14. In the "Review" screen, enter a name for the role and review the settings. If everything looks good, click on "Create role".

  15. Once the role is created, you can attach it to the EC2 instance. Navigate to the EC2 Dashboard, select the instance you created, and click on "Actions" > "Instance Settings" > "Attach/Replace IAM Role".

  16. In the "Attach/Replace IAM Role" screen, select the IAM role you created and click on "Apply".

Now we are Going to Connect the Created EC2 Instance and Hosting a Docker Image in Docker Container In bellow mentioned steps :

  1. Connect to your Ubuntu EC2 instance using an SSH client like PuTTY.

    Run the following command to update Ubuntu packages:

  2.  sudo apt update && sudo apt upgrade -y
    

    Install Docker by running the following commands:

  3.  sudo apt-get update
     sudo apt-get install docker.io -y
    

    Verify that Docker is installed correctly by running the following command:

  4.  sudo docker --version
    

    Pull the Nginx image by running the following command:

  5.  sudo docker pull nginx
    

    Verify that the Nginx image is downloaded by running the following command:

  6.  sudo docker images
    

    Run the Nginx container with the following command:

  7.  sudo docker run -d -p 80:80 nginx
    

    This command will start the Nginx container in the background (-d) and map port 80 of the container to port 80 of the host instance (-p 80:80).

    Verify that the Nginx container is running by running the following command:

  1.  sudo docker ps
    

    This command will show you a list of all the running Docker containers on your instance.

    To access the Nginx web server, open a web browser and enter the public IP address of your EC2 instance.

That's it! You should now be able to access the Nginx web server running on port 80 of your Ubuntu EC2 instance.