Take your side project to prod on AWS using ECR and ECS
Published: 2021-08-26 18:00:55

Video Version: https://youtu.be/TUU185htgds

Welcome, in this episode we will use ECR and ECS to bring your side project to production!

In the previous articles, we saw how it is possible to create an image starting from the application code, using a Dockerfile. 

To store images we have so far used Docker Hub, which is the official Docker public registry. BUT this is not the only possible solution. In fact, every big cloud provider has its own private registry available to be able to store, push and pull images. 

In this article, we will see how to do this with AWS. Let's start giving the definitions

# ECR and ECS definitions

ECR is short for Amazon Elastic Container Registry, which is an AWS-managed container image registry. 

This means that you can create private or public repositories and push/pull images as you do with docker hub. To have a preview of how Docker Hub works, please check this link (TODO first article/video).

An Amazon ECR registry is provided to each AWS account; you can create image repositories in your registry and store images in them.

For more information, check here https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html

ECS
Amazon Elastic Container Service (Amazon ECS) is a container orchestration service managed by AWS, that helps you easily deploy, manage, and scale containerized applications. 

It integrates with the rest of the AWS platform to provide a solution for running containers on the cloud.

Disclaimer: ECS is not a free tier on Amazon, but a paid one. AWS is a cloud provider, which gets money for their services! This means that you could be charged something to follow along with this example, so BE CAREFUL!


# ECR 

Prerequisites: 

https://docs.aws.amazon.com/AmazonECR/latest/userguide/get-set-up-for-amazon-ecr.html

You also need Docker installed and running on your machine, and the AWS CLI.

### Image creation

First of all, we need an image on our computer. To do this, you can watch this old video, which is about how to create your first image for your application. In this case, we are using a Node.js Express App, but every application can be containerized.
Video: https://youtu.be/5NUAZSvWAo0



ECR is the private AWS registry. you can upload images there and use them for the ECS
to get started, type ecr on the search bar, and it should show in the dropdown

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629719828838/sx1K7KsWl.png)

Here there are private and public repositories.

### Authenticate into default registry

Fist of all, you can't retrieve useful information about your was account by typing:
```
aws sts get-caller-identity
```

please replace REGION with your region and aws_account_id with your Account id (12 digits)

```
aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
```

### Create Repository on ECR

to create a repository, use this command:
```
aws ecr create-repository \
    --repository-name hello-world \
    --image-scanning-configuration scanOnPush=true \
    --region us-east-1
```

to get your identity, type

```
aws sts get-caller-identity
```

### Image tag 
to use the image on ECR, you need to use this command:
```
docker tag hello-world:latest aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
```

to push it
```
docker push aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
```

you can verify that the image has been correctly uploaded on ECR by checking the Console:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629726223119/wl4Ih1llL.png)

# ECS

Disclaimer: ECS is not a free tier on Amazon, but a paid one. AWS is a cloud provider, which gets money for their services! This means that you could be charged something to follow along with this example, so BE CAREFUL!

On the AWS Management Console, type "ecs",  which stands for Elastic Container Service, and click on that:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629699842206/-ddkPk7Ym.png)

In case you already have some clusters, click on "Get Started" to reach the same page:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629699911321/DAT2UHVL8.png)


### Task configuration 
You will see a page like this one, select "custom" on the bottom right and then "Configure"

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629701741815/At-RAqGsp.png)

Here you can, not surprisingly, configure your service:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629701797693/VTohbXNkM.png)

the container name, can be whatever you want, we will go with "awsdemo1"


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629702757627/I0Z7m8iRL.png)


As Image we can use an image either in the Docker Hub or the Docker Hub

If you want to use Docker Hub, you can just use the repository

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629703376773/3wt37aITb.png)


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629703425514/zVVktDHVt.png)

Let's add the image name in the "Image" field

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629703534287/t4IP3ZTP_.png)

and add the Port mapping, in this case, 80.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629703608414/ICm9FhERI.png)


IF you click Advanced container configuration, there are a lot of options to configure your container, but for now, we will not use it.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629703749561/Ld9QfmUfY.png)

Now your container configurations should look like this:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629723996811/RUN8cVv1x.png)
[image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629703786971/6Zmg-iBVy.png)

Click Update at the end of the form.

Now click Next

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629704083551/FgMTz5-h8.png)

### Service

Now we should define a service, which allows you to run/ maintain a specified number of instances of a task definition in an ECS cluster.

We don't need to edit this part so you can just click "Next"

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629704118275/QGDcvSIVj.png)

### Cluster

For now you don't need to edit this part so you can just click next

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629704654834/I9nKFb2ue.png)

Then you have a review of everything you have configured:
You can review what you have defined here, then finally click the "Create" button.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629707102388/E7ofVt4Du.png)

Then Amazon ECS starts to create the resources for your service.

It takes a couple of minutes, be patient :)

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629704795137/ZNhujKRQr.png)

Once it's done, you can click "view service"


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629704942070/MDjwalYx1.png)

And here, you have a review of your service which looks, to be honest, a bit intimidating


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629704984973/6LfCJkGol.png)

For example, to update the service, you can click the top right bottom


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629705293071/v3FMhLaqm.png)

But in this case, you can click Cancel and go back the the detailed view

Click on the "Task" tab

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629705611348/2JG5xrStz.png)

And here we can see our tasks, in this case just one, 

Then click on the "Task"


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629706062326/tQ9O5VTZO.png)

Here you have a review of the Task. you can check:
- Details
- Tags
- Logs

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629706092057/Fx1gzlna1.png)

On the details, check the Network part where you have the Private IP and the PUblic IP


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629706152393/XmtcZXb_I.png)

Of course this could be handled with better DNS, but try to visit your app at the Public IP and the right port 80. 

But wait for the task to become green and with the label RUNNING


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629706820999/r6uzmn4lo.png)

To test this service we can just use Postman

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629706901471/Khvli95_T.png)


Video Version: https://youtu.be/TUU185htgds

IF you want to know more about AWS, especially about Using CodeBuild and CodePipeline to Deploy AWS Applications Easily you can check this article  https://blog.tinystacks.com/using-codebuild-and-codepipeline-to-deploy-aws-applications-easily