SlideShare a Scribd company logo
Terraform modules and some of best-
practices
Anton Babenko
@antonbabenko
March 2019
Anton Babenko
Terraform AWS 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/terraform-docs-as-pdf
antonbabenko/modules.tf-lambda — generate Terraform code from visual diagrams
www.terraform-best-practices.com
medium.com/@anton.babenko
@antonbabenko — Twitter, GitHub, Linkedin
Collection of open-source Terraform AWS modules supported by the community.
More than 2 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
Write, plan and manage infrastructure as code
www.terraform.io
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Google Cloud
Deployment Manager
Azure Resource
Manager
Terraform modules and some of best-practices - March 2019
+morethan100providers
Why Terraform and not AWS CloudFormation,
Azure ARM, Google Cloud Deployment Manager?
• Terraform manages 100+ 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 (IaC)
• Allows for composition and combination
• Supports parallel management of resources (graph, fast)
• Separates planning from execution (dry-run)
Terraform — universal tool for everything with an API
GSuite
Dropbox files and access
New Relic metrics
Datadog users and metrics
Jira issues
All Terraform providers
Let’s start!
Terraform modules
Modules in Terraform are self-contained
packages of Terraform configurations that are
managed as a group.
Resource modules
Create resources in a very flexible configuration
Open-source
Resource modules
Resource modules
Resource modules
Resource modules
Q: Why use resource modules instead of resources?
A: Resources can’t be versioned, but modules can.
Resource modules
Terraform modules and some of best-practices - March 2019
Infrastructure modules
Consist of resource modules
Enforce tags and company standards
Use preprocessors, jsonnet, cookiecutter
Infrastructure modules
Infrastructure modules
Infrastructure modules
Terraform modules and some of best-practices - March 2019
Types of Terraform modules
Resource modules (github.com/terraform-aws-modules , for eg)
Infrastructure modules
-	[	]	How	to	write	modules?	
-	[	]	How	to	call	modules?	
-	[	]	How	to	work	with	the	code?
Tip №0
Check Terraform Registry before writing resource modules
Hide implementation details
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Size
Size
https://github.com/mbtproject/mbt
Things to avoid in modules
Exception: logical providers (template, random, local, http, external)
Providers in modules — evil
Terraform modules and some of best-practices - March 2019
Provisioner — evil
Avoid provisioner in all resources
Provisioner — evil
Avoid provisioner in all resources
Provisioner — evil
Avoid provisioner even in EC2 resources
Provisioner — evil
Avoid provisioner even in EC2 resources
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
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	you	can	
		-	[x]	Avoid	providers	in	modules,	provisioners	
-	[	]	How	to	call	modules?	
-	[	]	How	to	work	with	the	code?
Call Terraform modules
Amount of resources and code keeps growing
How to organize and call?
How to orchestrate calls?
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)
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
Which way do you group your code?
All-in-one or 1-in-1?
Correct
MFA (Most Frequent Answer):
Somewhere in between
What kind of orchestration tool do you use?
-target
Makefile
…
Orchestration in Terraform
No really, do not try this at home!
Orchestration = Terragrunt
https://github.com/gruntwork-io/terragrunt/
Orchestration = Terragrunt
Orchestration = Terragrunt
Orchestration = Terragrunt
Orchestration = Terragrunt
tfvars can’t contain dynamic values :(
Orchestration = Terragrunt
Orchestration = Terragrunt
tfvars can’t contain dynamic values, so I
have fixed it :)
before_hook + shell script
https://github.com/antonbabenko/modules.tf-lambda/blob/master/templates/
terragrunt-common-layer/template/common/scripts/
update_dynamic_values_in_tfvars.sh
or use modules.tf
-	[x]	How	to	write	modules?	
-	[x]	How	to	call	modules?	
		-	[x]	1-in-1	works	beter	over	time	
		-	[x]	Orchestration	=	Terragrunt	
		-	[x]	Dynamic	values	in	tfvars	
