SlideShare a Scribd company logo
Terraform AWS modules and some best-
practices
Anton Babenko
@antonbabenko
May 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/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 email: anton@antonbabenko.com
LinkedIn: https://www.linkedin.com/in/antonbabenko
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
@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
@antonbabenko
@antonbabenko
@antonbabenko
@antonbabenko
@antonbabenko
Terraform AWS modules and some best-practices - May 2019
Google Cloud
Deployment Manager
Azure Resource
Manager
@antonbabenko
@antonbabenko
+morethan100providers
@antonbabenko
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
Orchestration, not merely configuration
Supports parallel management of resources (graph, fast)
Separates planning from execution (dry-run)
@antonbabenko
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
Let’s begin — everything fits in main.tf
@antonbabenko
@antonbabenko
Project grows — main.tf:
20+ resources and data sources
@antonbabenko
Why?
@antonbabenko
Resources, regions, providers, …
@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
Solution — Terraform modules
@antonbabenko
Modules in Terraform are self-contained
packages of Terraform configurations that are
managed as a group.
@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
Consist of resource modules
Enforce tags and company standards
Use preprocessors, jsonnet, cookiecutter
@antonbabenko
Infrastructure modules
@antonbabenko
Infrastructure modules
@antonbabenko
Infrastructure modules
@antonbabenko
@antonbabenko
Types of Terraform modules
Resource modules (github.com/terraform-aws-modules , for eg)
Infrastructure modules
@antonbabenko
-	[	]	How	to	write	modules?	
-	[	]	How	to	call	modules?	
-	[	]	How	to	work	with	the	code?
@antonbabenko
Tip №0
Check Terraform Registry before writing resource modules
@antonbabenko
Hide implementation details
@antonbabenko
@antonbabenko
@antonbabenko
Size
@antonbabenko
Size
https://github.com/mbtproject/mbt
@antonbabenko
Things to avoid in modules
@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
-	[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?
@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
@antonbabenko
Defined project scope
Different types of developers
involved *
Code reuse is encouraged
(across organization and
environments)
Use Terragrunt
What about Terraform workspaces?
@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
What kind of orchestration method do you use?
-target
Makefile
…
@antonbabenko
Orchestration in Terraform
@antonbabenko
No really, do not try this at home!
@antonbabenko
Orchestration = Terragrunt
https://github.com/gruntwork-io/terragrunt/
@antonbabenko
Orchestration = Terragrunt
@antonbabenko
Orchestration = Terragrunt
@antonbabenko
Orchestration = Terragrunt
@antonbabenko
Orchestration = Terragrunt
@antonbabenko
tfvars can’t contain dynamic values :(
Orchestration = Terragrunt
@antonbabenko
subnet_id = "???"
Orchestration = Terragrunt
tfvars can’t contain dynamic values, so I
have fixed it :)
@antonbabenko
subnet_id = "???"
before_hook + shell script Go binary
https://github.com/antonbabenko/tfvars-annotations — Update values in
terraform.tfvars using annotations (WIP)
or take a look at modules.tf
@antonbabenko
Orchestration = Terragrunt
@antonbabenko
subnet_id = "" # tfvars:terragrunt_output.vpc.public_subnets
Orchestration = Terragrunt
@antonbabenko
subnet_id = ["subnet-1a2b3c4d"] # tfvars:terragrunt_output.vpc.public_subnets
-	[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?
@antonbabenko
Work with stateless lists
@antonbabenko
@antonbabenko
Work with stateless lists
@antonbabenko
Work with stateless lists
https://jsonnet.org/
Work with stateful lists
@antonbabenko
Work with stateful lists
@antonbabenko
Work with stateful lists
@antonbabenko
Work with stateful lists
@antonbabenko
Work with stateful lists
@antonbabenko
Integration
@antonbabenko
Integration
@antonbabenko
Auto-integration
@antonbabenko
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
@antonbabenko
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
@antonbabenko
-	[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
@antonbabenko
Summary
Write less and simpler (Terraform 0.12 won’t fix your code for you!)
Use existing modules and utilities
@antonbabenko
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)
@antonbabenko
What are the tools/solutions out there?
• Terraform Registry (https://registry.terraform.io/) — collection of public Terraform
modules for common infrastructure configurations for any provider.
• 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
@antonbabenko
Atlantis — Start working on Terraform as a team
A unified workflow for collaborating on Terraform through GitHub, GitLab and Bitbucket
https://www.runatlantis.io
@antonbabenko
Bonus
Terraform AWS modules and some best-practices - May 2019
Terraform AWS modules and some best-practices - May 2019
Terraform AWS modules and some best-practices - May 2019
Terraform AWS modules and some best-practices - May 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, tfvars-
annotations, 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

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

What's hot (20)

PDF
Preview of Terraform 0.12 + modules.tf - Kiev HUG meetup
PDF
What you see is what you get for AWS infrastructure
PDF
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
PPTX
Terraform on Azure
PDF
Terraform @Base
PPTX
Terraform
PDF
Docker and Fluentd
PPTX
Terraform
PPT
Step-by-Step Introduction to Apache Flink
PDF
Red Hat Nordics 2020 - Apache Camel 3 the next generation of enterprise integ...
PPTX
Writing New Relic Plugins: NSQ
PPTX
Building Docker Containers @ Scale
PDF
Bootstrapping Puppet and Application Deployment - PuppetConf 2013
PDF
Kubernetes API - deep dive into the kube-apiserver
PDF
Sheep it
PDF
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
PDF
Terraform 101: What's infrastructure as code?
PPTX
Integrating microservices with apache camel on kubernetes
PDF
Extend and build on Kubernetes
PDF
Best Practices for Middleware and Integration Architecture Modernization with...
Preview of Terraform 0.12 + modules.tf - Kiev HUG meetup
What you see is what you get for AWS infrastructure
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
Terraform on Azure
Terraform @Base
Terraform
Docker and Fluentd
Terraform
Step-by-Step Introduction to Apache Flink
Red Hat Nordics 2020 - Apache Camel 3 the next generation of enterprise integ...
Writing New Relic Plugins: NSQ
Building Docker Containers @ Scale
Bootstrapping Puppet and Application Deployment - PuppetConf 2013
Kubernetes API - deep dive into the kube-apiserver
Sheep it
Testing for Ops: Going Beyond the Manifest - PuppetConf 2013
Terraform 101: What's infrastructure as code?
Integrating microservices with apache camel on kubernetes
Extend and build on Kubernetes
Best Practices for Middleware and Integration Architecture Modernization with...
Ad

Similar to Terraform AWS modules and some best-practices - May 2019 (20)

PDF
OSDC 2019 | Terraform best practices with examples and arguments by Anton Bab...
PDF
Manage any AWS resources with Terraform 0.12 - April 2020
PDF
Terraform modules and best-practices - September 2018
PDF
DevOps Days Kyiv 2019 -- What you see is what you get for AWS // Anton Babenko
PPTX
Terraform training 🎒 - Basic
PDF
Instant download Terraform in Depth (MEAP V01) Robert Hafner pdf all chapter
PDF
Terraform Q&A - HashiCorp User Group Oslo
PDF
Terraform vs Pulumi
PDF
My Hashitalk Indonesia April 2024 Presentation
PDF
DevOpsDaysRiga 2018: Anton Babenko - What you see is what you get… for AWS in...
PDF
Introduction of eBPF - 時下最夯的Linux Technology
PDF
Git ops & Continuous Infrastructure with terra*
PDF
Building infrastructure as code using Terraform - DevOps Krakow
PDF
Collaborative Terraform with Atlantis
PDF
OSDC 2018 - Distributed monitoring
PDF
OSDC 2018 | Distributed Monitoring by Gianluca Arbezzano
PPTX
What's New in Docker - February 2017
PDF
Container orchestration on_aws
PDF
Deep Dive into Futures and the Parallel Programming Library
PDF
Metaflow: The ML Infrastructure at Netflix
OSDC 2019 | Terraform best practices with examples and arguments by Anton Bab...
Manage any AWS resources with Terraform 0.12 - April 2020
Terraform modules and best-practices - September 2018
DevOps Days Kyiv 2019 -- What you see is what you get for AWS // Anton Babenko
Terraform training 🎒 - Basic
Instant download Terraform in Depth (MEAP V01) Robert Hafner pdf all chapter
Terraform Q&A - HashiCorp User Group Oslo
Terraform vs Pulumi
My Hashitalk Indonesia April 2024 Presentation
DevOpsDaysRiga 2018: Anton Babenko - What you see is what you get… for AWS in...
Introduction of eBPF - 時下最夯的Linux Technology
Git ops & Continuous Infrastructure with terra*
Building infrastructure as code using Terraform - DevOps Krakow
Collaborative Terraform with Atlantis
OSDC 2018 - Distributed monitoring
OSDC 2018 | Distributed Monitoring by Gianluca Arbezzano
What's New in Docker - February 2017
Container orchestration on_aws
Deep Dive into Futures and the Parallel Programming Library
Metaflow: The ML Infrastructure at Netflix
Ad

More from Anton Babenko (12)

PDF
Описание инфраструктуры с Terraform на будущее
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)
Описание инфраструктуры с Terraform на будущее
"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)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
Understanding_Digital_Forensics_Presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
MIND Revenue Release Quarter 2 2025 Press Release
sap open course for s4hana steps from ECC to s4
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

Terraform AWS modules and some best-practices - May 2019