SlideShare a Scribd company logo
1 of 28
Download to read offline
Terraform Infrastructure as Code
Best Practices and Common Mistakes
Given by Derek C. Ashmore
DevOps West 2021
June 9, 2021
©2020 Derek C. Ashmore, All Rights Reserved 1
Who am I?
• Professional Geek
since 1987
• AWS since 2010
• Azure since 2017
• Terraform since the
0.5.x days
• Specialties
• Application
Transformation
• Infrastructure
Automation
• Yes – I still code!
©2021 Derek C. Ashmore, All Rights Reserved 2
Discussion Resources
• This slide deck
– https://www.slideshare.net/derekashmore/presentations
• Sample code on my Github
– https://github.com/Derek-Ashmore/
• Slide deck has hyper-links!
– Don’t bother writing down URLs
• Assumptions
– You have used Terraform (at least played with it)
– You know basic functionality
• Keep track of your questions – live Q&A at the end
©2021 Derek C. Ashmore, All Rights Reserved 3
Agenda
Intro and
Level Set
Environment
Management
Modularity
Summary /
Q&A
©2021 Derek C. Ashmore, All Rights Reserved 4
Terraform Terminology
• Configuration vs Module
– Terraform Module designed for
reuse
– Terraform configuration is the
outer layer
• TFVars files
– Provides variable input for an
environment
• Like properties file
– Used with the –var-file
option
©2021 Derek C. Ashmore, All Rights Reserved
• Resources
– Controls a cloud asset
• Data lookup
– Searches for cloud assets
• Variables
– Different values per context
• Function and Expressions
– Built-ins that
gather/manipulate values
How Terraform Works
• Terraform is
“Declarative”
– Like SQL
• Reads all files with
extension .tf
– Figures out execution
order
• Supports variables and
functions
• Plugin architecture
– Supports many clouds
and products
©2021 Derek C. Ashmore, All Rights Reserved
Agenda
Intro and
Level Set
Environment
Management
Modularity
Summary /
Q&A
©2021 Derek C. Ashmore, All Rights Reserved 7
Project Structure
• Separate Configurations
from Modules
– Documents what’s
designed for reuse and
what is not
• TFVars files provide
environment specifics
– All environments use the
same automation
– Easy to add
environments
– Different back-end state
per environment
©2021 Derek C. Ashmore, All Rights Reserved
Project Structure Anti-Pattern
• Separate configurations per
environment
• The good
– Code is often simpler
– Easier to add/subtract
capabilities per environment
– Separate state if using local
file system default
• The bad
– Has code duplication
– Harder to establish new
environments
– Environments can be
inconsistent
• Works in dev, but not prod
©2021 Derek C. Ashmore, All Rights Reserved
Making Environment Differences Configurable
• Use Conditionals
– No “If-Then”
capability
– Boolean indicators
• Resources using
count
• Dynamic blocks
©2021 Derek C. Ashmore, All Rights Reserved
Optional configuration through ‘try’
• Use for complex
inputs with optional
fields
• Try suppresses
exceptions
• Specify Terraform
defaults with null
©2021 Derek C. Ashmore, All Rights Reserved
Environment Management Best Practices
• Always run Terraform through tooling, not on your desktop
– CI/CD Tools such as Jenkins or Terraform Cloud
– Benefits
• Audit history
• Terraform and provider version control
• Consistent runtime environment
• Always require a plan before the apply
– Require approval step before going on to the apply
• Utilize cloud security constructs
– AWS IAM instance roles for Jenkins agents
– Azure Managed Identities for Jenkins or Azure DevOps agents
• Always use back-end state
©2021 Derek C. Ashmore, All Rights Reserved
Agenda
Intro and
Level Set
Environment
Management
Modularity
Summary /
Q&A
©2021 Derek C. Ashmore, All Rights Reserved 13
Terraform Usage Evolution
• In the beginning
– Use Source Control
– Use Back-end state
• As #Coders grows
– Feature branches
– CI/CD Pipelines
• As #Configurations grows
– Separate repo for modules
• Or Terraform registry
– Implement versioning
• Never use main/master!
• Further reading
©2021 Derek C. Ashmore, All Rights Reserved
Feature Branching
• DevOps Team Discipline is Key
• Feature Branches
– Never edit main/master directly!
– Update using Pull Requests
• Should live less than one day!
– Single targeted enhancement
– One developer only
– Long-lived branches prone to merge
conflicts
– Prefer rebase to merge
• Further reading
©2021 Derek C. Ashmore, All Rights Reserved
CI/CD Pipelines
• Provides consistent runtime
environment
– Terraform version
– Cloud security policy
• Audit history / Admin security
• Pipeline approvals
– Force Plan execution
– Force manual approval before
apply or destroy
– Automatic “Apply” nullifies benefit
of doing the plan
©2021 Derek C. Ashmore, All Rights Reserved
Modularity Anti-Patterns
• All of these examples come from
the field
– Module creation before it’s
needed
– Modules that only contain one
resource
– Inappropriate Data lookups in
modules
– Undocumented modules
– Use modules referencing
main/master
©2021 Derek C. Ashmore, All Rights Reserved
Module creation before it’s needed
• Should have at least two
consumers before module is
created
• Classic YAGNI
• Hard to track down consumers
after release
• Impossible to remove unused
modules
©2021 Derek C. Ashmore, All Rights Reserved
Modules that only contain one resource
• Amounts to a thin proxy
– No value-add
• Unnecessary complexity
• Not as well documented as the
underlying Terraform resource
• Every module should have at
least two resources!
©2021 Derek C. Ashmore, All Rights Reserved
Inappropriate Data lookups in modules
• Data lookups fail if
nothing is found
– Error if the consumer
configuration creates
the resource
• Makes assumptions
about execution
context
• Data lookups belong in
configurations, not
modules, as they do
know context
©2021 Derek C. Ashmore, All Rights Reserved
Undocumented Modules
• Force consumers to
read/understand module code
– Costs them time
• Makes it hard to use
• All modules should have a
README.md:
– Example module call
– Release Notes
– Variable list
– Output list
©2021 Derek C. Ashmore, All Rights Reserved
Use modules referencing main/master
• Always consume referencing
specific versions
– Version upgrades are planned
work
• Source code
©2021 Derek C. Ashmore, All Rights Reserved
• Recipe for unplanned work
– Consumers can break
unexpectedly when modules
change
• Always version modules
Agenda
Intro and
Level Set
Environment
Management
Modularity
Summary /
Q&A
©2021 Derek C. Ashmore, All Rights Reserved 23
Secrets Handling
• Secrets include
– Credentials (account/password)
– SSL Certificates
– SSH Keys
• Manage secrets separately
– Digital Vault
• Terraform looks the secret up
– CI/CD Pipeline “Secret” variable
• Anti-pattern: Terraform generating
password
– Easy to get out of sync with reality
– Secrets have different life-cycle
©2021 Derek C. Ashmore, All Rights Reserved
Simplicity is Key
• Eliminate unused variables
– Always remove dead code
• Don’t replicate derived values
– Derive once in locals and use
• Variable defaults
– Inappropriate defaults common
• Environment-specific names
• Globally unique names
©2021 Derek C. Ashmore, All Rights Reserved
Avoid the Hammer and Nail Problem
• Terraform is good for:
– Creating cloud assets
– Changing attributes on cloud
assets
©2021 Derek C. Ashmore, All Rights Reserved
• Terraform is not good for:
– Maintaining content on cloud
assets
• VM configuration management
– Use Ansible, Chef, etc.
• Image pipelines
– Use Packer
– Don’t “remote control”
• Use Terraform to execute Ansible
or Packer
Session Summary
• How to structure projects
• Manage environments using
tfvars
– Not configurations
• How to make resources
optional
• Use CI/CD tooling
• Appropriate uses for Terraform
©2021 Derek C. Ashmore, All Rights Reserved
• Module Anti-Patterns
– Module creation before it’s
needed
– Modules that only contain one
resource
– Inappropriate Data lookups in
modules
– Undocumented modules
– Use modules referencing
main/master
Thank you!
• Derek Ashmore:
– Blog: www.derekashmore.com
– LinkedIn: www.linkedin.com/in/derekashmore
• Connect Invites from attendees welcome
– Twitter: https://twitter.com/Derek_Ashmore
– GitHub: https://github.com/Derek-Ashmore
– Book: http://dvtpress.com/
• Please fill out the survey form!
• Click the “Subsessions” tab for live Q&A
©2021 Derek C. Ashmore, All Rights Reserved 28

