Deploy a Docker Container with ECS & Fargate using Terraform
Deploy a Docker Container with AWS ECS & Fargate using Terraform

Create an ECS Cluster
Before we start
-You will need a source code editor. I prefer Visual Studio Code or AWS Cloud9, but they must have Terraform and AWS CLI installed.
-You will need to create an AWS IAM user account with admin rights, an access key, and a secret key.
-You must possess some knowledge of Terraform
For this tutorial I am using AWS Cloud9
Click here to access this project repository in GitHub.

๐Open your source code editor, create, and name your new folder.
mkdir Terraform ECS

๐Enter your created folder
cd Terraform ECS
๐Create five configuration files
touch vpc.tf providers.tf security.tf variables.tf main.tf
For this project, I chose to separate my configuration files with the following: vpc.tf, providers.tf, security.tf, variables.tf, and main.tf
Providers
By adding those two providers we can build resources from Docker & AWS
๐Copy and paste the following code into your providers.tf ๐๐ฝ
VPC
We need a vpc and a minimum of two private subnets to build an ECS cluster
๐Copy and paste the following code into your vpc.tf ๐๐ฝ
Security Group
๐Copy and paste the following code into your security.tf ๐๐ฝ
Docker Image
We need an AWS ECS cluster and Fargate service in order to deploy a docker image for this project. The image being used is Centos. Also for this proj ect load balancing is False in Fargate.
๐Copy and paste the following code into your main.tf ๐๐ฝ
Variables
Here you can define the values for all variables.
๐Copy and paste the following code into your variables.tf ๐๐ฝ
Install the providers and run the infrastructure.
๐Run the following command
terraform init

Noticed AWS & Docker has been installed
๐Ensure that our syntax is correct with the following command:
terraform validate

๐View the layout of your infrastructure with the following command:
Terraform plan

๐Run the infrastructure with the following command:
Terraform apply --auto-approve
Auto-approve will skip the prompt to confirm installation

๐Let's check our resources


๐Now let's remove all resources with the following command.
terraform destroy

๐Type yes to confirm

Destroy complete! Resources deleted.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Deploy a Docker Container with ECS & Fargate using Terraform was originally published in Towards AWS on Medium, where people are continuing the conversation by highlighting and responding to this story.
Comments
Post a Comment