Take your side project to prod on AWS using ECR and ECS

Safeer Mohiuddin - Aug 26 '21 - - Dev Community

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to get your identity, type

aws sts get-caller-identity
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to push it

docker push aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
Enter fullscreen mode Exit fullscreen mode

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

image.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

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

image.png

Task configuration

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

image.png

Here you can, not surprisingly, configure your service:

image.png

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

image.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

image.png

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

image.png

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

image.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

Now your container configurations should look like this:

image.png
image.png

Click Update at the end of the form.

Now click Next

image.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

Cluster

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

image.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

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

It takes a couple of minutes, be patient :)

image.png

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

image.png

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

image.png

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

image.png

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

Click on the "Task" tab

image.png

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

Then click on the "Task"

image.png

Here you have a review of the Task. you can check:

  • Details
  • Tags
  • Logs

image.png

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

image.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

To test this service we can just use Postman

image.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

. . . . . . . . . . . . . . . . .