A Docker Image Deployed Into An ECS Cluster

Let’s make a cluster, buster.

It’s week two with Terraform and I have been given a new task. For my first project (creating a two tier architecture in AWS with Terraform) I built my infrastructure in one big, giant Monolith file. This time I’d like to break my resources into separate .tf files and take advantage of using variables to clean up my work.

The team has given me a couple tasks:

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

Overview of services and tools

Amazon Elastic Container Service (Amazon ECS):

Amazon ECS is a fully managed container orchestration service that helps you easily deploy, manage, and scale containerized applications. It deeply integrates with the rest of the AWS platform to provide a secure and easy-to-use solution for running container workloads in the cloud and now on your infrastructure with Amazon ECS Anywhere.

Amazon Fargate:

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

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.

Terraform:

HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle.

Perquisites:

  • Terraform installed on local system > Link
  • AWS account and AWS CLI installed on local system > Link
  • Docker Desktop installed on local system > Link
  • Basic understanding of GitHub and Linux commands

I will be working on Windows 10 using a combination of VSCode and Ubuntu 20.04.4 LTS subsystem

Lets begin!

First thing I did was create a repo in Github, then clone it to use as my project directory. This directory will contain all the files necessary for deployment. Including a .gitignore file.

Once the repo is cloned, move into that directory and lets create some .tf files…

touch main.tf providers.tf subnets.tf variables.tf vpc.tf

Now that we are have all of our files lets first define our variables. I’ve only set a few variables that allow you to change the region, image to pull, and cidr block for VPC. Paste the following code into your variables.tf…

Next lets define our providers. Paste the following code in your providers.tf file…

Once we have our providers lets initialize the terraform environment with terraform init

Always good to check if you can initialize after creating the providers file.

Next lets create our VPC. Paste the following code into the vpc.tf file…

Now lets create some subnets for the VPC. Paste the following code into the subnets.tf file…

A quick terraform plan just to make sure we are on track…

OK looks like it can create the VPC and subnets, lets carry on…

Now lets create our ECS cluster resource and Fargate module. Copy the following code into your main.tf file…

You will have to terraform init again after using a module for Fargate…

Next let’s validate and format our work with terraform validate and terraform fmt . This will assure us that our configuration is valid and clean up our code…

Now lets run a final terraform plan to have a look at the resources that will be created…

And with that we are ready to terraform apply and build our configuration!

enter ‘yes’ to continue…

Now lets go confirm our resources in the AWS console…

The VPC…

The subnets…

Security group…

IAM service and task roles…

ECS Cluster…

Container image…

And with that the objectives of this project is complete. This was a pretty barebones example of how to create a ECS cluster with a CentOS image. I am going to have to look into improving this deployment for security, accessibility and load distribution. Any comments, questions, concerns or tomatoes to throw please feel free to leave me a comment. Hope this was able to help in any way, shape or form.

Don’t forget to run a terraform destroy and blast this project to smithereens!

Find me here! >> https://www.linkedin.com/in/dansantarossa/

--

--

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