Create an ECS Cluster with Docker and a CentOS image in Terraform

ECS, Fargate, Docker & Terraform

Elastic Container Service (ECS) is a scalable and fast container management service that makes it easy to run, stop and start Docker Containers on a cluster of EC2 instances.

An Amazon ECS cluster is a regional grouping of one or more container instances on which you can run task requests.

Amazon ECS tasks and services are run in conjunction with the Fargate , you package your application in containers, specify the Operating System, CPU and memory requirements, define networking and IAM policies, and launch the application. Each Fargate task has its own isolation boundary and does not share the underlying kernel, CPU resources, memory resources, or elastic network interface with another task.

In this article I will demonstrate how to use Terraform IaC to build an(ECS) Cluster With CentOS Image.

Requirements for this project:

  1. Terraform installed. If not installed you can use the following link to do so: https://learn.hashicorp.com/tutorials/terraform/install-cli
  2. An AWS account.
  3. An IDE of your choice.

The first thing you will do is setup a main working directory; mine is called ecs-docker. In the directory ecs-docker we will create the following files main.tf, provider.tf, vpc.tf, variable.tf and terraform.tfvars. All documentation for the code in this project is found in the Hashicorp Terraform Registry. Below is a snapshot of my directory tree.

Above ecs-docker directory tree w/ all directories needed for project.

I will create my provider.tf file; as you can see below my two providers are Docker and AWS.

Docker and AWS as providers above.

Next, I will be creating the main.tf file holding code for ECS FARGATE Cluster, CentOS image, vpc id, cpu size, memory size and container port.

main.tf

Now we will add the variables.tf file, which holds the following information our region, aws access and secret access keys, and vpc cidr.

variables.tf

Here I will create what we are calling the vpc.tf file; which includes the vpc, public and private subnet resources.

vpc.tf

Last file for creation will be the terraform.tfvars file which will hold my sensitive data consisting of aws access and secret access keys. Be very careful how you store the access and secret access keys.

After all files are created you can run the following command,

  • terraform init; to initialize terraform.
  • terraform fmt to clean up code presentation.
  • terraform plan; to see if the code will be built without any errors.
  • terraform apply to run the code.

Below are the results of my terraform code.

  1. vpc
edubs_ecs_vpc created

2. Public and Private subnets respectively below.

3. ECS Cluster created below.

ecs_cluster

4. CentOS Image being run in container

I will now now run a terraform destroy command to remove all the data from my cloud9 IDE. The IaC is now gone for this project. Terraform is a robust application that is sometimes easy to use and other times it can be very difficult keeping up with changes in the code. In my next project I will bE creating the environment using Terraform Modules to ease the burden of tracking my code in terraform.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store