Terraform Best Practices
Anton Babenko
@antonbabenko
October 2019
Anton Babenko
AWS Community Hero / Certified Terraform fanatic since 2015
Organiser of HashiCorp UG, AWS UG, DevOps Norway, DevOpsDays Oslo
I 💚 open-source:
terraform-community-modules + terraform-aws-modules
antonbabenko/pre-commit-terraform — clean code and documentation
antonbabenko/tfvars-annotations — update terraform.tfvars using annotations
antonbabenko/modules.tf-lambda — generate Terraform code from visual diagrams
antonbabenko/terragrunt-reference-architecture — Terragrunt reference architecture
www.terraform-best-practices.com
medium.com/@anton.babenko
@antonbabenko — Twitter, GitHub, Linkedin
What do I do?
All-things Terraform + AWS + DevOps
Consulting
Workshops
Trainings
Mentorship
My interview: https://medium.com/@anton.babenko/my-terraform-aws-journey-hashitimes-interview-73d1b542fcc0
My email: anton@antonbabenko.com
LinkedIn: https://www.linkedin.com/in/antonbabenko
Collection of open-source Terraform AWS modules supported by the community.
More than 5 mil. downloads since September 2017.
(VPC, Autoscaling, RDS, Security Groups, ELB, ALB, Redshift, SNS, SQS, IAM, EKS, ECS…)
github.com/terraform-aws-modules
registry.terraform.io/modules/terraform-aws-modules
@antonbabenko
Cloudcraft.co — the best way to draw AWS diagrams
@antonbabenko
cloudcraft.co features
• Manage components in browser (EC2 instances, autoscaling groups, RDS, etc)
• Connect components
• Import live AWS infrastructure
• Calculate the budget
• Share link to a blueprint
• Export as image
• Embed drawing to wiki, Confluence, etc
@antonbabenko
Infrastructure as code makes DevOps possible
Key benefits:
• Treat infrastructure like application code
• Always know what changed
• Validate infrastructure before deployment
https://dzone.com/articles/infrastructure-as-code-the-benefits @antonbabenko
Tool for building, changing and versioning infrastructure safely and efficiently.
www.terraform.io
@antonbabenko
Terraform 0.12
@antonbabenko
@antonbabenko
@antonbabenko
@antonbabenko
Configuration
Management Tools
Google Cloud
Deployment Manager
Azure Resource
Manager
@antonbabenko
+morethan250providers
@antonbabenko
Configuration
Management Tools
Why Terraform and not AWS CloudFormation,
Azure ARM, Google Cloud Deployment Manager?
@antonbabenko
Terraform supports 250+ providers, has easier syntax (HCL), has native support for
modules and remote states, has teamwork related features, is an open-source project
Provides a high-level abstraction of infrastructure, unifies the view of resources
Supports the modern datacenter (IaaS, PaaS, SaaS)
Separates planning from execution (dry-run)
Provides a workflow which is technology agnostic
Manages anything with an API
Terraform — universal tool for everything with an API
Google G Suite
Dropbox files and access
New Relic metrics
Datadog users and metrics
Jira issues
Minecraft, or even order Domino’s pizza
All Terraform providers — https://www.terraform.io/docs/providers/index.html
@antonbabenko
Terraform 0.12
HCL2 — simplified syntax
Loops ("for")
Dynamic blocks ("for_each")
Correct conditional operators (… ? … : …)
Extended types of variables
Templates in values
Links between resources are supported (depends_on everywhere)
Read more — https://www.hashicorp.com/blog/announcing-terraform-0-1-2-beta
@antonbabenko
Let’s start!
@antonbabenko
"Let’s manage AWS network stack"
@antonbabenko
@antonbabenko
@antonbabenko
@antonbabenko
@antonbabenko
@antonbabenko
main.tf:
10-20 Kb
300+ LOC
@antonbabenko
Emerging issues
Code size is increasing
Dependencies between resources become complicated
@antonbabenko
Terraform modules
@antonbabenko
– What is Terraform module?
“Modules in Terraform are self-contained packages
of Terraform configurations that are managed as a group.”
@antonbabenko
Types of Terraform modules
Resource modules (github.com/terraform-aws-modules , for eg)
Infrastructure modules
@antonbabenko
Resource modules
Create resources in a very flexible configuration
Open-source
@antonbabenko
Resource modules
@antonbabenko
Resource modules
@antonbabenko
Resource modules
@antonbabenko
Resource modules
@antonbabenko
Would you use Terraform module to manage AWS
EC2 security group?
@antonbabenko
@antonbabenko
Would you use Terraform module to manage AWS
EC2 security group?
Yes :)
@antonbabenko
Infrastructure modules
Also known as "curated modules" and "company-wide modules"
Consist of resource modules
Enforce tags and company standards
In 0.11 — use preprocessors, jsonnet, cookiecutter
In 0.12 — may implement complex logic (conditions, loops, nested
blocks)
@antonbabenko
Infrastructure modules
@antonbabenko
Infrastructure modules
@antonbabenko
Infrastructure modules
@antonbabenko
@antonbabenko
Terraform modules: do and don’t
@antonbabenko
Terraform Registry
Check registry.terraform.io before writing any Terraform modules
@antonbabenko
Very Frequent Problem:
Terraform modules can’t be re-used,
because they are very specific
@antonbabenko
Exception: logical providers (template, random, local, http, external)
Providers in modules — evil
@antonbabenko
@antonbabenko
Provisioner — evil
Avoid provisioner in all resources
@antonbabenko
Provisioner — evil
Avoid provisioner in all resources
@antonbabenko
Provisioner — evil
Avoid provisioner even in EC2 resources
@antonbabenko
Provisioner — evil
Avoid provisioner even in EC2 resources
@antonbabenko
@antonbabenko
@antonbabenko
null_resource provisioner — good
@antonbabenko
Traits of good Terraform modules
Documentation and examples
Feature rich
Sane defaults
Clean code
Tests
Read more: http://bit.ly/common-traits-in-terraform-modules
@antonbabenko
Are Terraform modules enough?
@antonbabenko
No, Terraform module is the beginning.
@antonbabenko
-	[x]	Terraform	modules	
-	[	]	How	to	structure	Terraform	configurations?	
-	[	]	Terraform	workspaces	
-	[	]	Terraform	0.12
@antonbabenko
How to structure Terraform
configurations? How to call them?
@antonbabenko
Call Terraform modules
Use Terraform modules, because amount of resources and code is
increasing
How to organize Terraform configurations and invoke them?
How to orchestrate modules?
@antonbabenko
All-in-one
Good:
Declare variables and outputs in
fewer places
Bad:
Large blast radius
Everything is blocked at once
Impossible to specify
dependencies between modules
(depends_on)
@antonbabenko
1-in-1
Good:
Smaller blast radius
Possible to join invocation
Easier and faster to work with
Bad:
Declare variables and outputs in
more places
@antonbabenko
Which way do you group your code?
All-in-one or 1-in-1?
@antonbabenko
All-in-one 1-in-1
or
@antonbabenko
Correct
MFA (Most Frequent Answer):
Somewhere in between
@antonbabenko
All-in-one
Undefined project scope
Fast prototyping and initial
development phase
Small number of resources &
developers
Tightly connected resources
1-in-1
Defined project scope
Different types of developers
can be involved
Code reuse is encouraged
(across organization and
environments)
Use Terragrunt
@antonbabenko
What about Terraform workspaces?
@antonbabenko
– What is a Terraform workspace?
“Workspaces allow the use of multiple states with a single
configuration directory.”
@antonbabenko
Problems with Terraform workspaces
Terraform Workspaces aren’t infrastructure-as-code friendly. You
can’t answer straight from the code:
"How many workspaces do you have?"
"What infrastructure has been deployed in workspaceX?"
"What is the difference between workspaceX and workspaceY?"
Introducing complexity almost in all cases.
@antonbabenko
Solution — use re-usable modules
instead of workspaces
@antonbabenko
-	[x]	Terraform	modules	-	Yes,	must-have!	
-	[x]	How	to	structure	Terraform	configurations?	
		-	[x]	One-in-one	+	terragrunt	
