effective terraform

121
Effective Terraform Calvin French-Owen @calvinfo SF Devops for Startups 2/28/2017

Upload: calvin-french-owen

Post on 21-Mar-2017

89 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Effective terraform

Effective TerraformCalvin French-Owen@calvinfo

SF Devops for Startups2/28/2017

Page 2: Effective terraform
Page 3: Effective terraform

But it wasn’t always that way…

Page 4: Effective terraform
Page 5: Effective terraform

Where we started

Page 6: Effective terraform
Page 7: Effective terraform

Let’s provision some infrastructure!

Page 8: Effective terraform

Let’s provision some infrastructure!

uhh… now what?

Page 9: Effective terraform
Page 10: Effective terraform

It was… janky.

Page 11: Effective terraform

github.com/ivolo/animals

Page 12: Effective terraform

So we defaulted to the easiest alternative

Page 13: Effective terraform

😱

Page 14: Effective terraform

✅ Initial Speed ❌ No audits. No changelog. ❌ No reproduceability.❌ ❌ No fun :(

Page 15: Effective terraform

💖

Page 16: Effective terraform

✅ Initial Speed ✅ Audits. ✅ Changelog.

Reproduceability.✅ ✅ Fun :) (if you do it right)

Page 17: Effective terraform

This Talk• What is Terraform?

• The Segment AWS Stack

• Writing and managing “good” Terraform

• Moving beyond infrastructure

Page 18: Effective terraform

What is Terraform?

Page 19: Effective terraform

Terraform enables you to safely and predictably create, change, and improve production infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.- terraform.io

Page 20: Effective terraform
Page 21: Effective terraform

Infrastructure == Code

Page 22: Effective terraform

Terraform at work

Page 23: Effective terraform

Terraform Nouns

Page 24: Effective terraform

resource: configuration for a given cloud entity (instance, load balancer, image, network)

Page 25: Effective terraform

resources take inputs as configuration, and can produce outputs once they are created in your infrastructure

Page 26: Effective terraform
Page 27: Effective terraform

type identifierresource

Page 28: Effective terraform

inputs

Page 29: Effective terraform
Page 30: Effective terraform

module: a re-usable collection of resources that can be passed its own inputs and outputs

Page 31: Effective terraform
Page 32: Effective terraform
Page 33: Effective terraform

How does it know?

Page 34: Effective terraform

.tfstate

Page 35: Effective terraform
Page 36: Effective terraform

Terraform Workflow• $ terraform plan

• $ terraform apply

Page 37: Effective terraform

Terraform workflow1. load the desired configuration2. load the stored .tfstate file3. calculate the diff between the current and

desired states4. use CRUD APIs to update the current state to

match the desired state5. update the state file

Page 38: Effective terraform

Terraform internals1. First READ the state2. If a resource is not in the state, CREATE3. If a resource is in the state and the config

UPDATE4. If a resource is in the state and not the

config DELETE

Page 39: Effective terraform

Terraform Workflow• $ terraform plan

• $ terraform apply

Page 40: Effective terraform

Terraform applies diffs in your configuration to manage your infrastructure

Page 41: Effective terraform
Page 42: Effective terraform
Page 43: Effective terraform

Segment Terraform by the numbers- 25 developers working with

Terraform- hundreds of microservices- thousands of AWS resources

Page 44: Effective terraform

The Segment AWS Stack

Page 45: Effective terraform

AWSScalableFlexibleCheapEasy-to-use

Page 46: Effective terraform
Page 47: Effective terraform
Page 48: Effective terraform
Page 49: Effective terraform

Production-ready infrastructure in under 5 minutes

Page 50: Effective terraform

A set of terraform modules for easily booting infrastructure on AWS

Page 51: Effective terraform

The Segment AWS Stack• an auto-scaling group of instances to run

your services• a multi-az VPC with different subnets for

availability• self-managed services run via docker and

ECS• an ELB and ECS definition for each service• docker logs that populate in CloudWatch• a bastion node for manual SSH access• automatic ELB logging to S3

