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.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more