Skip to content

A Terraform template used for provisioning web application stacks on AWS ECS Fargate


Notifications You must be signed in to change notification settings


Folders and files

Last commit message
Last commit date

Latest commit



65 Commits

Repository files navigation

Terraform ECS Fargate

A set of Terraform templates used for provisioning web application stacks on AWS ECS Fargate.

Note: We suggest moving away from this template to our newer modular version. This repo will still receive updates to maintain compatability as terraform and the AWS providers evolve, but most newer features will be implemented in the module. You can find the module here:


The templates are designed to be customized. The optional components can be removed by simply deleting the .tf file.

The templates are used for managing infrastructure concerns and, as such, the templates deploy a default backend docker image. We recommend using the fargate CLI for managing application concerns like deploying your actual application images and environment variables on top of this infrastructure. The fargate CLI can be used to deploy applications from your laptop or in CI/CD pipelines.



These components are shared by all environments.

Name Description Optional AWS provider, output S3 bucket backend for storing Terraform remote state ECR repository for application (all environments share)


These components are for a specific environment. There should be a corresponding directory for each environment that is needed.

Name Description Optional Terrform remote state, AWS provider, output ECS Cluster, Service, Task Definition, ecsTaskExecutionRole, CloudWatch Log Group ALB, Target Group, S3 bucket for access logs NSG for ALB and Task HTTP listener, NSG rule. Delete if HTTPS only Yes HTTPS listener, NSG rule. Delete if HTTP only Yes CloudWatch dashboard: CPU, memory, and HTTP-related metrics Yes Application Role for container Yes IAM user that can be used by CI/CD systems Yes Performance-based auto scaling Yes Time-based auto scaling Yes Ship container logs to Yes Add a Secrets Manager secret with a CMK KMS key. Also gives app role and ECS task definition role access to read secrets from Secrets Manager Yes Adds a task definition configuration for deploying your app along with a sidecar container that writes your secrets manager secret to a file. Note that this is dependent upon opting in to Yes Add a CMK KMS key for use with SSM Parameter Store. Also gives ECS task definition role access to read secrets from parameter store. Yes Add an ECS event log dashboard Yes


Typically, the base Terraform will only need to be run once, and then should only need changes very infrequently. After the base is built, each environment can be built.

# Move into the base directory
$ cd base

# Sets up Terraform to run
$ terraform init

# Executes the Terraform run
$ terraform apply

# Now, move into the dev environment
$ cd ../env/dev

# Sets up Terraform to run
$ terraform init

# Executes the Terraform run
$ terraform apply
Important (after initial terraform apply)

The generated base .tfstate is not stored in the remote state S3 bucket. Ensure the base .tfstate is checked into your infrastructure repo. The default Terraform .gitignore generated by GitHub will ignore all .tfstate files; you'll need to modify this!


Alternatively you can use the fargate-create CLI to scaffold new projects based on this template.


curl -s | sh

create an input vars file (terraform.tfvars)

# app/env to scaffold
app = "my-app"
environment = "dev"

internal = true
container_port = "8080"
replicas = "1"
health_check = "/health"
region = "us-east-1"
aws_profile = "default"
saml_role = "admin"
vpc = "vpc-123"
private_subnets = "subnet-123,subnet-456"
public_subnets = "subnet-789,subnet-012"
tags = {
  application   = "my-app"
  environment   = "dev"
  team          = "my-team"
  customer      = "my-customer"
  contact-email = ""
$ fargate-create -f terraform.tfvars

Additional Information

Install pre-commit hook that checks terraform code for formatting

ln -s ../../ .git/hooks/pre-commit