DevOps(Day-57): Project - Deploying a nginx webapp through AWS Fargate, ECS & ECR

DevOps(Day-57): Project - Deploying a nginx webapp through AWS Fargate, ECS & ECR

TABLE OF CONTENTS

Aim

Deploying an Nginx application image through AWS ECR to AWS Fargate and ECS to make our application live.

Pre-requisite

  1. Writing a Dockerfile

  2. Building AWS EC2 instance.

Project Build

  • Cloning the source code from GitHub

  1. Let's go to the GitHub repository and copy the source code url.

  2. Create an EC2 instance in AWS management console. Navigate to the EC2 instance.

  3. Clone the repository from GitHub to the EC2 instance.

    • Configuring image in AWS ECR

  4. Navigate to AWS ECR.

  5. Create a repository. I have selected public repository and the repo name.

  6. Select all the operating systems and versions according to the OS of Fargate you would select. Finally, create the repository.

  7. Now, you can check the public repository for the created repository.

    • Configuring IAM

  8. Create an IAM user in the AWS management console.

  9. Attach the policies required for accessing the ECR.

  10. Install the awscli in the AWS EC2 instance.

  11. Connect the EC2 instance with AWS management console through awscli.

    • Pushing the image to ECR

  12. Navigate to the AWS ECR and view the push commands.

    Earlier, we had pushed the image to DockerHub in my previous blogs. The steps and concepts are similar but the platform differs. Generally in IT industry ECR is used as an image registry.

  13. Execute commands 1 and 2 in the EC2 instance.

    Command 1: This will take the ecr token and provide to docker for the connection to the repository.

    Command 2: This will build the image from the Dockerfile.

  14. Execute commands 3 and 4 in EC2 instance.

    Command 3: This tags the image that we have built using docker.

    Command 4: This command will push the image from EC2 instance to ECR.

  15. After the image push, you can check the ECR for the image.

    • Configuring AWS ECS

  16. Navigate to the ECS repository in the AWS console.

  17. Create a cluster in ECS.

  18. Provide the Cluster name, VPC and subnet you want your application to be available on.

    • Using AWS Fargate

One of the major disadvantages of using AWS EC2 for an application is we have to create multiple instances in case of an application load increase that will have a cost impact.

To avoid this, we will use AWS Fargate which is a serverless technique of providing the specification of applications that will be automatically managed without spinning off any instance.

  1. Therefore, we will select AWS Fargate fo the cluster to run on.

  2. The cluster is now ready!!!

  3. We used to run docker run to create a container in the EC2 instance out of the docker image. That was nothing but a task. Therefore let's create a task definition for our cluster.

  4. Provide the task name, Container image details and ports for the application to be run on. We must have exposed a port in our Dockerfile.

  5. Select the CPU and memory configuration according to the application load.

  6. Let the rest be default and click on next.

  7. Review the details and create the task.

  8. Now, the task is finally created!!!!

  9. Click on deploy and then Run task to run the task on the cluster we had created.

  10. Choose the cluster. Select the launch type to be Fargate.

  11. Now click to create.

  12. The task is now deployed to the cluster.

    • Open port in the security group.

  13. Navigate to the ENI ID in the task.

  14. Go to the security group URL.

  15. Navigate to the Inbound rule in the security group and open the Port 80 which is HTTP and select my IP to have access for myself.

    • Project live execution

  16. Navigate to the task that we created. Take the public IP.

  17. We can see our application is live!!!!!!!

Thanks for reading my article. Have a nice day.

WRITTEN BY Biswaraj Sahoo --AWS Community Builder | DevOps Engineer | Docker | Linux | Jenkins | AWS | Git | Terraform | Docker | kubernetes

Empowering communities via open source and education. Connect with me over linktree: linktr.ee/biswaraj333