SlideShare a Scribd company logo
Terraform modules and (some of)
best-practices
Anton Babenko
@antonbabenko
December 2018
Anton Babenko
Terraform AWS fanatic since 2015.
HUG, AWS, DevOps Norway, DevOpsDays Oslo…
I 💚 open-source:
terraform-community-modules + terraform-aws-modules
antonbabenko/pre-commit-terraform — auto-formatting code and documentation
antonbabenko/modules.tf-lambda — Terraform configurations from visual diagrams
www.terraform-best-practices.com
medium.com/@anton.babenko
@antonbabenko - Twitter, and many Slacks
Collection of open-source Terraform AWS modules supported by the community.
More than 2 millions downloads.
(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
Write, plan and manage infrastructure as code
www.terraform.io
Google Cloud
Deployment Manager
Azure Resource
Manager
Terraform modules and (some of) best practices
Plus100+moreproviders
Terraform — is a universal tool to manage
anything that has an API
GSuite
Dropbox files and access
New Relic metrics
Datadog users and metrics
Bugs in Jira
All Terraform providers
VPC, please!
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
Problems
Code size is growing
Complicated dependencies
Solution — Terraform modules
Terraform modules are self-contained
packages of Terraform configurations that are
managed as a group.
Resource modules
Only create resources in a very flexible way
Open-source
Resource modules
Terraform modules and (some of) best practices
Infrastructure modules
Consist of resource modules
Company standards and tags
Pre-processors, jsonnet, cookiecutter
Infrastructure modules
Terraform modules and (some of) best practices
Types of Terraform modules
Resource modules (terraform-aws-modules, for example)
Infrastructure modules
-	[	]	How	to	write	modules	
-	[	]	How	to	use	modules
Tip №0
Check Terraform Registry before writing new resource module.
Hide specifics
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
Size
Size
https://github.com/mbtproject/mbt
Things to avoid in Terraform modules
Exception: logical providers (template, random, local, http, external)
Providers in modules — bad
Terraform modules and (some of) best practices
Provisioner — bad
Avoid provisioners in all resources
Provisioner — bad
Avoid provisioners even inside EC2 resources
Provisioner — bad
Avoid provisioners even inside EC2 resources
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
null_resource provisioner — good
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
-	[x]	How	to	write	modules	
		-	[x]	Do	not	write,	if	possible	
		-	[x]	Do	not	use:	providers	and	provisioners	
-	[	]	How	to	use	modules
How to use Terraform modules
Many resources, many modules
How to organize and use them?
How to orchestrate them?
All in one
Good:
Declare variables and outputs in
fewer places
Bad:
Large blast radius
Everything is blocked at once
Not possible to specify dependenies
between modules (depends_on)
1-in-1
Good:
Small blast radius
Possible to chain calls
Faster and easier to work with
Bad:
Declare variables and outputs
in several places
How is it in your project?
"All in one" or 1-in-1 ?
Correct
Most frequent answer:
"somewhere in between" + "it depends"
What about orchestration in your project?
-target
Makefile
…
Orchestration in Terraform
Do not try this at home!
Orchestration = Terragrunt
https://github.com/gruntwork-io/terragrunt/
Orchestration = Terragrunt
Orchestration = Terragrunt
Orchestration = Terragrunt
Orchestration = Terragrunt
Orchestration = Terragrunt
tfvars can’t contain dynamic values :(
Orchestration = Terragrunt
tfvars can’t contain dynamic values,
so I fixed it :)
before_hook + shell-script
See this: https://github.com/antonbabenko/modules.tf-lambda/blob/master/
templates/terragrunt-common-layer/template/common/scripts/
update_dynamic_values_in_tfvars.sh
Or try it yourself by using cloudcraft.co
Edge cases
Different AWS regions (S3 signature, EC2 ClassicLink, IPv6)
Age of AWS accounts
Limits in AWS
Avoid in Terraform
Non-sensitive arguments in CLI. Put them in tfvars file.
• -target
• -parallelism
Terraform workspaces => Separate directory
Dependency hell in modules
-	[x]	How	to	write	modules	
-	[x]	How	to	use	modules	
		-	[x]	1-in-1	much	better	over	time	
		-	[x]	Orchestration	=	Terragrunt	
		-	[x]	Dynamic	values	in	tfvars	
-	[	]	What	is	next?
Terraform 0.12
HCL2 — simplified syntax
Loops ("for")
Dynamic blocks ("for_each")
Correct operations of comparison (… ? … : …)
Extended types in variables
Templates in string values
Links between all resources everywhere (depends_on)
Read more — https://www.hashicorp.com/blog/terraform-0-1-2-preview
Summary
Write less and simpler — Terraform 0.12 will not fix your code for you
Use existing modules and tools
BONUS
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
cloudcraft.co features
Manage AWS 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
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
Infrastructure as code generator — from visual diagrams to Terraform
✓ cloudcraft.co — design, plan and visualize
✓ terraform-aws-modules — building blocks of AWS infrastructure
✓ Terraform — infrastructure as code
modules.tf notes
✓ Available for all users: https://cloudcraft.co/
✓ Generates potentially ready-to-use Terraform configurations
✓ Suits best for bootstrapping
✓ Enforces Terraform best practices
✓ Batteries included (terraform-aws-modules, terragrunt, pre-commit, …)
✓ 100% free for all & open-source (https://github.com/antonbabenko/
modules.tf-lambda )
✓ Want to sponsor, or a sticker? Contact me.
modules.tf demo
Thanks to my supporters!
Cloudcraft — the best way to draw AWS diagrams
cloudcraft.co
Thanks!
Questions?
In progress — www.terraform-best-practices.com
github.com/antonbabenko
twitter.com/antonbabenko

More Related Content

PDF
Preview of Terraform 0.12 + modules.tf - Kiev HUG meetup
PDF
Gotchas using Terraform in a secure delivery pipeline
PDF
Terraform modules and some of best-practices - March 2019
PDF
Terraform 0.12 + Terragrunt
PDF
Terraform AWS modules and some best-practices - May 2019
PDF
Terraform modules and (some of) best practices
PDF
Terraform Best Practices - DevOps Unicorns 2019
PDF
What you see is what you get for AWS infrastructure
Preview of Terraform 0.12 + modules.tf - Kiev HUG meetup
Gotchas using Terraform in a secure delivery pipeline
Terraform modules and some of best-practices - March 2019
Terraform 0.12 + Terragrunt
Terraform AWS modules and some best-practices - May 2019
Terraform modules and (some of) best practices
Terraform Best Practices - DevOps Unicorns 2019
What you see is what you get for AWS infrastructure

What's hot (20)

PDF
Terraform modules and best-practices - September 2018
PDF
Building infrastructure as code using Terraform - DevOps Krakow
PPTX
PPTX
Terraform
PPTX
Terraform
PDF
Terraform Q&A - HashiCorp User Group Oslo
PPTX
Terraform on Azure
PDF
Terraform @Base
PPTX
Comprehensive Terraform Training
PPTX
Terraform
PDF
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
PDF
Scaling terraform
PPTX
Terraform Concepts
PPTX
Final terraform
PPTX
Infrastructure as Code: Introduction to Terraform
PPTX
Terraform
PDF
[Alibaba Cloud Singapore Community Meetup Webinar, 3 Sep 2020] Automate Your ...
PDF
Case Study: Using Terraform and Packer to deploy go applications to AWS
PPTX
Hybrid Clouds: Dancing with "Automated" Virtual Machines
PDF
Terraform in deployment pipeline
Terraform modules and best-practices - September 2018
Building infrastructure as code using Terraform - DevOps Krakow
Terraform
Terraform
Terraform Q&A - HashiCorp User Group Oslo
Terraform on Azure
Terraform @Base
Comprehensive Terraform Training
Terraform
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
Scaling terraform
Terraform Concepts
Final terraform
Infrastructure as Code: Introduction to Terraform
Terraform
[Alibaba Cloud Singapore Community Meetup Webinar, 3 Sep 2020] Automate Your ...
Case Study: Using Terraform and Packer to deploy go applications to AWS
Hybrid Clouds: Dancing with "Automated" Virtual Machines
Terraform in deployment pipeline
Ad

Similar to Terraform modules and (some of) best practices (20)

PDF
Terraform AWS modules and some best practices - September 2019
PDF
OSDC 2019 | Terraform best practices with examples and arguments by Anton Bab...
PDF
OSDC 2018 | Lifecycle of a resource. Codifying infrastructure with Terraform ...
PPTX
Terraform Modules Restructured
PPTX
Terraform modules restructured
PDF
Hashicorp-Terraform-Deep-Dive-with-no-Fear-Victor-Turbinsky-Texuna.pdf
PDF
Terraform-2.pdf
PPTX
Terraform in production - experiences, best practices and deep dive- Piotr Ki...
PDF
Terraform + ansible talk
PDF
Manage any AWS resources with Terraform 0.12 - April 2020
PPTX
Introduction to basics of Terraform.pptx
PPTX
Terraform - The Road to Self-Service
PPTX
"Continuously delivering infrastructure using Terraform and Packer" training ...
PPTX
terraform cours intéressant et super fort
PPTX
DelEx Conference: Jenkins+Terragrunt+Terraform eco-system
PDF
DevOps Braga #9: Introdução ao Terraform
PPTX
Terraform Modules and Continuous Deployment
PPTX
Terraform Abstractions for Safety and Power
PDF
The hitchhiker's guide to terraform your infrastructure
PDF
Introductory Overview to Managing AWS with Terraform
Terraform AWS modules and some best practices - September 2019
OSDC 2019 | Terraform best practices with examples and arguments by Anton Bab...
OSDC 2018 | Lifecycle of a resource. Codifying infrastructure with Terraform ...
Terraform Modules Restructured
Terraform modules restructured
Hashicorp-Terraform-Deep-Dive-with-no-Fear-Victor-Turbinsky-Texuna.pdf
Terraform-2.pdf
Terraform in production - experiences, best practices and deep dive- Piotr Ki...
Terraform + ansible talk
Manage any AWS resources with Terraform 0.12 - April 2020
Introduction to basics of Terraform.pptx
Terraform - The Road to Self-Service
"Continuously delivering infrastructure using Terraform and Packer" training ...
terraform cours intéressant et super fort
DelEx Conference: Jenkins+Terragrunt+Terraform eco-system
DevOps Braga #9: Introdução ao Terraform
Terraform Modules and Continuous Deployment
Terraform Abstractions for Safety and Power
The hitchhiker's guide to terraform your infrastructure
Introductory Overview to Managing AWS with Terraform
Ad

More from Anton Babenko (11)

PDF
What you see is what you get for AWS infrastructure
PDF
Описание инфраструктуры с Terraform на будущее
PDF
"I’ve heard you know infrastructure"
PDF
Continuous delivery in AWS
PDF
Tools exist for a reason
PPTX
AWS CodeDeploy - basic intro
PPTX
Managing AWS infrastructure using CloudFormation
PPTX
Designing for elasticity on AWS - 9.11.2015
PPTX
Recap of AWS re:invent 2015
PPTX
Designing for elasticity on AWS
PDF
Build & deploy PHP application (intro level)
What you see is what you get for AWS infrastructure
Описание инфраструктуры с Terraform на будущее
"I’ve heard you know infrastructure"
Continuous delivery in AWS
Tools exist for a reason
AWS CodeDeploy - basic intro
Managing AWS infrastructure using CloudFormation
Designing for elasticity on AWS - 9.11.2015
Recap of AWS re:invent 2015
Designing for elasticity on AWS
Build & deploy PHP application (intro level)

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Spectroscopy.pptx food analysis technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
KodekX | Application Modernization Development
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
Advanced methodologies resolving dimensionality complications for autism neur...
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectroscopy.pptx food analysis technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
MYSQL Presentation for SQL database connectivity
KodekX | Application Modernization Development
Reach Out and Touch Someone: Haptics and Empathic Computing
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?

Terraform modules and (some of) best practices