# Deploying an Application on AWS ECS with ECR and Docker

# **Deploying an Application on AWS ECS with ECR and Docker**

## 🚀 Overview:

This project involves deploying an application using Amazon Elastic Container Service (ECS), Amazon Elastic Container Registry (ECR), and Docker. This combination allows for efficient containerization, storage, and orchestration of applications in the AWS cloud environment.

## 🔧 Problem Statement

The project goal is to implement a robust, scalable, and efficient deployment solution using AWS Elastic Container Service (ECS), Elastic Container Registry (ECR), and Docker. This solution should automate the deployment process, ensure environment consistency, improve resource utilization, enhance security, and provide better monitoring and scaling capabilities.

By successfully implementing this project, we expects to significantly reduce deployment times, minimize downtime, improve application performance, optimize resource usage, and ultimately deliver a better experience to their rapidly growing user base.

## 📌 Architecture Diagram

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724950495108/a5099e37-7e3c-4fd1-ad2f-b178806ff9ea.png align="center")

## 💽 Techonology Stack

The architecture consists of the following three tiers:

* **Private Repository** : AWS ECR
    
* **Container**: AWS ECS & Docker
    
* **Terminal**: AWS CLI
    
* **Policies and User**: AWS IAM
    
* **Virtual Machine**: AWS EC2
    

## 🌟 Project Requirements

Before you get started, make sure you have the following prerequisites in place:

* IAM User with access and secrets access keys .
    
* AWS IAM credentials configured in your text editor. In this case we will use VSCODE.
    
* A virtual machine EC2 with Docker install already
    
* Dockerfile and Index.html written
    

## 📋 Table of Contents

I - **IAM configuration**

[**Step 1:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Provider-configuration) **Create IAM Policy for ECR Access**

[**Step 2:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-variables-configuration) **Attach policy to IAM user**

[**Step 3:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-VPC-configuration) **Configure your AWS Credentials on a running Ubuntu EC2**

II - **Create an ECR Repository**

[**Step 1:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Clone-Repository) **Navigate to Amazon ECR**

[**Step 2:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Initialize-Folder) **Create a new Repository**

[**Step 3:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Format-Files) **Configure setting**

[**Step 4:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Validate-Files) **Repository created**

**III - Push Docker Image to ECR**

[**Step 1:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Apply) **Push commands for my-ecr-repo**

[**Step 2:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Review-Of-Resources) **Creation Image and push to repository**

**IV - Create ECS**

[**Step 1:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Clone-Repository) **Navigate to Amazon ECS**

[**Step 2:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Initialize-Folder) **Create a new ESC cluster**

[**Step 3:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Format-Files) **Create Task Definition**

[**Step 4:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Validate-Files) **Create a Container**

[**Step 5:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Format-Files) **Create ECS Service**

[**Step 6:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Validate-Files) **Create a Container**

**V - Access the web page**

[**Step 1:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Clone-Repository) **Navigate to Configuration**

[**Step 2:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Initialize-Folder) **Check the result**

## ✨IAM CONFIGURATION

Here is the place to set up permission and authentication for our user to access the repository

##### Step 1: **Create IAM Policy for ECR Access**

First, create an IAM policy that allows necessary permissions for Amazon ECR.

Go to AWS console, search for **IAM** service. In **IAM Dashboard,** click on **Policies** and then click on **Create policy**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724941760815/a7cdaa77-b890-4e1f-93b3-88e9a4b10f17.png align="center")

Click on **JSON** , and Then use the following JSON code for the IAM user policy to provide Amazon ECR permissions for creating repositories and pushing images.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724941921017/6da58f93-c0a2-4fa6-a265-14186f7aabfa.png align="center")

Click on **Next** and enter the **name** for your policy. In this case we will call it **AWS-ECR-Task\_Policy.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724942213353/7b7641e9-bc62-47fe-8ee4-8034017f962b.png align="center")

Click on Create policy

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724942267149/e1bd6e16-945b-4f1c-8e90-813ecc5ec2a1.png align="center")

##### Step 2: **Attach policy to IAM user**

This is where we attach the new policy to a user that was created before. Or feel free to create you own user.

Go to the **IAM Management console**, navigate to **user**, find the user. under **Set permissions**, select **Attach policies directly** and select the policy created

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724942840552/4ceec802-5fb5-4c89-8dcd-ad12475b57fe.png align="center")

Click on **Add permission**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724942956568/2b2f836e-9df7-44d2-9383-538f704180d3.png align="center")

##### Step 3: **Configure your AWS Credentials on a running Ubuntu EC2**

To perform this action as we said in the requirement you must have an Amazon Ubuntu EC2 running with Docker installed on it. Check my other project to see how to do that.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724945794791/ba7410c7-1277-4231-a96c-a4415295a9ce.png align="center")

Configure AWS credentials using the **aws configure** command.

Provide your **AWS Access Key ID**, **Secret Access Key**, **AWS Region**, and output format as **JSON**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724943277683/27f74d55-8bb9-415d-a554-3279efd88618.png align="center")

## 💼 Create an ECR Repository

Follow these steps to create an Elastic Container registry:

##### Step 1: ***Navigate* to Amazon ECR**

Use the AWS services search bar and search for ECR

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724943588545/ce0aa2f3-eb55-4701-a562-92da9f000b55.png align="center")

##### Step 2: ***Create a new Repository***

In the Amazon ECR console , click on Create

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724943724488/a460d90f-f236-43f0-8385-0ef28a3792d4.png align="center")

You will be prompt to a page where you will have to choose **Public or private registry**. For this project we will go with the private registry. then click **Create repository**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724943877800/89d6574b-e846-4ed8-a1d5-9a89d7fe53ef.png align="center")

