# Project ECR-ECS

Hey Learners! Welcome back. We already deployed nginx on the ECS cluster as a part of the 90-daysofdevops challenge on day 48, where we directly had an image from the public repository and ran the nginx container with the help of that image. But in this project, we will first create a custom image as per requirement on the local system and then push that image to the private ECR repository and use that image further for the creation of the container. Let's start...

Day 48 link:- [https://avp23.hashnode.dev/day-48-90daysofdevops](https://avp23.hashnode.dev/day-48-90daysofdevops)

### Create a private repository on ECR

Search for ECR service. Click *Create repository.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695739454462/6c6dc840-37a4-450b-b1a8-b6160a4922ee.png align="center")

Enter the name for the repository and select *Private* as *Visibility settings.* Click *Create Repository.*

*Note Repository name should be in lowercase*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695739489032/d4493137-32d5-4e69-ae95-374675bc23dc.png align="center")

Repository is created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695739516118/07ba260a-5a72-4221-a84a-96a36c8f22ac.png align="center")

### Create a custom Image and Push to the created repository

To create a Node JS app we have to create Dockerfile

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695738031805/b3868297-7696-4e6e-89ce-5d0cbf4c81d7.png align="center")

You can directly pull the code from *GitHub* on the local machine. Refer GitHub repository for Code:- [https://github.com/amitvpawar/Docker-AWS-Crash-Course.git](https://github.com/amitvpawar/Docker-AWS-Crash-Course.git)

Build an image using the docker build command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695738164268/b47d4ce9-341a-4916-a118-6c7a47a71658.png align="center")

The image is created successfully

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695738208320/ea082e40-d033-4aed-85c4-28f4db1a6c9c.png align="center")

Tag this created image with the ECR repository to push the tagged image to the ECR repository

To tag the image select the repository and click on the *View push command*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695739674432/375f8487-eb1a-413d-89e7-674af0da2a92.png align="center")

Now follow the commands shown in the above screenshot

Step 1- Docker login using AWS credentials

Prerequisite:- AWS CLI must be configured with the access token

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695739788395/1e928295-a241-4692-acb7-186809682213.png align="center")

Step 2- We can already have the Image created so we can perform step 3 directly. If you have not created an image yet then create an image using the docker build command as shown in step 2.

Step 3- Tag the created image to push it to the ECR repository. Select the proper image to tag as we have to tag the image with the ECR repository name and tag. Refer below screenshot

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695741005816/8f128df8-2049-4151-906f-343a208dc420.png align="center")

Step 4- Push the tagged image to the ECR repository

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695742235016/d6898253-2fe9-48c8-964d-abf2b1f963cf.png align="center")

Go to the console and check by selecting Repository. You will have one image with a *v1* tag.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695742259738/3601a709-0a13-4544-a217-d95b5cb3b3c2.png align="center")

Now we have to create an ECS cluster. Search for *ECS* service and select *Clusters*. Click *Create cluster.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695742328385/2f705c69-e769-4d60-a04a-5d499a89ee2e.png align="center")

Enter the Name and select *Amazon EC2 instances* in the infrastructure section. Click *Create*.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695742445878/3b526bcc-af6b-4553-803b-635739de156f.png align="center")

Create one service role with the AmazonECSTaskExecutionRolePolicy permissions

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747943775/edaf2677-f64c-45a6-9258-97bd9b1925ca.png align="center")

Check service as *ecs-Task-amazonaws.com* in *Trust relationships.*

*If it's not then change to the mentioned service or else it will not be shown in the Task role while creating the task definition.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695748125217/5598c081-ef00-44d7-bf9a-ca6bd4fbb63a.png align="center")

We will create a Task definition for our application. Select *Task definition* from the left navigation pane and click *Create new task definition.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695742606522/cb4cdd8b-a1e8-487d-8db7-19978c47f232.png align="center")

Enter a name for the Task. Select *AWS Fargate* as the *Launch type*.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743455715/62a3b300-5804-41ca-b675-152d18d6de51.png align="center")

In the container section Enter the name of the container and the image URL which is copied from the ECR repository.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695748316716/08171b40-67f4-4921-ac0a-9118f26f28bf.png align="center")

Select the Task role as we created the service role earlier by the drop-down menu. Review and click *Create.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743490843/965177ad-d72a-4f53-9a2d-dddd7a4721f5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743506856/a29b29b6-6e28-4697-8303-3ead57946634.png align="center")

Enter the information as per requirements and after completion click *Create*.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743530023/b3cc281a-f601-4181-a25b-c3eaae342b8b.png align="center")

Select the ECS cluster created earlier and navigate to the *Task* section as shown.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743602738/be4a2b04-c2a7-4611-a0d8-8a975b639d61.png align="center")

Click on Run new task Select Compute options as required and select the task definition you created earlier in the *Family* section. Click *Create.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743646588/f1c605e4-4094-4c8d-9726-bf355200df09.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695743720541/f030aad2-de9d-4780-a4ce-8b8e7d320f93.png align="center")

Select the cluster and navigate to the *Tasks* section. One task is created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695744173635/a28ca4e4-257d-474e-bbae-beb2f8f71898.png align="center")

Select the task to review all the details after showing the status as Running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695744175766/122ef2dc-7946-44fa-8202-99831429acad.png align="center")

If you try to access your application on Port 5000 it will not be accessible as we do not allow Port 5000 yet in SG.

To allow the port navigate to the *Networking* section and select *Security group.*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747070425/17e9e4d2-fa88-4698-bac7-ed9bf6e97edf.png align="center")

Select *Edit inbound rules* and add port 5000 by selecting *Add rule*. Save the changes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747155087/300bed62-2bf0-4a23-88ad-26b8a9f8425b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747167727/d888b7e3-4f59-43da-a217-a0d0d8a23458.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747252393/58d4b402-3415-4a32-a4fc-1df18ae4366d.png align="center")

Now try to access your application on port 5000.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747219528/83481b97-aa28-4a77-bea5-157a008664d1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695747324461/468a113e-af37-4c64-a66d-84fff1739c1f.png align="center")

Congratulations! You did a great project.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Find me on:- [Hashnode](https://avp23.hashnode.dev/) [LinkedIn](http://www.linkedin.com/in/amit-pawar-7b802b265) [Github](https://github.com/amitvpawar)

**Happy Learning!**