-	[x]	Terraform	workspaces	-	No,	please!	
		-	[x]	More	directories	are	easier	to	work	with	
-	[	]	Terraform	0.12	-	How	it	should	help	us?	
@antonbabenko
Summary
Terraform 0.12
What does it mean for us?
@antonbabenko
Who are you?
Terraform users vs developers
@antonbabenko
Types of Terraform users
Terraform developers
Terraform users (everyone else)
@antonbabenko
Terraform developers
Write and support Terraform modules
Implement company’s standards (security, encryption, integrations)
Maintain reference architectures
@antonbabenko
Terraform users (everyone)
Use Terraform modules by specifying correct values
Domain experts
May not have "Terraform" in LinkedIn profile
@antonbabenko
Terraform 0.12 for developers
DevOps&Terraform developers
Allow to implement flexible/dynamic/reusable Terraform modules
@antonbabenko
Terraform 0.12 for users
Terraform users
Like HCL2 lightweight syntax more
@antonbabenko
-	[x]	Terraform	0.12	-	Awesome!	
		-	[x]	90%	of	benefits	for	Terraform	developers	
		-	[x]	10%	of	benefits	for	Terraform	users
@antonbabenko
Summary
Thanks!
Questions?
github.com/antonbabenko
twitter.com/antonbabenko

Terraform Best Practices - DevOps Unicorns 2019