Terraform at Scale
Hashiconf
Calvin French-Owen
Co-Founder of Segment
@calvinfo
September 7, 2016
Terraform at Scale
Terraform at Scale
💖
Terraform at Scale
Scaling vectors
Complexity
People
Complexity
People
Complexity
❌
People
Complexity
✅
How do we move
nimbly–while adding
people?
This talk
- Terraform at Segment
- What makes “good” Terraform
- What’s next
Terraform at Segment
By the numbers
- 16 developers working with Terraform
- 94 microservices
- thousands of AWS resources
A year with Terraform
December 2012 – Launch day
April 2015 – Terraform first attempt (v1)
November 2015 – Terraform “redux” (v2)
Before Terraform
Terraform at Scale
😱
Terraform at Scale
Terraform at Scale
Migrating to Terraform
April 2015
Terraform at Scale
Terraform at Scale
Migrating to Terraform
Migrating to Terraform
1. AWS accounts per environment
dev stage prod
old
prod
vpc peering
dev stage prod
old
prod
vpc peering
managed by Terraform
Separate accounts
- confidence to apply ‘at will’
- test the waters without screwing up the old account
- any sort of ‘global’ configs are okay
Migrating to Terraform
1. AWS accounts per environment
2. Docker and ECS
Terraform at Scale
Terraform: First Attempt
Terraform (our first attempt)
├── Makefile
├── README.md
└── environments
├── dev
├── production
└── stage
Terraform (our first attempt)
├── Makefile
├── README.md
└── environments
├── dev
├── production
└── stage
Terraform (our first attempt)
environments/stage
├── api.tf
├── bastion.tf
├── dns.tf
├── elasticache.tf
├── elbs.tf
├── iam.tf
├── outputs.tf
├── redis.tf
├── s3.tf
├── terraform.tfstate
├── terraform.tfvars
└── vpc.tf
Terraform (our first attempt)
resource "aws_ecs_task_definition" "app" {
family = "app"
container_definitions = <<EOF
[
{
"cpu": 1024,
"memory": 768,
"environment": [
{
"name": "NODE_ENV",
"value": "stage"
}
],
"image": "segment/app:1.54.14",
"name": "app",
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000
}
]
}
]
EOF
}
Life was
better
Life was better!Life was
better…
Life was better!Life was
better…
but not
good.
1. environment drift
Terraform first attempt
├── Makefile
├── README.md
└── environments
├── ops
├── production
└── stage
resource "aws_ecs_task_definition" "app" {
family = "app"
container_definitions = <<EOF
[
{
"cpu": 1024,
"memory": 768,
"environment": [
{
"name": "NODE_ENV",
"value": "stage"
}
],
"image": "segment/app:1.54.14",
"name": "app",
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000
}
]
}
]
EOF
}
<= stage
resource "aws_ecs_task_definition" "app" {
family = "app"
container_definitions = <<EOF
[
{
"cpu": 1024,
"memory": 768,
"environment": [
{
"name": "NODE_ENV",
"value": "stage"
}
],
"image": "segment/app:1.54.14",
"name": "app",
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000
}
]
}
]
EOF
}
<= stage
resource "aws_ecs_task_definition" "app" {
family = "app"
container_definitions = <<EOF
[
{
"cpu": 1024,
"memory": 3072,
"environment": [
{
"name": "NODE_ENV",
"value": "production”,
}
],
"image": "segment/app:1.54.17",
"name": "app",
"portMappings": [
{
"containerPort": 8000,
"hostPort": 3000
}
]
}
]
EOF
}
prod =>
2. one massive local state
Terraform at Scale
Terraform at Scale
3. production drift
$ terraform plan –target=aws_elb.feels_so_easy
$ terraform plan –target=aws_elb.oh_no_what_have_w
Terraform Redux (v2)
Terraform v1 Problems
1. massive shared state
2. locally stored state
3. drift between environments
Terraform v1 Problems
1. massive shared state: split states
2. locally stored state: remote state
3. drift between environments: modules
v2: state management
core
(vpc, networking, security groups, asgs)
auth api site db cdn
services
core
(vpc, networking, security groups, asgs)
auth api site db cdn
services
→readonly→
/**
* Remote state.
*/
resource "terraform_remote_state" "state" {
backend = "s3"
config {
bucket = "segment-ops"
key = "terraform/${var.environment}/terraform.tfstate"
}
}
data "template_file" ”test" {
template = "${file("${path.module}/init.tpl")}"
vars {
zone_id = "${terraform_remote_state.state.zone_id}"
}
}
/**
* Remote state.
*/
resource "terraform_remote_state" "state" {
backend = "s3"
config {
bucket = "segment-ops"
key = "terraform/${var.environment}/terraform.tfstate"
}
}
data "template_file" ”test" {
template = "${file("${path.module}/init.tpl")}"
vars {
zone_id = "${terraform_remote_state.state.zone_id}"
}
}
read only!
/**
* Remote state.
*/
resource "terraform_remote_state" "state" {
backend = "s3"
config {
bucket = "segment-ops"
key = "terraform/${var.environment}/terraform.tfstate"
}
}
data "template_file" ”test" {
template = "${file("${path.module}/init.tpl")}"
vars {
zone_id = "${terraform_remote_state.state.zone_id}"
}
}
read only!
reference
Terraform at Scale
v2: modules
Modules enforce
configuration parity.
Terraform at Scale
Terraform at Scale
Terraform at Scale
Terraform at Scale
What makes good*
Terraform?
*for some definitions of good
Terraform at Scale
Docker AMIs by Packer
Service Config by Terraform
1. Variables
2. Composition
3. State
4. Versioning
1. Variables
- anything a user might want to override should be a
variable
- use defaults liberally
1. Variables
resource "aws_instance" "bastion" {
ami = "${module.ami.ami_id}"
source_dest_check = false
instance_type = "${var.instance_type}"
subnet_id = "${var.subnet_id}"
key_name = "${var.key_name}"
vpc_security_group_ids = ["${split(",",var.security_groups)}"]
monitoring = true
tags {
Name = "bastion"
Environment = "${var.environment}"
}
}
configurable
configurable
configurable
configurable
configurable
1. Variables
resource "aws_instance" "bastion" {
ami = "${module.ami.ami_id}"
source_dest_check = false
instance_type = "${var.instance_type}"
subnet_id = "${var.subnet_id}"
key_name = "${var.key_name}"
vpc_security_group_ids = ["${split(",",var.security_groups)}"]
monitoring = true
tags {
Name = "bastion"
Environment = "${var.environment}"
}
}
configurable
configurable
configurable
configurable
configurable
non-configurable
non-configurable
non-configurable
1. Variables
resource "aws_instance" "bastion" {
ami = "${module.ami.ami_id}"
source_dest_check = ${var.source_dest_check}
instance_type = "${var.instance_type}"
subnet_id = "${var.subnet_id}"
key_name = "${var.key_name}"
vpc_security_group_ids = ["${split(",",var.security_groups)}"]
monitoring = ${var.monitoring}
tags {
Name = "bastion"
Environment = "${var.environment}"
}
}
2. Composition
- build modules as you need them
- it’s okay if not everything fits the abstraction
2. Composition – “full stack”
module “stack” {
source = “github.com/segmentio/stack”
name = “my-stack”
environment = “production”
}
2. Composition – inside stack
module "vpc" {
source = "./vpc”
…
}
module "security_groups" {
source = "./security-groups”
…
}
module "bastion" {
source = "./bastion”
…
}
module "dhcp" {
source = "./dhcp”
…
}
2. Composition – byo edition
module “cluster” {
source = “github.com/segmentio/stack//ecs-cluster”
environment = “prod”
name = “cdn”
vpc_id = “vpc-eff2eada”
image_id = “ami-204faaf3”
}
3. State management
- separate core from services
- states per service
- use atlas or s3
- use binary plans
core
(vpc, networking, security groups, asgs)
auth api site db cdn
services
→readonly→
4. Versioning
module “stack” {
source =
“github.com/segmentio/stack?ref=v1.x”
}
What’s next
What’s next
- Applying in CI
- Atlas
- Data sources
- Terraform generation
People
Complexity
✅
Fin
Prior Art
Stack: github.com/segmentio/stack
Atlas Examples: github.com/hashicorp/atlas-examples

