This knowledge base article will guide you through the process of creating AWS ECS infrastructure that allows you to spin up a simple website inside an ECS cluster using Terraform.
Terraform by HashiCorp is an open-source DevOps tool that helps you build, manage and define infrastructure in human-readable language.
Technologies & tools:
After finishing all the steps from this article, you should have a working:
Note: If you are not familiar how some of the following resources works, check the Documentation section for official AWS documentation
First, create a working directory for our new terraform build:
Inside your new directory create an index.html with the following code:
In this step we are creating a Dockerfile which we will use to build an image that we will run inside our ECS containers. If you are new to Docker, check the official Docker guide for more information.
Create a Dockerfile with the following code:
Note: make sure that port 80 is exposed by the Docker
Before we start working with Terraform files, make sure that you have the following environment variables configured:
Once the Dockerfile is ready we can start creating our ECR repository which we will use as a source for our container images. In this step we will create main.tf file, which is our main configuration file.
Create a main.tf and copy the following code inside:
By the end of this step our working directory structure should look like this:
After you are sure that all the files are in the right place, run:
After running terraform apply you will be presented Terraform execution plan. If everything looks in order, type yes.
Once terraform finishes creating you ECR repository, you can build your image and push it into ECR using the following steps:
1 Authenticate yourself with ECR
2 Build a Docker image using the following command
3 After the build is completed, tag your image so you can push the image to your repository:
4 Run the following command to push this image to your newly created AWS repository:
Important: replace AWS_ACCOUNT_ID and AWS_DEFAULT_REGION with your own, also you can find all push commands for your repository in AWS Management console -> Amazon ECR -> View push commands
In order to create a new cluster for our website, add the following code to our main.tffile:
After the resource is added, run terraform apply
In this step we are creating resources that we will use to manage our website’s traffic.
Add the following code to our main.tf:
After we created our Load Balancer, we can proceed creating necessary IAM roles for our task.
Copy the following code to main.tf:
After we are done with creating IAM roles, we can proceed with creating our service
Copy the following code to your main.tf:
After we have created all the resources from above, log in to your AWS Management console, navigate to your ECS cluster and check the health of your containers. If everything works, there should be three healthy containers running. If all of the containers are up and running, we can check our Load Balancer’s A record to see if our deployment was successful.
Terraform lets us define infrastructure resources in human-readable configuration files that you can version, reuse, and share with others. Also, Terraform allows us to use a consistent workflow to safely and efficiently provision and manage your infrastructure throughout its lifecycle.