-	[	]	How	to	work	with	the	code?
Add new features
Usually it is easy…
Create new or use existing
Create new or use existing
Create new or use existing
Create new or use existing
Work with lists
Work with lists
Work with lists
https://jsonnet.org/
Work with stateful lists
Work with stateful lists
Work with stateful lists
Work with stateful lists
Work with stateful lists
Integration
Integration
Auto-integration
Edge cases
Different AWS regions (version of S3 signature, EC2 ClassicLink,
IPv6)
Date of creation of AWS account
Limits on resources in AWS
Services and features availability
Avoid in Terraform
Not secret arguments should not be specified as command line
arguments => put them in tfvars
Reduce usage of "-target" and "-parallelism"
"Terraform workspaces" evil in=> separate by directories
Dependency hell in modules
-	[x]	How	to	write	modules?	
-	[x]	How	to	call	modules?	
-	[x]	How	to	work	with	the	code?	
		-	[x]	Lists	in	Terraform	0.11	can	be	painful	
		-	[x]	Perceive	Terraform	easier
-	[x]	How	to	write	modules?	
-	[x]	How	to	call	modules?	
-	[x]	How	to	work	with	the	code?	
-	[x]	Terraform	0.12	beta!
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
Summary
Write less and simpler (Terraform 0.12 won’t fix your code for you!)
Use existing modules and utilities
How to handle secrets in Terraform?
• Can you accept secrets to be saved in state file in plaintext? Probably not.
• AWS IAM password & access secret keys — use PGP as keybase.io
• AWS RDS — set dummy password and change after DB is created
• AWS RDS — use iam_database_authentication_enabled = true
• EC2 instance user-data + AWS KMS
• EC2 instance user-data + AWS System Manager’s Parameter Store
• AWS Secrets Manager
• https://github.com/opencredo/terrahelp
• Other options:
• Secure remote state location (S3 bucket policy, KMS key)
What are the tools/solutions out there?
• Terraform Registry — collection of public Terraform modules for common
infrastructure configurations for any provider — https://registry.terraform.io/
• Terraform linter to detect errors that can not be detected by `terraform plan`
— https://github.com/wata727/tflint
• Terraform version manager — https://github.com/kamatama41/tfenv
• A web dashboard to inspect Terraform States — https://github.com/
camptocamp/terraboard
• Jsonnet — The data templating language — http://jsonnet.org
• terraform-compliance - BDD style Terraform validation/compliancy check —
https://github.com/eerkunt/terraform-compliance
Atlantis — Start working on Terraform as a team
A unified workflow for collaborating on Terraform through GitHub, GitLab and
Bitbucket
https://www.runatlantis.io
https://github.com/terraform-aws-modules/terraform-aws-atlantis
Bonus
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
✓ cloudcraft.co — design, plan and visualize
✓ terraform-aws-modules — building blocks of AWS infrastructure
✓ Terraform — infrastructure as code
Infrastructure as code generator — from visual diagrams to Terraform
https://github.com/antonbabenko/modules.tf-lambda
Demo video: https://www.youtube.com/watch?v=F1Ax1zfZbiY
1. Go to cloudcraft.co
2. Sign up, sign in (free account)
3. Draw your AWS infrastructure
4. Click "Export"
5. Click "Terraform code export"
Try it yourself!
modules.tf — generated code
✓ Potentially ready-to-use Terraform configurations
✓ Suits best for bootstrapping
✓ Enforces Terraform best-practices
✓ Batteries included (terraform-aws-modules, terragrunt, pre-commit)
✓ 100% free and open-source (https://github.com/antonbabenko/
modules.tf-lambda)
✓ Released under MIT license
Thanks!
Questions?
github.com/antonbabenko
twitter.com/antonbabenko

More Related Content

PPTX
Building Repeatable Infrastructure using Terraform
PPTX
PDF
Introduction of Kubernetes - Trang Nguyen
PDF
Kubernetes Concepts And Architecture Powerpoint Presentation Slides
ODP
Introduction to Ansible
ODP
An Introduction To Jenkins
PPTX
Introduction to Kubernetes
PDF
Introduction to CICD
Building Repeatable Infrastructure using Terraform
Introduction of Kubernetes - Trang Nguyen
Kubernetes Concepts And Architecture Powerpoint Presentation Slides
Introduction to Ansible
An Introduction To Jenkins
Introduction to Kubernetes
Introduction to CICD

What's hot (20)

