1. Variables - names, tags, environments
2. Constants - set defaults if overrides, otherwise locals
3. Dynamic Values - use interpolation / data sources
4. Secrets - generate dynamically / data sources
Slide 12
Slide 12 text
Break Down Monoliths
There’s no such thing as micro-Terraform
Slide 13
Slide 13 text
Monolithic Singleton
put everything in one configuration and one state to rule it all.
Slide 14
Slide 14 text
No content
Slide 15
Slide 15 text
Do I have a monolithic singleton?
1. Find and replace
2. Long apply time
Slide 16
Slide 16 text
Refactor
1. Find and replace → modules
2. Long apply time → state
Slide 17
Slide 17 text
Module Refactor
Slide 18
Slide 18 text
Pattern: Use
moved
Module Refactor
Only
resource "aws_instance" "bastion" {}
module "bastion" {}
moved {
from = aws_instance.bastion
to =
module.bastion.aws_instance.bastion
}
CODE EDITOR
developer.hashicorp.com/terraform/language/modules/develop/refactoring
Slide 19
Slide 19 text
Pattern: Use
import
Module Refactor
TERMINAL
# Build module
> terraform import \
module...
> terraform state rm .
# Comment out old resources in Terraform
> terraform plan # check no drift
# Delete old resources in Terraform
Slide 20
Slide 20 text
State Refactor
Slide 21
Slide 21 text
Dependency Injection
decouple dependencies to handle cross-state interpolation
Pattern:
Dependency
Injection
with Data
Sources
data "aws_vpcs" "network" {
tags = {
unit = "hug"
}
}
data "aws_subnet_ids" "private" {
vpc_id = data.aws_vpcs.network.ids.0
tags = {
Type = "private"
}
}
resource "aws_instance" "server" {
subnet_id = data.aws_subnet_ids.private.ids.0
}
CODE EDITOR
Slide 24
Slide 24 text
State Refactor
Slide 25
Slide 25 text
Pattern: Use
import
State Refactor
TERMINAL
# Copy resource configurations to new
working directory
# Implement dependency injection
> cd
> terraform init
> terraform import .
> terraform plan # check no drift
Slide 26
Slide 26 text
Pattern: Use
import
State Refactor
TERMINAL
> cd
> terraform state rm .
# Comment out old resources in Terraform
> terraform plan # check no drift
# Delete old resources in Terraform
Slide 27
Slide 27 text
Refactor Rules
1. Go from high-level to low-level resources
2. Choose resources with fewer dependencies
3. terraform plan = test
4. terraform state = source of truth
Slide 28
Slide 28 text
Use Immutability
When all else fails, create everything new.
Pattern:
Blue-green
deployment
Old Network New Network
Servers Servers
Modules in
same state
Module in
separate state
Module in
separate state
Slide 31
Slide 31 text
Pattern:
Blue-green
deployment
Blue resources share the
same state
terraform {
cloud {
organization = "hashicorp"
workspaces {
name = “hug"
}
}
}
module "network" {}
module "servers" {
count = 3
subnet = module.network.subnet.0
}
CODE EDITOR
Slide 32
Slide 32 text
Pattern:
Blue-green
deployment
Green resources in
separate states
terraform {
cloud {
organization = "hashicorp"
workspaces {
name = "hug-network"
}
}
}
module "network" {}
output “subnets" {
value = module.network.subnets
}
CODE EDITOR
Slide 33
Slide 33 text
Pattern:
Blue-green
deployment
Green resources in
separate states
terraform {
cloud {
organization = "hashicorp"
workspaces {
name = "hug-servers"
}
}
}
data "tfe_outputs" "network" {
organization = "hashicorp"
workspace = "hug-network"
}
module "servers" {
count = 3
subnet = data.tfe_outputs.network.values.subnets.0
}
CODE EDITOR
Slide 34
Slide 34 text
Pattern:
Blue-green
deployment
Old Network New Network
Servers
Servers Servers
Modules in
same state
Module in
separate state
Module in
separate state
Load Balancer
50%
50%
Slide 35
Slide 35 text
Refactoring is inevitable.
1. Takes time and effort.
2. Go high-level to low-level.
3. Roll forward.
Slide 36
Slide 36 text
Rosemary Wang
@joatmon08
joatmon08.github.io
Thanks!