How to Generate an Amazon ECS Cluster using Docker Image with Terraform

Erika Anderson
Nerd For Tech
Published in
3 min readOct 15, 2022

A short walk through on how to pull a CentOS image from the Docker registry and create an ECS cluster using the Docker image with Terraform.

Objective: Deploy a Docker container with a CentOS image.

Prerequisites: Terraform & Docker installed, AWS account, AWS CLI, and Cloud9.

Resources: Everything you need for the walk through can be found here. Occasionally, when I come across an error, I end up on Stack Overflow. Be sure to bookmark Stack Overflow, as it will be one of your besties outside of Google.

Now on to the good stuff…

First, create directory and .tf files. I created a new directory and in this directory you will have all of your terraform files for this project. To make the directory use mkdir and to navigate to the directory use cd. Then I created all my files main.tf, variables.tf, vpc.tf, subnets.tf, keys.tvars. I then used the touch command and added Docker and AWS as the providers and the AWS ECS cluster resource and Fargate module to the main.tf file.

The variables.tf file consists of the VPC, access key and secret access key. The vpc.tf consists of the VPC tag name. The same applies for subnet.tf except it is the tag name for the subnets, VPC name, with the addition of availability zones, etc. Lastly the keys.tvars holds the secret sauce, you won’t ever share this information and that is the access keys and secret keys.

Next, I used terraform init to initialize Terraform. I then used terraform plan to check out a preview of the actions Terraform would use to modify the infrastructure and lastly I used terraform fmt and terraform validate, which is used to rewrite Terraform configuration files and validate the configuration files in a directory.

On my first attempt, I had an error and immediately transitioned into investigative mode, using the resources stated at the beginning of this article.

According to my research, something went wrong in the authorization process, but once that was corrected, I re-performed terraform plan, fmt, and validate again. Once I verified the configuration was correct, I then used Terraform apply. This carries out the planned changes to each resource using the relevant infrastructure provider’s API and will verify by having you type yes to confirm. To terraform applychanges without having to type ‘yes’, you can try terraform apply -auto-approve. I haven’t tried it yet, but it would be useful in automation.

I then checked to make sure all the resources were working properly on my AWS console.

There’s nothing like the sweet smell of SUCCESS!

Lastly, I used terraform destroy to end all resources. For other ways to use the command, destroy click here.

Thank you for reading!!

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Erika Anderson
Erika Anderson

Written by Erika Anderson

Cyber professional who is life long learner.