[**Step 3:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Format-Files) ***Configure setting***

In **General setting** give a **repository name (my-ecr-repo) ,** the choose the **image mutability.** In **Encryption setting** stay with the standard **AES-256.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944142111/b5bee8fc-ea59-48a1-b70a-00871e93bc88.png align="center")

##### Step 4: ***Repository created***

The repository has been created successfully

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944342546/b73e115b-851b-4b02-8623-103920c60305.png align="center")

## 💼 Push Docker image to ECR

Here we have to create the docker image and the push it to the ECR repository. To complete that we have first the Dockerfile and the index.html files created and save in the virtual machine.

Dockerfile

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944794207/24b60eae-f045-4f9b-92af-ff9434ccd952.png align="center")

index.html

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944812753/ca23ce98-90f8-4e43-a913-9ccf8c631923.png align="center")

##### Step 1: ***Push commands for my-ecr-repo***

To push go to the **Amazon ECR**, open the **Repository name** and click on **View push commands**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944977692/d7ea23db-0617-4aaf-9f58-e3b8146f316e.png align="center")

By following below steps, you can successfully push your Docker image to Amazon ECR and make it available for use in ECS  
Run the following commands one by one.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724945077619/e5cd05e2-d09b-4092-8fbf-f5a1dfdd41e2.png align="center")

##### Step 2: ***Creation Image and push to repository***

**Authenticate Docker to ECR**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724946046168/d2ef328f-be5d-49d1-8d50-b40ba7db2bd9.png align="center")

**Build Docker Image**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724946089517/9bce2ee2-b1eb-4632-be29-3100bce7d6fb.png align="center")

Tag image and push

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724946178322/5f71cf80-34e6-4240-8fc4-0108fa69e280.png align="center")

List Images in ECR Repository:

Click on the refresh button to verify that the Docker image has been uploaded to the ECR repository .

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724946336300/227e2aaf-5e3d-4cb6-94b8-0d6c6da1b5f3.png align="center")

## **📌 Create ECS**

##### Step 1: **Navigate to Amazon ECS**

Go to the AWS Management Console and search for ECS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947486797/2585c702-85dd-4daa-a6bd-4d74433f7de8.png align="center")

##### Step 2: **Create a new ESC cluster**

in the option, Enter name for your cluster put **cluster1**  
Under the **Infrastructure**, choose **"AWS Fargate"**. Click on **Create**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947605798/75f131ca-d277-4be6-879c-5544bb6f9baa.png align="center")

Cluster was created successfully

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947658849/c6abc172-1ae7-41ba-921e-e037f8efc247.png align="center")

##### Step 3: **Create Definition Task**

Click on **Create a new task definition**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947699022/cc370348-eaa4-4c7c-8f8f-9428d7255bf1.png align="center")

Under **task definition family** enter name for your task. Choose **FARGATE** launch type. For the operating system take Linux/x86\_64.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947771528/936a85f6-cd48-41b1-a301-38c7b01b813c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947846925/9c4456a2-3ef1-48a9-922b-46244d2d14ef.png align="center")

##### Step 4: **Create a container**

Fill the option with

**Name of container** (web-server)  
**Image URL**: Copy the URI from the Repository that we created earlier  
**Essential Container** (Yes)  
**Port Mapping** Container (Port 80), **Port Name** (httpd)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947911922/c5fb721b-8079-4ddf-8c24-8a613ecb189b.png align="center")

Then click create and the task will create

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947976077/69ea3038-b3fa-45d4-8f11-1df41e746d33.png align="center")

##### Step 5: **Create ECS service**

Here you have to Go back to the cluster we created. Scroll down and click **Create** under **Services**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948197989/f1ff1bfe-49df-45ab-a60b-28c6bfdf015b.png align="center")

Under the Compute options menu. Select **Capacity provider strategy.** Select **FARGATE** as the capacity provider.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948039139/00c16061-10ee-4634-962f-21f045fbad96.png align="center")

Under Deployment configuration, choose **Task**. In **Task definition** Select the created task definition, **(i.e., ECR-httpd)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948300078/10cddbab-7438-487b-a3ab-236e3d8d46e6.png align="center")

Under **Networking**, select your VPC and Subnets.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948314483/d2bf5d18-55aa-437e-94c3-a0ac612fe1c5.png align="center")

Click on **Create new security group. Select HTTP and open to anywhere.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948381535/f39f6138-5b88-4b91-aafc-b62fa53e053c.png align="center")

Click **Create** the task has been created successfully

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948518537/13c59916-b6b2-40d2-a637-5f07d22a59d3.png align="center")

## **✨Access the web page**

[**Step 1:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Clone-Repository) **Navigate to Configuration**

Return to the cluster1 open it , click on **Tasks** and click on the running task

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948731710/d8f059fc-1cdb-4248-82a2-fb21cfd86007.png align="center")

Under **Configuration**, click on **open address.** Open the address in a web browser to access the **HTTPD** page.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948884348/2cd33c64-b7c8-47e7-81b2-c2a1d7b74461.png align="center")

[**Step 2:**](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Initialize-Folder) **Check the result**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724948917862/cc6eed2d-0ed6-468c-95fe-b21f33c31a28.png align="center")

## 🤝 Contributing

Your perspective is valuable! Whether you see potential for improvement or appreciate what's already here, your contributions are welcomed and appreciated. Thank you for considering joining us in making this project even better. Feel free to follow me for updates on this project and others, and to explore opportunities for collaboration. Together, we can create something amazing!

## 📄 License

This project is licensed under the JoebahoCloud License
