Image for post
Image for post

Docker Deployments using Terraform

Payam Moghaddam
Jul 18, 2018 · 10 min read
Image for post
Image for post
Terraform supports all key ECS-related resources to get set up. You simply need to put the pieces together.
Image for post
Image for post
On code commit (Source), build a new Docker image (Build), and deploy it onto ECS (Deploy), taking care of any potential schema updates along the way.

AWS Elastic Container Service (ECS)

Imperative vs. Declarative

null_resource — Execute Arbitrary Logic in Terraform

variable "password" { default = "password123" }

resource "aws_db_instance" "example" {
allocated_storage = 10
storage_type = "gp2"
engine = "postgres"
instance_class = "db.t2.micro"
name = "example"
username = "user"
password = "${var.password}"
}

resource "null_resource" "seed" {
provisioner "local-exec" {
command = "PGPASSWORD=${var.password} psql --host=${aws_db_instance.example.address} --port=${aws_db_instance.example.port} --username=${aws_db_instance.example.username} --dbname=${aws_db_instance.example.name} < seed.sql"
}
}

Taking Control of the Resource Graph

resource "null_resource" "first" {
provisioner "local-exec" {
command = "echo 'first'"
}
}

resource "null_resource" "second" {
provisioner "local-exec" {
command = "echo 'second'"
}
}

resource "null_resource" "third" {
provisioner "local-exec" {
command = "echo 'third'"
}
}
Image for post
Image for post
Underlying dependency graph shows parallel execution (simplified view)
resource "null_resource" "first" {
provisioner "local-exec" {
command = "echo 'first'"
}
}

resource "null_resource" "second" {
depends_on = ["null_resource.first"]
provisioner "local-exec" {
command = "echo 'second'"
}
}

resource "null_resource" "third" {
depends_on = ["null_resource.second"]
provisioner "local-exec" {
command = "echo 'third'"
}
}
Image for post
Image for post
Underlying dependency graph shows sequential execution (simplified view)

Targeted Changes

resource "null_resource" "deployment" {
triggers {
revision = "${var.git_revision}"
}
depends_on = [
"aws_ecs_service.application",
"aws_ecs_task_definition.application",
"null_resource.migrate"
]
}

Putting it all Together — Deploying using Terraform

Image for post
Image for post
resource "null_resource" "migrate" {
triggers {
revision = "${var.git_revision}"
database = "${aws_db_instance.application.address}"
}
depends_on = ["aws_ecs_task_definition.application"]
provisioner "local-exec" {
command = <<EOS
ruby ./ecs_task_runner.rb
-f ${aws_ecs_task_definition.application.family}
-v ${aws_ecs_task_definition.application.revision}
-e .ecs/db_migrate.sh
EOS
}
}

Additional Learnings

Conclusion

Build Galvanize

A window to the product, design, and engineering teams at…

Thanks to Alon Sabi and Peter Smith

Payam Moghaddam

Written by

Software Writer, Ninja Fighter

Build Galvanize

A window to the product, design, and engineering teams at Galvanize, an enterprise SaaS company

Payam Moghaddam

Written by

Software Writer, Ninja Fighter

Build Galvanize

A window to the product, design, and engineering teams at Galvanize, an enterprise SaaS company

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store