Using AWS Cloud9, Docker w/CentOS image and Terraform to create an ECS cluster

Morgan Clark
5 min readJul 4, 2022

--

Hello, I am back with my latest project which contains AWS, Docker and Terraform project.

First let me give you a brief description of some of the key items utilized:

AWS Cloud9 is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal. {see for reference: https://aws.amazon.com/cloud9/}

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run. {see for reference: https://aws.amazon.com/docker/ and https://www.docker.com/}

CentOS Linux Consistent, manageable platform that suits a wide variety of deployments. For some open-source communities, it is a solid, predictable base to build upon. {see for reference: https://www.centos.org/}

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.

AWS ECR (Amazon Elastic Container Registry) Amazon Elastic Container Registry (Amazon ECR) is an AWS managed container image registry service that is secure, scalable, and reliable. {See reference: https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html}

AWS ECS (Amazon Elastic Container Service) is a highly scalable and fast container management service. You can use it to run, stop, and manage containers on a cluster. {See reference: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html}

AWS Fargate (AWS Fargate is a technology that you can use with Amazon ECS to run containers without having to manage servers or clusters of Amazon EC2 instances. (See reference: https://docs.aws.amazon.com/AmazonECS/latest/userguide/what-is-fargate.html}

The task at hand:

My team needs me to deploy a Docker container with a centos image; Pull a Centos image from the Docker registry and Create an ecs cluster using the docker image with terraform

Tools for the above task:

1. A repository account to store your completed project, GitHub (https://github.com/), GitLab (https://gitlab.com/help)

2. Selected IDE (Integrated development environment) AWS Cloud9

3. Terraform

4. AWS CLI to execute amazon commands

I. Building the files

  1. Create (make) a new director: mkdir name (mkdir project20)

2. For a slightly cleaner and more modular approach, I am using several .tf (Terraform Files) for some of the many different blocks: providers.tf, main.tf, variables.tf and vpc_subnets.tf

a. touch main.tf
b. touch providers.tf
c. touch variables.tf
d. touch vpc_subnets.tf

Figure 1 main.tf: contains details for the ECS cluster and Fargate
Figure 2 providers.tf: required providers and Terraform cloud info {See reference: https://www.terraform.io/cloud-docs}

3. In this week’s material I got a better understanding on utilizing variables, so I am using a file named variables.tf to place a few variables.

Figure 3 variables.tf: region and VPC variables
Figure 4 vpc_subnets.tf: vpc and subnet details

II. Infrastructure deployed using Terraform

a. Run terraform init to initialize modules, the backend and provider plugins.

b. Next, I executed the following commands: terraform fmt (formats the code in a structured layout) and terraform validate (runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state.)

c. Terraform plan (command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.) The below screen shot only contains a small portion of the data that was generated during the execution of this command. The Preparing the remote plan… section is for the Terraform Cloud and there is a link to access via a web-browser.

d. Terraform apply — auto-approve (command executes the actions proposed in a Terraform plan. Auto-approve eliminates any prompting that may happen when running terraform apply)

e. The process continues with the creation of the ECS Cluster, CloudWatch log group and several other components.

f. Terraform cloud workstation data:

III. Verify the results in the AWS console

a. VPC

b. Private subnets

c. Security group

d. ECS cluster

e. ECS Task definition

f. To eliminate any extra charges, run terraform destroy — -auto-approve.
g. Confirm that the number shown in the Plan section matches the number shown in the Apply Complete section to confirm that you have successfully destroyed the infrastructure.

You have successfully deployed completed the task that your team had assigned to you which consisted of deploy a Docker container with a centos image; Pull a Centos image from the Docker registry and Create an ecs cluster using the docker image with terraform.

--

--

Morgan Clark
Morgan Clark

Written by Morgan Clark

Sr. Telecom Engineer, pursuing new skills and career opportunities in DevOps.

No responses yet