Creating an ECS Cluster using a CentOS Image from Docker with Terraform

James Banks
5 min readSep 28, 2021

--

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

What is ECS?

Amazon Elastic Container Service (ECS) is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances. Amazon ECS eliminates the need for you to install, operate, and scale your own cluster management infrastructure.

What is Fargate?

AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. AWS Fargate is compatible both with Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service(EKS).

Scenario:

Your team needs you to deploy a Docker container with a CentOS image. To accomplish this you will need to perform the following:

  1. Pull a Centos image from the Docker registry
  2. Create an ECS cluster using the Docker image with Terraform

Prerequisites:

  • Terraform
  • AWS account
  • AWS CLI (must be configured)
  • Your preferred IDE (i.e. Cloud9)

**If using Cloud9, Terraform and the AWS CLI are already installed.

Let’s get started…

First, in your terminal create a directory. This is where we’ll create all of our files and execute the various Terraform commands. Everything must be done within this directory in order to be executed properly. In this directory we’ll be creating the following files:

main.tf, variables.tf, provisioners.tf, terraform.tfvars, and .gitignore

main.tf

With our main.tf file we can reference this example here and simply make a few modifications. You’ll need to update the vpc_id and private_subnet_ids to reflect your own VPC. We’ve also set the test_container_image = “centos”. This will pull the latest CentOS image from the Docker Registry. We’ll also be adding a line for our load balancer.

variables.tf

Here we will set variables for our access key and secret access key. We are setting the type to string and sensitive to true. sensitive = true will ensure that these variables are not displayed as plain text in the CLI or State File. Notice that we do not actually pass our keys in this variable.tf file. That is because this file will be included in our repo and we don’t want others to have access to our keys. This information will be stored in our terraform.tvars file.

terraform.tfvars

Add your access key and secret key information to this file. This file is automatically ignored by your .gitignore file and will not be published to your repo. You can get your access key information by going to the Security Credentials under your username in the IAM Console in your AWS account. Here you can create an access key and save the information to your local machine for future use. These credentials are also needed to configure the AWS CLI to perform the necessary Terraform commands that will create your ECS Cluster. You can find more information on configuring your AWS CLI here.

providers.tf

In this file we’ll add Docker and AWS as our required providers. Note that in the AWS provider block we’re inputting our variables for the access and secret access keys.

.gitignore

This file will ensure your sensitive files are not tracked by Git.

Now that we have our files created we’re going to perform the following commands:

  • From the terminal first we will run terraform init.
  • Run terraform fmt. This ensures your formatting is correct and will modify the code for you to match.
  • Run terraform validate to ensure there are no syntax errors.
  • Run terraform plan to see what resources will be created.
  • Run terraform apply to create your infrastructure. Type Yes when prompted.

Now that the commands have been executed successfully, it’s time to verify that our resources have in fact been created. To do this we’ll need to head to our AWS account. Once logged in, navigate to the Elastic Container Service and verify that your cluster was created.

Click the Cluster name and then select the Service name. Under the Tasks tab we should be able to see our running container. Select the container and at the bottom you should the CentOS image.

And that’s it! We have successfully created an ECS Cluster using the CentOS image from Docker with Terraform.

Now comes the fun part… the clean up! Run terraform destroy --auto-approve in your terminal.

Be sure to return to your AWS account to ensure all your resources have been destroyed.

--

--