Terraform: Deploying A Docker Image to An AWS ECS Cluster.

Matthew Mendez
DevOps Engineer Documentation
5 min readJul 5, 2021

1. About the services we will be using.

2. Prerequisites.

3. Let’s Get Started.

1a. What is terraform?

Terraform allows infrastructure to be expressed as code in a simple, human readable language called HCL (HashiCorp Configuration Language). It reads configuration files and provides an execution plan of changes, which can be reviewed for safety and then applied and provisioned.

Extensible providers allow Terraform to manage a broad range of resources, including IaaS, PaaS, SaaS, and hardware services.

Write

  • Write infrastructure as code using declarative configuration files. HashiCorp Configuration Language (HCL) allows for concise descriptions of resources using blocks, arguments, and expressions.

Plan

  • Run terraform plan to check whether the execution plan for a configuration matches your expectations before provisioning or changing infrastructure.

Apply

  • Apply changes to hundreds of cloud providers with terraform apply to reach the desired state of the configuration.

1b. What is Docker?

Docker is an open source containerization platform. Docker enables developers to package applications into containers — standardized executable components that combine application source code with all the operating system (os) libraries and dependencies required to run the code in any environment.

1c. What is AWS ECR?

Amazon Elastic Container Registry (ECR) is a fully managed container registry that makes it easy to store, manage, share, and deploy your container images and artifacts anywhere.

1d. What is AWS ECS?

AWS ECS is a container management service to run, stop, and manage Docker containers on a cluster. ECS can be used to create a consistent deployment and build experience, manage, and scale batch and Extract-Transform-Load (ETL) workloads, and build sophisticated application architectures on a microservices model.

2. Prerequisites.

  • Sign up for an AWS account.
  • Install the AWS CLI. Link below.
  • Install Docker on your OS. Link below.
  • Install Terraform. Link below.
  • Configure the AWS CLI. Link below.

3. Let’s Get Started.

We’re gonna use Terraform to pull a centos image from the Docker hub.

  • Create a new directory:

mkdir terraform-docker.

  • Within that directory make a file called main.tf

touch main.tf

  • Open up main.tf with your favorite IDE. ( I am using vim)

Terraform relies on plugins called “providers” to interact with remote systems. The code below let’s Terraform interact with Docker and lets us pull an image from the Docker Hub.

  • Once the code is written out.

Type: terraform init

Than: terraform apply

You should see your docker image being created.

  • Verify the image by typing

docker images

  • Next we’ll configure a new directory for our AWS provider.

mkdir terraform-aws

  • Within that directory, create a new file named main.tf

touch main.tf

  • Open up main.tf with your IDE and write the following code out.
  • The following code will talk to the AWS provider.
  • Configure the default region. ( I am using us-east-2)
  • Create our first repository on AWS ECR where we will push our docker image to.
  • Type terraform apply in your terminal.
  • Verify your repository was created in ECR in the AWS Console.
  • Now we can push our docker image to our ECR repo.
  • Look for the view push command button.
  • It will display a list of commands you can use to push your docker image to ECR.
  • Follow the command instructions.
  • View my example below.
  • Verify in the terminal that your image has been pushed to ECR.
  • Verify in your AWS Console that the image was pushed.
  • Next we can create an ECS cluster by adding the following code in main.tf
  • Hit another terraform apply after adding the code.
  • Verify in the ECS console that your cluster has been created.
  • In the next step we’ll need to configure an ECS task.
  • To prepare your application to run on Amazon ECS, you create a task definition. The task definiton is a text file, in JSON format, that describes one or more containers, up to a maximum of ten, that form your application. It can be thought of as a blueprint for your application.
  • Add the following code to your main.tf

In our code above, we define:

  • Our ports.
  • Stating we are using ECS Fargate.
  • IAM roles so tasks have the correct permissions to execute.

Another terraform apply and view your task in the AWS Console.

Next, we can create containers by defining our service.

  • Add the following code below.

We’ll need to configure our Network settings to state that we our launching our containers in our default VPC.

This will also reference additional resources to our default VPC and subnets.

  • Add the following code below

Hit another terraform apply and verify that your resources have been created in the AWS ECS Console.

notice the containers that have been spun up.

We have now deployed our docker image to an ECS cluster.

Now that were all done, you can type terraform destroy to clean up our environment.

Thanks for following along!

--

--

Matthew Mendez
DevOps Engineer Documentation

Documenting my journey from bartender to a career as a devops engineer