PPTX
Infrastructure-as-Code (IaC) using Terraform
PPT
presentation on Docker
PDF
Ansible
PPTX
Comprehensive Terraform Training
PPTX
MeetUp Monitoring with Prometheus and Grafana (September 2018)
PPTX
Kuberenetes - From Zero to Hero
PDF
Gitlab ci-cd
PPTX
K8s in 3h - Kubernetes Fundamentals Training
PPTX
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
PPTX
Azure kubernetes service (aks)
PPT
Docker introduction
PDF
Terraform -- Infrastructure as Code
PDF
Introduction to Google Compute Engine
PDF
DevOps & SRE at Google Scale
PPTX
Kubernetes for Beginners: An Introductory Guide
PPTX
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
PDF
Kubernetes Architecture | Understanding Kubernetes Components | Kubernetes Tu...
PPTX
Terraform Basics
PDF
Prometheus Overview
PPTX
Introduction to docker
Infrastructure-as-Code (IaC) using Terraform
presentation on Docker
Ansible
Comprehensive Terraform Training
MeetUp Monitoring with Prometheus and Grafana (September 2018)
Kuberenetes - From Zero to Hero
Gitlab ci-cd
K8s in 3h - Kubernetes Fundamentals Training
Docker Tutorial For Beginners | What Is Docker And How It Works? | Docker Tut...
Azure kubernetes service (aks)
Docker introduction
Terraform -- Infrastructure as Code
Introduction to Google Compute Engine
DevOps & SRE at Google Scale
Kubernetes for Beginners: An Introductory Guide
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
Kubernetes Architecture | Understanding Kubernetes Components | Kubernetes Tu...
Terraform Basics
Prometheus Overview
Introduction to docker
Ad

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

PDF
Building infrastructure as code using Terraform - DevOps Krakow
PDF
Terraform modules and best-practices - September 2018
PDF
Terraform modules and (some of) best practices
PDF
Terraform modules and (some of) best practices
PDF
Terraform Q&A - HashiCorp User Group Oslo
PDF
Manage any AWS resources with Terraform 0.12 - April 2020
PDF
Terraform 0.12 + Terragrunt
PDF
Terraform AWS modules and some best-practices - May 2019
PPTX
Terraform Abstractions for Safety and Power
PDF
Terraform vs Pulumi
PDF
Terraform Best Practices - DevOps Unicorns 2019
PPTX
Terraform
PDF
Terraform AWS modules and some best practices - September 2019
PDF
What you see is what you get for AWS infrastructure
PPTX
[AWSKRUG 아키텍처 모임] 세일즈부스트 인프라스트럭처 사례 공유
PPTX
Automatic Terraform Execution powerpoint
PDF
OSDC 2019 | Terraform best practices with examples and arguments by Anton Bab...
PPTX
Effective terraform
PPTX
Terraform training - Modules 🎒
PDF
What you see is what you get for AWS infrastructure
Building infrastructure as code using Terraform - DevOps Krakow
Terraform modules and best-practices - September 2018
Terraform modules and (some of) best practices
Terraform modules and (some of) best practices
Terraform Q&A - HashiCorp User Group Oslo
Manage any AWS resources with Terraform 0.12 - April 2020
Terraform 0.12 + Terragrunt
Terraform AWS modules and some best-practices - May 2019
Terraform Abstractions for Safety and Power
Terraform vs Pulumi
Terraform Best Practices - DevOps Unicorns 2019
Terraform
Terraform AWS modules and some best practices - September 2019
What you see is what you get for AWS infrastructure
[AWSKRUG 아키텍처 모임] 세일즈부스트 인프라스트럭처 사례 공유
Automatic Terraform Execution powerpoint
OSDC 2019 | Terraform best practices with examples and arguments by Anton Bab...
Effective terraform
Terraform training - Modules 🎒
What you see is what you get for AWS infrastructure
Ad

More from Anton Babenko (14)

PDF
Gotchas using Terraform in a secure delivery pipeline
PDF
Описание инфраструктуры с Terraform на будущее
PDF
Preview of Terraform 0.12 + modules.tf - Kiev HUG meetup
PDF
"I’ve heard you know infrastructure"
PDF
Terraform in deployment pipeline
PPTX
"Continuously delivering infrastructure using Terraform and Packer" training ...
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)
Gotchas using Terraform in a secure delivery pipeline
Описание инфраструктуры с Terraform на будущее
Preview of Terraform 0.12 + modules.tf - Kiev HUG meetup
"I’ve heard you know infrastructure"
Terraform in deployment pipeline
"Continuously delivering infrastructure using Terraform and Packer" training ...
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)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
Teaching material agriculture food technology
PDF
Approach and Philosophy of On baking technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectroscopy.pptx food analysis technology
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
sap open course for s4hana steps from ECC to s4
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
Teaching material agriculture food technology
Approach and Philosophy of On baking technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.

Terraform modules and some of best-practices - March 2019