AWS ECS Fargate Tutorial

Prakash AgrawalPrakash Agrawal
4 min read

To follow along this tutorial, you require free Docker Hub account and a free tier AWS account. These are the steps we will follow in this tutorial:

a. Save the application image in a Docker Hub repository.

b. Define an ECS Fargate task in AWS console.

c. Set up an ECS cluster.

d. Launch the Fargate task in the ECS cluster.

e. View the resource footprint in CloudFormation.

f. Delete the ECS cluster, VPC, and Fargate task definition.

Create a container on your local machine

a. Create the folder structure on your machine: testapp1 -> src -> html

b. In this folder location, add index.html which can be copied from here: sample-repository/index.html.txt at main · techonlinewriter/sample-repository (github.com)

c. Move to the folder location: testapp1 and add a Dockerfile which can be copied from here: sample-repository/Dockerfile.txt at main · techonlinewriter/sample-repository (github.com)

d. Install Docker Desktop on your machine using instructions at Install Docker Desktop on Windows | Docker Docs and confirm the installation by using the command docker --version

e. Start the Docker Desktop on the Windows machine

f. In the Powershell Command window you can check the container images present by running command "docker images"

g. Browse to the folder where you created the project

h. You can create an image from the Dockerfile we have in this project using this command: docker build -t hello-fargate .

i. You will be able to verify that docker image is available now, you can check using the command: docker images

j. Enter the following command to create a container

docker run -d -p 80:80 hello-fargate

Run the command docker ps -a to check the status of the container.

k. Once container is running, you can check that by going to localhost in the browser

You can stop the docker container on local by running command:

docker stop <docker_name>

Create ECS Fargate Task definition on AWS Console

a. Search for ECS on AWS console and select Elastic Container Service

b. On the left side, click on Task Definitions

c. Click on Create new task definition button

d. Enter the details in the page for Create New Task Definition. Select AWS Fargate in the Launch Type.

e. Add in the minimum task size amount for CPU and Memory

Add the container information (add Image URI for the docker image you have in docker hub) and keep the default values for all other configuration for the task.

Once the task definition is created, you will get the page similar to this:

Set up an ECS Cluster

a. Select the Elastic Container Service in AWS Console

b. Select Clusters in the left and click on Create cluster on the right

c. Enter cluster information as per the below details. Then click on the Create button.

d. Once the ECS cluster is created, check the status from Cloud Formation:

Launching a task in ECS Cluster:

a. For this tutorial we only need to launch one task in the ECS Cluster

Select the task section after going in ECS Cluster that got created.

b. Enter the task details as shown and then run the task

c. In the Networking section, keep the default settings for VPC, Subnets, Security Group and Public IP

d. Add the tag for the task in the ECS Cluster

e. Once the task is launched you will get a message of Task launched

f. Check the status of the task, and it should be in the Running status

Test the task launched in the ECS Cluster

a. Open the task created and copy the public IP address

b. You will be able to see that container is running and below message is shown

c. Go to the Cloud Formation, check the Events tab and you will see that no new resource got created. That means that container and task are running in serverless AWS managed ECS Fargate cluster.

Clean up the AWS resources

a. Tasks need to be stopped by using the stop button

b. Go to the task definition and select Deregister

c. Once the task is deregistered it will show message that deregister is successful

d. Select the Clusters option on left side

e. Click on the cluster name and then click on Delete Cluster

We have now cleaned up the resources created for this tutorial.

About me: I am an independent technical writer and if you are an organization that want to hire me then I can be contacted at techonlinewriter@gmail.com.

0
Subscribe to my newsletter

Read articles from Prakash Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prakash Agrawal
Prakash Agrawal