More Related Content

What's hot

Flintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test LabFlintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test LabTechWell
 
Delivering Mobile Apps That Perform
Delivering Mobile Apps That PerformDelivering Mobile Apps That Perform
Delivering Mobile Apps That PerformRuben Goncalves
 
Monitoring Cloud/Virtual/Physical IT Infrastructures
Monitoring Cloud/Virtual/Physical IT InfrastructuresMonitoring Cloud/Virtual/Physical IT Infrastructures
Monitoring Cloud/Virtual/Physical IT InfrastructuresJohnnie Burke-Gaffney
 
Managing and Monitoring Virtual/Cloud/Physical Infrastructures
Managing and Monitoring Virtual/Cloud/Physical InfrastructuresManaging and Monitoring Virtual/Cloud/Physical Infrastructures
Managing and Monitoring Virtual/Cloud/Physical InfrastructuresJohnnie Burke-Gaffney
 
[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric Cloud[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric CloudPerforce
 
Yeoman - Santa Barbara JavaScript Meetup
Yeoman - Santa Barbara JavaScript MeetupYeoman - Santa Barbara JavaScript Meetup
Yeoman - Santa Barbara JavaScript MeetupTim Doherty
 
BOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demandBOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demandDevOps Enterprise Summit
 
Calculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the CloudCalculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the CloudAcquia
 
Building azure applications ireland
Building azure applications irelandBuilding azure applications ireland
Building azure applications irelandMichael Meagher
 
be the captain of your connections deployment
be the captain of your connections deploymentbe the captain of your connections deployment
be the captain of your connections deploymentSharon James
 
Automatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI PlanningAutomatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI PlanningHiroshi Wada
 
Embracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at NetflixEmbracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at NetflixJosh Evans
 
Java Application Servers Are Dead!
Java Application Servers Are Dead!Java Application Servers Are Dead!
Java Application Servers Are Dead!Eberhard Wolff
 
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...DevOps Enterprise Summit
 
Extending Availability to the Cloud
Extending Availability to the CloudExtending Availability to the Cloud
Extending Availability to the CloudYoong Seng Lai
 
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...The Linux Foundation
 
v10 of Backup & Replication: a sneak peek
v10 of Backup & Replication: a sneak peekv10 of Backup & Replication: a sneak peek
v10 of Backup & Replication: a sneak peekXylos
 
Software Architecture
Software ArchitectureSoftware Architecture
Software ArchitectureYoav Avrahami
 
Lucas Gravley - HP - Self-Healing And Monitoring in a DevOps world
Lucas Gravley - HP - Self-Healing And Monitoring in a DevOps worldLucas Gravley - HP - Self-Healing And Monitoring in a DevOps world
Lucas Gravley - HP - Self-Healing And Monitoring in a DevOps worldDevOps Enterprise Summit
 

What's hot (20)

Flintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test LabFlintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test Lab
 
Delivering Mobile Apps That Perform
Delivering Mobile Apps That PerformDelivering Mobile Apps That Perform
Delivering Mobile Apps That Perform
 
Monitoring Cloud/Virtual/Physical IT Infrastructures
Monitoring Cloud/Virtual/Physical IT InfrastructuresMonitoring Cloud/Virtual/Physical IT Infrastructures
Monitoring Cloud/Virtual/Physical IT Infrastructures
 
Managing and Monitoring Virtual/Cloud/Physical Infrastructures
Managing and Monitoring Virtual/Cloud/Physical InfrastructuresManaging and Monitoring Virtual/Cloud/Physical Infrastructures
Managing and Monitoring Virtual/Cloud/Physical Infrastructures
 
[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric Cloud[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric Cloud
 
Yeoman - Santa Barbara JavaScript Meetup
Yeoman - Santa Barbara JavaScript MeetupYeoman - Santa Barbara JavaScript Meetup
Yeoman - Santa Barbara JavaScript Meetup
 
BOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demandBOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demand
 
Calculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the CloudCalculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the Cloud
 
Building azure applications ireland
Building azure applications irelandBuilding azure applications ireland
Building azure applications ireland
 
be the captain of your connections deployment
be the captain of your connections deploymentbe the captain of your connections deployment
be the captain of your connections deployment
 
Automatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI PlanningAutomatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI Planning
 
Embracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at NetflixEmbracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at Netflix
 
Java Application Servers Are Dead!
Java Application Servers Are Dead!Java Application Servers Are Dead!
Java Application Servers Are Dead!
 
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
 
Extending Availability to the Cloud
Extending Availability to the CloudExtending Availability to the Cloud
Extending Availability to the Cloud
 
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
 
v10 of Backup & Replication: a sneak peek
v10 of Backup & Replication: a sneak peekv10 of Backup & Replication: a sneak peek
v10 of Backup & Replication: a sneak peek
 
Software Architecture
Software ArchitectureSoftware Architecture
Software Architecture
 
Lucas Gravley - HP - Self-Healing And Monitoring in a DevOps world
Lucas Gravley - HP - Self-Healing And Monitoring in a DevOps worldLucas Gravley - HP - Self-Healing And Monitoring in a DevOps world
Lucas Gravley - HP - Self-Healing And Monitoring in a DevOps world
 
DevOps in Silos
DevOps in SilosDevOps in Silos
DevOps in Silos
 

Similar to Terraform best-practices-and-common-mistakes-dev ops-west-2021

Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...
Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...
Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...Derek Ashmore
 
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018Derek Ashmore
 
Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06
Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06
Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06Derek Ashmore
 
Implementing DevOps Automation Best Practices and Common Mistakes
Implementing DevOps AutomationBest Practices and Common MistakesImplementing DevOps AutomationBest Practices and Common Mistakes
Implementing DevOps Automation Best Practices and Common MistakesDerek Ashmore
 
Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18
Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18
Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18Derek Ashmore
 
Tactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure CodeTactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure CodeDerek Ashmore
 
Managing AWS Using Terraform AWS Atlanta 2018-07-18
Managing AWS Using Terraform AWS Atlanta 2018-07-18Managing AWS Using Terraform AWS Atlanta 2018-07-18
Managing AWS Using Terraform AWS Atlanta 2018-07-18Derek Ashmore
 
Refactoring Into Microservices 2016-11-08
Refactoring Into Microservices 2016-11-08Refactoring Into Microservices 2016-11-08
Refactoring Into Microservices 2016-11-08Derek Ashmore
 
Refactoring Into Microservices 2016-11-06
Refactoring Into Microservices 2016-11-06Refactoring Into Microservices 2016-11-06
Refactoring Into Microservices 2016-11-06Derek Ashmore
 
Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...
Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...
Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...Derek Ashmore
 
Platform Engineering for the Modern Oracle World
Platform Engineering for the Modern Oracle WorldPlatform Engineering for the Modern Oracle World
Platform Engineering for the Modern Oracle WorldSimon Haslam
 
APIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabe
APIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabeAPIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabe
APIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabeapidays
 
Docker in the Enterprise
Docker in the EnterpriseDocker in the Enterprise
Docker in the EnterpriseSaul Caganoff
 
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)Derek Ashmore
 
Microservices for architects los angeles-2016-07-16
Microservices for architects los angeles-2016-07-16Microservices for architects los angeles-2016-07-16
Microservices for architects los angeles-2016-07-16Derek Ashmore
 
Database Provisioning in EM12c: Provision me a Database Now!
Database Provisioning in EM12c: Provision me a Database Now!Database Provisioning in EM12c: Provision me a Database Now!
Database Provisioning in EM12c: Provision me a Database Now!Maaz Anjum
 
Testing Infrastructure Code Best Practices and Common Mistakes
Testing Infrastructure Code Best Practices and Common MistakesTesting Infrastructure Code Best Practices and Common Mistakes
Testing Infrastructure Code Best Practices and Common MistakesDerek Ashmore
 
NGENSTOR_ODA_P2V_V5
NGENSTOR_ODA_P2V_V5NGENSTOR_ODA_P2V_V5
NGENSTOR_ODA_P2V_V5UniFabric
 
Cloud Design Patterns - Hong Kong Codeaholics
Cloud Design Patterns - Hong Kong CodeaholicsCloud Design Patterns - Hong Kong Codeaholics
Cloud Design Patterns - Hong Kong CodeaholicsTaswar Bhatti
 
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019Derek Ashmore
 

Similar to Terraform best-practices-and-common-mistakes-dev ops-west-2021 (20)

Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...
Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...
Microservices with Terraform, Docker and the Cloud. Chicago Coders Conference...
 
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
 
Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06
Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06
Microservices with Terraform, Docker and the Cloud. IJug Chicago 2017-06-06
 
Implementing DevOps Automation Best Practices and Common Mistakes
Implementing DevOps AutomationBest Practices and Common MistakesImplementing DevOps AutomationBest Practices and Common Mistakes
Implementing DevOps Automation Best Practices and Common Mistakes
 
Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18
Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18
Managing AWS Using Terraform AWS Chicago-Suburbs 2018-01-18
 
Tactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure CodeTactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure Code
 
Managing AWS Using Terraform AWS Atlanta 2018-07-18
Managing AWS Using Terraform AWS Atlanta 2018-07-18Managing AWS Using Terraform AWS Atlanta 2018-07-18
Managing AWS Using Terraform AWS Atlanta 2018-07-18
 
Refactoring Into Microservices 2016-11-08
Refactoring Into Microservices 2016-11-08Refactoring Into Microservices 2016-11-08
Refactoring Into Microservices 2016-11-08
 
Refactoring Into Microservices 2016-11-06
Refactoring Into Microservices 2016-11-06Refactoring Into Microservices 2016-11-06
Refactoring Into Microservices 2016-11-06
 
Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...
Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...
Application Deployment Patterns in the Cloud - NOVA Cloud and Software Engine...
 
Platform Engineering for the Modern Oracle World
Platform Engineering for the Modern Oracle WorldPlatform Engineering for the Modern Oracle World
Platform Engineering for the Modern Oracle World
 
APIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabe
APIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabeAPIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabe
APIsecure 2023 - How to abuse Terraform to elevate access, Mike McCabe
 
Docker in the Enterprise
Docker in the EnterpriseDocker in the Enterprise
Docker in the Enterprise
 
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
 
Microservices for architects los angeles-2016-07-16
Microservices for architects los angeles-2016-07-16Microservices for architects los angeles-2016-07-16
Microservices for architects los angeles-2016-07-16
 
Database Provisioning in EM12c: Provision me a Database Now!
Database Provisioning in EM12c: Provision me a Database Now!Database Provisioning in EM12c: Provision me a Database Now!
Database Provisioning in EM12c: Provision me a Database Now!
 
Testing Infrastructure Code Best Practices and Common Mistakes
Testing Infrastructure Code Best Practices and Common MistakesTesting Infrastructure Code Best Practices and Common Mistakes
Testing Infrastructure Code Best Practices and Common Mistakes
 
NGENSTOR_ODA_P2V_V5
NGENSTOR_ODA_P2V_V5NGENSTOR_ODA_P2V_V5
NGENSTOR_ODA_P2V_V5
 
Cloud Design Patterns - Hong Kong Codeaholics
Cloud Design Patterns - Hong Kong CodeaholicsCloud Design Patterns - Hong Kong Codeaholics
Cloud Design Patterns - Hong Kong Codeaholics
 
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
 

Recently uploaded

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 

Recently uploaded (20)

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 

Terraform best-practices-and-common-mistakes-dev ops-west-2021

  • 1. Terraform Infrastructure as Code Best Practices and Common Mistakes Given by Derek C. Ashmore DevOps West 2021 June 9, 2021 ©2020 Derek C. Ashmore, All Rights Reserved 1
  • 2. Who am I? • Professional Geek since 1987 • AWS since 2010 • Azure since 2017 • Terraform since the 0.5.x days • Specialties • Application Transformation • Infrastructure Automation • Yes – I still code! ©2021 Derek C. Ashmore, All Rights Reserved 2
  • 3. Discussion Resources • This slide deck – https://www.slideshare.net/derekashmore/presentations • Sample code on my Github – https://github.com/Derek-Ashmore/ • Slide deck has hyper-links! – Don’t bother writing down URLs • Assumptions – You have used Terraform (at least played with it) – You know basic functionality • Keep track of your questions – live Q&A at the end ©2021 Derek C. Ashmore, All Rights Reserved 3
  • 4. Agenda Intro and Level Set Environment Management Modularity Summary / Q&A ©2021 Derek C. Ashmore, All Rights Reserved 4
  • 5. Terraform Terminology • Configuration vs Module – Terraform Module designed for reuse – Terraform configuration is the outer layer • TFVars files – Provides variable input for an environment • Like properties file – Used with the –var-file option ©2021 Derek C. Ashmore, All Rights Reserved • Resources – Controls a cloud asset • Data lookup – Searches for cloud assets • Variables – Different values per context • Function and Expressions – Built-ins that gather/manipulate values
  • 6. How Terraform Works • Terraform is “Declarative” – Like SQL • Reads all files with extension .tf – Figures out execution order • Supports variables and functions • Plugin architecture – Supports many clouds and products ©2021 Derek C. Ashmore, All Rights Reserved
  • 7. Agenda Intro and Level Set Environment Management Modularity Summary / Q&A ©2021 Derek C. Ashmore, All Rights Reserved 7
  • 8. Project Structure • Separate Configurations from Modules – Documents what’s designed for reuse and what is not • TFVars files provide environment specifics – All environments use the same automation – Easy to add environments – Different back-end state per environment ©2021 Derek C. Ashmore, All Rights Reserved
  • 9. Project Structure Anti-Pattern • Separate configurations per environment • The good – Code is often simpler – Easier to add/subtract capabilities per environment – Separate state if using local file system default • The bad – Has code duplication – Harder to establish new environments – Environments can be inconsistent • Works in dev, but not prod ©2021 Derek C. Ashmore, All Rights Reserved
  • 10. Making Environment Differences Configurable • Use Conditionals – No “If-Then” capability – Boolean indicators • Resources using count • Dynamic blocks ©2021 Derek C. Ashmore, All Rights Reserved
  • 11. Optional configuration through ‘try’ • Use for complex inputs with optional fields • Try suppresses exceptions • Specify Terraform defaults with null ©2021 Derek C. Ashmore, All Rights Reserved
  • 12. Environment Management Best Practices • Always run Terraform through tooling, not on your desktop – CI/CD Tools such as Jenkins or Terraform Cloud – Benefits • Audit history • Terraform and provider version control • Consistent runtime environment • Always require a plan before the apply – Require approval step before going on to the apply • Utilize cloud security constructs – AWS IAM instance roles for Jenkins agents – Azure Managed Identities for Jenkins or Azure DevOps agents • Always use back-end state ©2021 Derek C. Ashmore, All Rights Reserved
  • 13. Agenda Intro and Level Set Environment Management Modularity Summary / Q&A ©2021 Derek C. Ashmore, All Rights Reserved 13
  • 14. Terraform Usage Evolution • In the beginning – Use Source Control – Use Back-end state • As #Coders grows – Feature branches – CI/CD Pipelines • As #Configurations grows – Separate repo for modules • Or Terraform registry – Implement versioning • Never use main/master! • Further reading ©2021 Derek C. Ashmore, All Rights Reserved
  • 15. Feature Branching • DevOps Team Discipline is Key • Feature Branches – Never edit main/master directly! – Update using Pull Requests • Should live less than one day! – Single targeted enhancement – One developer only – Long-lived branches prone to merge conflicts – Prefer rebase to merge • Further reading ©2021 Derek C. Ashmore, All Rights Reserved
  • 16. CI/CD Pipelines • Provides consistent runtime environment – Terraform version – Cloud security policy • Audit history / Admin security • Pipeline approvals – Force Plan execution – Force manual approval before apply or destroy – Automatic “Apply” nullifies benefit of doing the plan ©2021 Derek C. Ashmore, All Rights Reserved
  • 17. Modularity Anti-Patterns • All of these examples come from the field – Module creation before it’s needed – Modules that only contain one resource – Inappropriate Data lookups in modules – Undocumented modules – Use modules referencing main/master ©2021 Derek C. Ashmore, All Rights Reserved
  • 18. Module creation before it’s needed • Should have at least two consumers before module is created • Classic YAGNI • Hard to track down consumers after release • Impossible to remove unused modules ©2021 Derek C. Ashmore, All Rights Reserved
  • 19. Modules that only contain one resource • Amounts to a thin proxy – No value-add • Unnecessary complexity • Not as well documented as the underlying Terraform resource • Every module should have at least two resources! ©2021 Derek C. Ashmore, All Rights Reserved
  • 20. Inappropriate Data lookups in modules • Data lookups fail if nothing is found – Error if the consumer configuration creates the resource • Makes assumptions about execution context • Data lookups belong in configurations, not modules, as they do know context ©2021 Derek C. Ashmore, All Rights Reserved
  • 21. Undocumented Modules • Force consumers to read/understand module code – Costs them time • Makes it hard to use • All modules should have a README.md: – Example module call – Release Notes – Variable list – Output list ©2021 Derek C. Ashmore, All Rights Reserved
  • 22. Use modules referencing main/master • Always consume referencing specific versions – Version upgrades are planned work • Source code ©2021 Derek C. Ashmore, All Rights Reserved • Recipe for unplanned work – Consumers can break unexpectedly when modules change • Always version modules
  • 23. Agenda Intro and Level Set Environment Management Modularity Summary / Q&A ©2021 Derek C. Ashmore, All Rights Reserved 23
  • 24. Secrets Handling • Secrets include – Credentials (account/password) – SSL Certificates – SSH Keys • Manage secrets separately – Digital Vault • Terraform looks the secret up – CI/CD Pipeline “Secret” variable • Anti-pattern: Terraform generating password – Easy to get out of sync with reality – Secrets have different life-cycle ©2021 Derek C. Ashmore, All Rights Reserved
  • 25. Simplicity is Key • Eliminate unused variables – Always remove dead code • Don’t replicate derived values – Derive once in locals and use • Variable defaults – Inappropriate defaults common • Environment-specific names • Globally unique names ©2021 Derek C. Ashmore, All Rights Reserved
  • 26. Avoid the Hammer and Nail Problem • Terraform is good for: – Creating cloud assets – Changing attributes on cloud assets ©2021 Derek C. Ashmore, All Rights Reserved • Terraform is not good for: – Maintaining content on cloud assets • VM configuration management – Use Ansible, Chef, etc. • Image pipelines – Use Packer – Don’t “remote control” • Use Terraform to execute Ansible or Packer
  • 27. Session Summary • How to structure projects • Manage environments using tfvars – Not configurations • How to make resources optional • Use CI/CD tooling • Appropriate uses for Terraform ©2021 Derek C. Ashmore, All Rights Reserved • Module Anti-Patterns – Module creation before it’s needed – Modules that only contain one resource – Inappropriate Data lookups in modules – Undocumented modules – Use modules referencing main/master
  • 28. Thank you! • Derek Ashmore: – Blog: www.derekashmore.com – LinkedIn: www.linkedin.com/in/derekashmore • Connect Invites from attendees welcome – Twitter: https://twitter.com/Derek_Ashmore – GitHub: https://github.com/Derek-Ashmore – Book: http://dvtpress.com/ • Please fill out the survey form! • Click the “Subsessions” tab for live Q&A ©2021 Derek C. Ashmore, All Rights Reserved 28