Skip to main content

Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that simplifies the deployment, management, and scaling of containerized applications on AWS. It manages containers without the need to learn Kubernetes. With Fargate, resource management can also be serverless. In this post, you will learn how to deploy a built container image from Amazon Elastic Container Registry (ECR) to Amazon Elastic Container Service (ECS) provisioned with Fargate. The goal is to do the bare minimum to get a URL/IP from a container image on ECR (image built and pushed in part 1 of this series), let’s get going!

Deploy a Docker image from ECR to ECS with resources managed by AWS Fargate

Table of contents #

What is Amazon Elastic Container Service (ECS) #

Amazon ECS is a fully managed container orchestration service that helps you to more efficiently deploy, manage, and scale containerized applications. You can provision the underlying resource with Fargate or Elastic Compute 2 (EC2) instances. With Fargate, you can use Amazon ECS to run containers without having to manage servers or clusters of Amazon EC2 instances.

Below is a diagram of how Amazon ECS with Fargate fits in the pipeline with AWS CodePipeline, ECR, and other services including Docker in the mix:

How Amazon ECS with Fargate fits in the pipeline with AWS CodePipeline, ECR and other services

You can also use the AWS CLI to create the cluster, service, and task definition. For this tutorial, however, you will use the AWS console UI to keep things simple.

Create an Elastic Container Service cluster #

First, you must create an Amazon Elastic Container Service (ECS) cluster to deploy your Docker Image. For this, after logging in to your AWS console with a user having the correct IAM permissions, search for ecs on the search bar and click on the Clusters link under Top Features as seen below:

AWS console showing search for ecs to go to Elastic Container Service - Clusters

After the clusters listing page loads, click on the Create Cluster orange button on the top right of the page:

AWS console showing ECS Clusters empty page with Create Cluster button

Then, in the form that loads, type in the cluster's name. I am using dev-cluster as an example. In the Infrastructure section, make sure AWS Fargate (serverless) is checked, as follows:

AWS console showing ECS create cluster form with name and infrastructure section

Don’t change any of the other optional settings and scroll to the bottom of the form, where you will see the Create button, click that:

AWS console showing ECS create cluster form's end section with Create button

It might take a couple of minutes for the cluster to be created, and it will show up on the Clusters listing page as seen below:

AWS console showing ECS cluster created called dev-cluster

Hurray! Your ECS Cluster with Fargate has been created. In the next section, you will create a task definition.

Create a task definition for ECS #

A task is your application's blueprint. It can be created from a JSON file or the AWS Web UI. There are many parameters for a task, but you will only focus on the important ones for the tutorial's scope.

To create a task definition, click on Task definitions as seen in the previous screenshot, it will take you to the Task definitions listing page. Here, click on the Create new task definition orange button and select Create new task definition as seen below:

AWS console showing ECS Create new task definition link on the Task defintions page

On the task definition form, put in the Task definition family as nodejs-apps and make sure you have AWS Fargate selected in the Launch type of Infrastructure requirements :

AWS console showing ECS Create new task definition form with task definition family and infrastructure options

In the task size section, select CPU as .5 vCPU and Memory as 1 GB. As we are running a simple Hello World Node.js application, these resources would be more than enough. Then, select the Task role and Task execution role as ecsTaskExecutionRole.

AWS console showing ECS Create new task definition form with task resources and roles

Now scroll down to the Container-1 section and name the app as hello-world, for the Image URL part, copy the image URI you pushed in part 1 of this series from the container registry page as shown below:

AWS console showing ECR image page to copy the image URI for the built and pushed image

Then paste the URI in the Image URI field, as it is a single container task; this container will be Essential Container - Yes. In the port mapping section, expose port 3000 in the Container port field with the Protocol being TCP, name the port nodejs-3000, and keep App Protocol as HTTP as selected. It will look like the following:

AWS console showing ECS Create new task definition form with task container settings like name, Image URI, port, protocol, etc

Scroll down to the Environment variables and add an environment variable called PORT with the value 3000:

AWS console showing ECS Create new task definition form with task container settings adding PORT environment variable

After that, scroll to the bottom of the form and click the Create button:

AWS console showing ECS Create new task definition form submisstion with Create button

You will see the service definition has been created:

AWS console showing ECS task created

Until now, you have only created a service definition, not a service, so no containers are running. In the next section, you will create a service with a task that will bring up the container.

Create an ESC Service #

To create a service, click Clusters on the previous screenshot and then click on the cluster name, which should be dev-cluster. In the cluster detail page, on the Services tab, click the Create button on the bottom right of the page to create a service:

AWS console showing ECS service create link in the Cluster page, Services tab

On the create service form, select the Compute options as Launch type with FARGATE as Launch type and the Platform version as LATEST:

AWS console showing ECS service create form with compute configuration

In the Deployment Configuration section, Application type would be pre-selected as Service, in the Family field, select in nodejs-apps and select the Revision to be LATEST. Then name the service hello-world-service, then leave the other settings as-is like Replica has Desired tasks of 1:

AWS console showing ECS service create form with deployment configuration

Then scroll down to the Networking section, this is the important part. Expand it, make sure the VPC is selected as is. In the Subnets section click Clear current selection and from the drop-down, choose only one subnet that has us-east-1a .

In the Security group section, choose Create new security group. In the Security group name field, type in port-3000-open-from-anywhere. Similarly, type in Open port 3000 from anywhere in the Security group description field.

After that, in the Inbound rule for security groups part, choose Customized TCP as Type; in the Port Range field, type in 3000, and select' Anywhere' for the source field. Also, make sure that the Public IP is Turned On:

AWS console showing ECS service create form with networking and security group configuration

After that, scroll to the bottom of the form and click Create to create the service.

AWS console showing ECS service create form with the Create button at the end

It will take some time for the service to come up, you can click on the Service name hello-world-service:

AWS console showing ECS service created and service showing up in the Services tab

Then, on the Tasks tab on the service page, click on the task ID:

AWS console showing ECS service running and Tasks tab selected

On the task page, click on the Networking Tab and click the open address beside the Public IP as shown below:

AWS console showing ECS service task and the networking tab with the Public IP

When the IP opens in a new tab browser (if it is Chrome, allow the tab to load it insecurely without HTTPs), then append :3000 to the IP as the Hello World Node.js app is set to run on 3000 with the PORT environment variable, you should see the app run by printing Hello World! on the browser:

ESC Service and task accessible using the IP on a browser tab

Congratulations, your Node.js Hello World container is now running on ECS with Fargate. You should read about the difference between an ESC Task and a service.

Important note #

This is a simple example: in a real-life, production-ready application, you would have added a Load Balancer and some DNS records. You would also configure the Security groups, Subnets, VPCs, and IAM settings much more precisely.

You would have written some form of CI/CD pipeline to deploy the new changes automatically. You would have also added some monitoring and logging to the application.

Delete the cluster, service definition, and container registry if you don’t need them anymore.

Conclusion #

As a recap, you learned about Amazon Elastic Container Service (ECS) and how to provision it with Fargate to have serverless resources. Then, this tutorial taught you how to create an ECS task definition. After that, you deployed a service with the correct parameters to expose the service via a public IP without using a Load Balancer. Keep updating your AWS knowledge!

Comments

Latest Posts

Side Projects

Read more on

Join the Newsletter

Receive exclusive content and links about software engineering and web development every month.

    We hate spam as much as you do. Unsubscribe at any time.