Create Amazon ECS Cluster using Docker Image with Terraform

Prerequisites:
- Terraform & Docker installed
- Valid AWS account
- AWS CLI
- Your preferred IDE (I will be using Cloud9)
Our objective:
1. Pull a centos image from the Docker registry
2. Create an ECS cluster using the Docker image with Terraform
I would assume if you’re reading this article, you’re familiar with most of the services we’ll be using for this project. However, I am a fan of having people explaining things to me like I’m 5yr old… so that’s what I’ll do! I’ll teach your kindergartener DevOps! lol.
Let’s get started!
Terraform explained:
Terraform is an application that converts configuration files known as HCL (Hashicorp Configuration Language) into real world infrastructure, typically in Cloud providers such as AWS, Azure or Google Cloud Platform.
Docker explained:
Docker is a tool designed to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts needed, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be assured that the application will run on any other Linux machine regardless of settings.
ECS explained:
ECS makes it easier to work with Docker containers, providing a clustering and orchestration layer for controlling the deployment of the containers onto hosts, and manages the containers lifecycle within a cluster.
Fargate explained:
AWS Fargate is a technology that can be used with Amazon ECS & EKS. It allows you to run containers without managing servers or clusters of Amazon EC2 instances. Fargate separates the task of running containers from the task of managing the underlying infrastructure. Users can easily specify the resources that each container requires, and Fargate will handle the rest.
1| Create main.tf file
For this project, I will be using the Amazon Web Services (AWS) Cloud9 environment. First, we will need to create our main.tf file. To do this, we will simply create a new directory named terraform_ecs_tutorial
. This directory will contain your template for this tutorial.
mkdir terraform_ecs_tutorial
Navigate into the directory.
cd terraform_ecs_tutorial
We’ll go ahead and create all of our files needed for this project.
Create the following files:
main.tf
variables.tf
vpc.tf
subnets.tf
keys.tfvars
With the $ touch
command, we will add Docker and AWS as our required providers to our main.tf file and also AWS ECS cluster resource and fargate module. Terraform AWS ECS Fargate documentation can be found here: Fargate Service Registry.
2| Create Variables file
Our variable file will be for our vpc, access key and secret access key.
3| Create the VPC
4| 2 Private Subnets
5|Create .tfvars file
The .tfvars file is where we will put our access key & secret access key information. This file will be included in our repo, however others will not have access to it.
6|Terraform Init & Plan
After you have created the above files, we will initialize the terraform backend by using the terraform init
command.

Next, we will run the terraform plan
command to evaluate the Terraform configuration.


Success! Now, we will run the terraform fmt
& terraform validate
command to ensure our formatting is perfect!

7|Terraform Apply
In our final step, we will run the command terraform apply
to apply the configuration.

And it worked!!!

For fun, run the command $ terraform state list
to list all the resources within out Terraform state file.

We will pop around the AWS console to double-check that everything deployed correctly. As you can see from the images below, our VPC, Private Subnets, ECS cluster and Fargate were all created successfully:




8|Terraform Destroy
The absolute BEST thing about Terraform (in my opinion!) is that you can takedown ALL of the resources you created with ONE single command:
terraform destroy --auto-approve


Congrats!!! You have successfully created an ECS Cluster using the CentOS image from Docker with Terraform.