Page 52: Effective terraform
Page 53: Effective terraform
Page 54: Effective terraform
Page 55: Effective terraform
Page 56: Effective terraform
Page 57: Effective terraform
Page 58: Effective terraform

Writing “good” Terraform

Page 59: Effective terraform
Page 60: Effective terraform

Writing good Terraform

• Managing state

• Organizing your modules

Page 61: Effective terraform

Managing State

Page 62: Effective terraform

dev stage prod old prodvpc peering

managed by Terraform

Page 63: Effective terraform

The advantage of states per environment?

Page 64: Effective terraform

The advantage of states per environment?

Safety

Page 65: Effective terraform
Page 66: Effective terraform

Developers avoid selecting tools if the probability of the effect of the tools is unknown, and the tools have some risks.

To promote development support tools, we have to suppress the risk of the tools.

- Analyzing the Decision Criteria of Software Based on Prospect Theory

Page 67: Effective terraform
Page 68: Effective terraform

States per service

Page 69: Effective terraform

core(vpc, networking, security groups,

asgs)

auth api site db cdn

services

Page 70: Effective terraform

core(vpc, networking, security groups,

asgs)

auth api site db cdn

services→

read

onl

y →

Page 71: Effective terraform

read only!

Page 72: Effective terraform

reference

Page 73: Effective terraform
Page 74: Effective terraform

State Management• separate core from services• states per service and env• use atlas or s3

Page 75: Effective terraform

Writing modules

Page 76: Effective terraform

Writing modules• Variables

• Composability

• Visibility

• In practice

Page 77: Effective terraform

Modules - Variables• Use variables liberally, everywhere you might

need config

• Use defaults even more liberally

Page 78: Effective terraform
Page 79: Effective terraform

Clever defaults ==Simple end-user interface

Page 80: Effective terraform

“${coalesce( var.cpu, lookup( map("low", "64", "medium", "256", "high", “1024”), var.resource_allocation), “64”)}”

Page 81: Effective terraform

Modules - Composability

• Don’t start with a large set of modules

• Start by combining a few resources, then combine them

Page 82: Effective terraform

A simple example:An IAM user

Page 83: Effective terraform
Page 84: Effective terraform
Page 85: Effective terraform
Page 86: Effective terraform
Page 87: Effective terraform
Page 88: Effective terraform
Page 89: Effective terraform
Page 90: Effective terraform

Another example:Workers and Services

Page 91: Effective terraform

module “consumer” {source = “modules/worker”…

}

module “webapp” {source = “modules/service”…

}

Page 92: Effective terraform

module “consumer” {source = “modules/worker”…

}

module “webapp” {source = “modules/service”…

}

Page 93: Effective terraform

Modules - Visibility• Outputs and template_file

Page 94: Effective terraform
Page 95: Effective terraform
Page 96: Effective terraform

Normally opaque (a hash)

Page 97: Effective terraform

Modules - in practice

Page 98: Effective terraform
Page 99: Effective terraform

Repo Structure

Page 100: Effective terraform
Page 101: Effective terraform
Page 102: Effective terraform

Beyond Infrastructure

Page 103: Effective terraform

If all of our infrastructure is now applied programmatically...

…how else can we use it?

Page 104: Effective terraform

Alerting

Page 105: Effective terraform
Page 106: Effective terraform
Page 107: Effective terraform

Cost analysis

Page 108: Effective terraform
Page 109: Effective terraform
Page 110: Effective terraform
Page 111: Effective terraform

Cloud package manager?

Page 112: Effective terraform

Kube and Docker provide an awesome API…

Page 113: Effective terraform

Kube and Docker provide an awesome API…

…but cloud hosted services are here to stay

Page 114: Effective terraform
Page 115: Effective terraform
Page 116: Effective terraform
Page 117: Effective terraform
Page 118: Effective terraform
Page 119: Effective terraform

$ terraform plan <org/repo>

Page 120: Effective terraform

Terraform

• Powerful

• Flexible

• Audible

• The cross-cloud API

Page 121: Effective terraform

Fin@calvinfo