Code Like A Girl

Welcome to Code Like A Girl, a space that celebrates redefining society's perceptions of women in…

Follow publication

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.

It’s cool if you have to run terraform plan a few times! We get it!

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Code Like A Girl

Welcome to Code Like A Girl, a space that celebrates redefining society's perceptions of women in technology. Share your story with us!

Written by Kinsey Matthews

𝐂𝐥𝐨𝐮𝐝 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫 ☁️ 𝐇𝐚𝐬𝐡𝐢𝐜𝐨𝐫𝐩 𝐓𝐞𝐫𝐫𝐚𝐟𝐨𝐫𝐦 𝐀𝐬𝐬𝐨𝐜𝐢𝐚𝐭𝐞 🔒 𝐀𝐖𝐒 𝐂𝐞𝐫𝐭𝐢𝐟𝐢𝐞𝐝 𝐃𝐞𝐯𝐎𝐩𝐬 🌐 𝐋𝐢𝐧𝐮𝐱 𝐂𝐞𝐫𝐭𝐢𝐟

Responses (3)

Write a response