More Related Content

PDF
Refactoring terraform
PDF
Terraform Introduction
PDF
Terraform 0.9 + good practices
PDF
Declarative & workflow based infrastructure with Terraform
PDF
Intro to Terraform
PDF
Using Terraform.io (Human Talks Montpellier, Epitech, 2014/09/09)
PDF
Terraform at Scale - All Day DevOps 2017
PPTX
An intro to Docker, Terraform, and Amazon ECS
Refactoring terraform
Terraform Introduction
Terraform 0.9 + good practices
Declarative & workflow based infrastructure with Terraform
Intro to Terraform
Using Terraform.io (Human Talks Montpellier, Epitech, 2014/09/09)
Terraform at Scale - All Day DevOps 2017
An intro to Docker, Terraform, and Amazon ECS

What's hot (20)

PPTX
"Continuously delivering infrastructure using Terraform and Packer" training ...
PPTX
Infrastructure as Code: Introduction to Terraform
PDF
A Hands-on Introduction on Terraform Best Concepts and Best Practices
PDF
Terraform introduction
PPTX
Final terraform
PDF
Terraform - Taming Modern Clouds
PPTX
Terraform modules restructured
PDF
OSDC 2015: Mitchell Hashimoto | Automating the Modern Datacenter, Development...
PDF
Terraform: Cloud Configuration Management (WTC/IPC'16)
PDF
Terraforming the Kubernetes Land
PDF
Terraform in action
PDF
AWS DevOps - Terraform, Docker, HashiCorp Vault
PDF
Scaling terraform
PDF
Hashiconf EU 2019 - A Tour of Terraform 0.12
PDF
Infrastructure as Code with Terraform
PDF
Rapid Infrastructure Provisioning
PDF
Building infrastructure with Terraform (Google)
PDF
Everything as Code with Terraform
PDF
Terraform in deployment pipeline
PPTX
Reusable, composable, battle-tested Terraform modules
"Continuously delivering infrastructure using Terraform and Packer" training ...
Infrastructure as Code: Introduction to Terraform
A Hands-on Introduction on Terraform Best Concepts and Best Practices
Terraform introduction
Final terraform
Terraform - Taming Modern Clouds
Terraform modules restructured
OSDC 2015: Mitchell Hashimoto | Automating the Modern Datacenter, Development...
Terraform: Cloud Configuration Management (WTC/IPC'16)
Terraforming the Kubernetes Land
Terraform in action
AWS DevOps - Terraform, Docker, HashiCorp Vault
Scaling terraform
Hashiconf EU 2019 - A Tour of Terraform 0.12
Infrastructure as Code with Terraform
Rapid Infrastructure Provisioning
Building infrastructure with Terraform (Google)
Everything as Code with Terraform
Terraform in deployment pipeline
Reusable, composable, battle-tested Terraform modules

Viewers also liked (19)

PPTX
Effective terraform
PDF
Terraform and cloud.ca
PDF
Terraform
PPTX
PPTX
Terraform
PDF
DevOps - Infrastructure as Code by Andre Marcelo-Tanner
PDF
Etcd terraform by Alex Somesan
PPTX
2016 - IGNITE - Terraform to go from Zero to Prod in less than 1 month and TH...
PPTX
Rediscovering Developer Opportunities in the Philippines by Fred Tshidimba
PDF
TerraformでECS+ECRする話
PDF
Jsonnet, terraform & packer
PDF
Infrastructure as code with Terraform
PDF
London Hug 19/5 - Terraform in Production
PPTX
Automation with Packer and TerraForm
PDF
Delivering Go.CD with Terraform and Docker
PDF
Terraform: An Overview & Introduction
PDF
PPTX
Comprehensive Terraform Training
PPTX
Scaling Your App With Docker Swarm using Terraform, Packer on Openstack
Effective terraform
Terraform and cloud.ca
Terraform
Terraform
DevOps - Infrastructure as Code by Andre Marcelo-Tanner
Etcd terraform by Alex Somesan
2016 - IGNITE - Terraform to go from Zero to Prod in less than 1 month and TH...
Rediscovering Developer Opportunities in the Philippines by Fred Tshidimba
TerraformでECS+ECRする話
Jsonnet, terraform & packer
Infrastructure as code with Terraform
London Hug 19/5 - Terraform in Production
Automation with Packer and TerraForm
Delivering Go.CD with Terraform and Docker
Terraform: An Overview & Introduction
Comprehensive Terraform Training
Scaling Your App With Docker Swarm using Terraform, Packer on Openstack

Similar to Terraform at Scale (20)

PDF
Burn down the silos! Helping dev and ops gel on high availability websites
ODP
Integrating icinga2 and the HashiCorp suite
PDF
Infrastructure-as-code: bridging the gap between Devs and Ops
PPTX
Aprovisionamiento multi-proveedor con Terraform - Plain Concepts DevOps day
PPTX
Dive into DevOps | March, Building with Terraform, Volodymyr Tsap
PDF
TIAD : Automating the modern datacenter
PPTX
Harmonious Development: Via Vagrant and Puppet
PDF
Play vs Rails
PPTX
Building and Deploying Application to Apache Mesos
KEY
Zend Framework Study@Tokyo #2
PPTX
terraform cours intéressant et super fort
PDF
Immutable Deployments with AWS CloudFormation and AWS Lambda
PDF
Charla - SharePoint en la Nube (17Jul2013)
PPTX
Meetup bangalore aug31st2019
PPTX
Scale 16x: Terraform all the Things
PDF
Terraform, Ansible, or pure CloudFormation?
KEY
PDF
Understanding OpenStack Deployments - PuppetConf 2014
KEY
Zendcon 09
KEY
From Dev to DevOps - Apache Barcamp Spain 2011
Burn down the silos! Helping dev and ops gel on high availability websites
Integrating icinga2 and the HashiCorp suite
Infrastructure-as-code: bridging the gap between Devs and Ops
Aprovisionamiento multi-proveedor con Terraform - Plain Concepts DevOps day
Dive into DevOps | March, Building with Terraform, Volodymyr Tsap
TIAD : Automating the modern datacenter
Harmonious Development: Via Vagrant and Puppet
Play vs Rails
Building and Deploying Application to Apache Mesos
Zend Framework Study@Tokyo #2
terraform cours intéressant et super fort
Immutable Deployments with AWS CloudFormation and AWS Lambda
Charla - SharePoint en la Nube (17Jul2013)
Meetup bangalore aug31st2019
Scale 16x: Terraform all the Things
Terraform, Ansible, or pure CloudFormation?
Understanding OpenStack Deployments - PuppetConf 2014
Zendcon 09
From Dev to DevOps - Apache Barcamp Spain 2011

Recently uploaded (20)

PDF
Performance, energy consumption and costs: a comparative analysis of automati...
PPTX
highway-150803160405-lva1-app6891 (1).pptx
PPTX
sub station Simple Design of Substation PPT.pptx
PDF
IAE-V2500 Engine Airbus Family A319/320
PPT
UNIT-I Machine Learning Essentials for 2nd years
PDF
BBC NW_Tech Facilities_30 Odd Yrs Ago [J].pdf
PPTX
ARCHITECTURE AND PROGRAMMING OF EMBEDDED SYSTEMS
PPTX
Hardware, SLAM tracking,Privacy and AR Cloud Data.
PDF
Application of smart robotics in the supply chain
PDF
SURVEYING BRIDGING DBATU LONERE 2025 SYLLABUS
PPTX
quantum theory on the next future in.pptx
PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PDF
IAE-V2500 Engine for Airbus Family 319/320
PDF
Introduction to Machine Learning -Basic concepts,Models and Description
PDF
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
PDF
Module 1 part 1.pdf engineering notes s7
PDF
The Journal of Finance - July 1993 - JENSEN - The Modern Industrial Revolutio...
PPTX
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
PPT
Basics Of Pump types, Details, and working principles.
Performance, energy consumption and costs: a comparative analysis of automati...
highway-150803160405-lva1-app6891 (1).pptx
sub station Simple Design of Substation PPT.pptx
IAE-V2500 Engine Airbus Family A319/320
UNIT-I Machine Learning Essentials for 2nd years
BBC NW_Tech Facilities_30 Odd Yrs Ago [J].pdf
ARCHITECTURE AND PROGRAMMING OF EMBEDDED SYSTEMS
Hardware, SLAM tracking,Privacy and AR Cloud Data.
Application of smart robotics in the supply chain
SURVEYING BRIDGING DBATU LONERE 2025 SYLLABUS
quantum theory on the next future in.pptx
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
Software defined netwoks is useful to learn NFV and virtual Lans
IAE-V2500 Engine for Airbus Family 319/320
Introduction to Machine Learning -Basic concepts,Models and Description
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
Module 1 part 1.pdf engineering notes s7
The Journal of Finance - July 1993 - JENSEN - The Modern Industrial Revolutio...
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
Basics Of Pump types, Details, and working principles.

Terraform at Scale