Home

Awesome

terraform-ecs-fargate-dns-https

A fargate-create compatible Terraform template that spins up HTTPS applications with DNS and TLS certificates.

This will give you a running endpoint with the following format:

https://${environment}.${app}.${domain}

diagram

Note that this assumes that the route53 zone for the domain is in the same account and you have access to create records.

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.

Components

base

These components are shared by all environments.

NameDescriptionOptional
main.tfAWS provider, output
state.tfS3 bucket backend for storing Terraform remote state
ecr.tfECR repository for application (all environments share)

env/dev

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

NameDescriptionOptional
main.tfTerrform remote state, AWS provider, output
ecs.tfECS Cluster, Service, Task Definition, ecsTaskExecutionRole, CloudWatch Log Group
lb.tfALB, Target Group, S3 bucket for access logs
nsg.tfNSG for ALB and Task
lb-https.tfHTTPS listener, NSG rule, DNS entry, HTTPS CertificateYes
dashboard.tfCloudWatch dashboard: CPU, memory, and HTTP-related metricsYes
role.tfApplication Role for containerYes
cicd.tfIAM user that can be used by CI/CD systemsYes
autoscale-perf.tfPerformance-based auto scalingYes
autoscale-time.tfTime-based auto scalingYes
logs-logzio.tfShip container logs to logz.ioYes
secretsmanager.tfAdd a base secret to SecretsmanagerYes
ecs-event-stream.tfAdd an ECS event log dashboardYes

Usage

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!

fargate-create

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

install

curl -s get-fargate-create.turnerlabs.io | sh

create an input vars file (terraform.tfvars)

app = "myapp"
environment = "dev"
domain = "foo.turnerlabs.io"

region = "us-east-1"
aws_profile = "default"
saml_role = "myrole"

tags = {
  application   = "myapp"
  environment   = "dev"
  team          = "myteam"
  customer      = "mycustomer"
  contact-email = "me@email.com"
}

container_port = "8080"
health_check = "/health"

vpc = "vpc-123456789"
private_subnets = "subnet-abc,subnet-123"
public_subnets = "subnet-def,subnet-456"
$ fargate-create -t git@github.com:turnerlabs/terraform-ecs-fargate-dns-https

Additional Information