SlideShare a Scribd company logo
1 of 35
Misadventures
With Terraform
Matthew Revell
Senior Consultant at OpenCredo
In the beginning...
Bash &
Python
A few years ago...
Terraform
0.6.xx
Bash &
Python
Into the future...
Terraform
0.6.xx
Terraform
0.12.xx
Bash &
Python
when modules get too small
Honey I Shrunk The Mods
Monolith Terraform State
Reusable Modules
Single Resource Modules
Complex Deployment
Consider whether a
single resource
module adds any
value
Consider whether
the additional
complexity is worth
the perceived value
Consider whether
the module will be
usable by the
intended
consumer(s)
Value Complexity Usability
Lessons Learned (small modules)
pray the code works the second time
Run Once And Forget
A Service
Two Services
Two Connected Services
FIRE!
Testing modules in
isolation can only
validate the
internals
Full deployment
tests are essential
to validate the
entire Terraform
structure
A dedicated
account can allow
continuous testing
without disruption
Testing Testing Testing
Lessons Learned (running code once)
or in this case Terraform States
How To Slice The Cake
Terraform States and Modules
Terraform States and Modules
Resources should
be grouped such
that states do not
grow exponentially
States should have
a limited scope to
minimise impact in
the event of
mistakes
Teams should be
able to manage
their own Terraform
independently
Scalability Blast Radius Ownership
Lessons Learned (dividing states)
but DRY is divine
To dir Is Human
terraform
+ env
+ dev
- terraform.tfvars
- backend.tf
- main.tf
+ prod
- terraform.tfvars
- backend.tf
- main.tf
Terraform Code Repo
variable "instance_type" {}
module "vpc" {
source = "git::https://example.com/vpc.git?ref=0.1.0"
}
module "app" {
source = "git::https://example.com/app.git?ref=0.4.20"
instance_type = "${var.instance_type}"
}
Terraform main.tf
instance_type = "m4.large"
Terraform terraform.tfvars
terraform {
backend "s3" {
bucket = "terraform-states"
key = "prod/terraform.tfstate"
}
}
Terraform backend.tf
terraform
+ env
+ dev
- terraform.tfvars
- backend.tf
- main.tf
+ prod
- terraform.tfvars
- backend.tf
- main.tf
Terraform Code Repo
● Thin wrapper for Terraform
● Allows for easier management of backends
● Reduces amount of repeated code
● Developed by Gruntworks
Terragrunt
Image courtesy of Gruntworks Inc.
terraform
+ env
- common.tfvars
+ dev
- terraform.tfvars
+ prod
- terraform.tfvars
Terragrunt Code Repo
terragrunt = {
remote_state {
backend = "s3"
config {
bucket = "my-terraform-state"
key = "${path_relative_to_include()}/terraform.tfstate"
}
}
}
instance_type = "m4.medium"
Terragrunt common.tfvars
terragrunt = {
include {
path = "../common.tfvars"
}
terraform {
source = "git::https://example.com/deployment.git?ref=v0.0.1"
}
}
instance_type = "m4.xlarge"
Terragrunt terraform.tfvars
terraform
+ env
- common.tfvars
+ dev
- terraform.tfvars
+ prod
- terraform.tfvars
Terragrunt Code Repo
● Generic templating engine
● Generate output in YAML or JSON
● Uses jsonnet, jinja2 (and more)
● Developed by Deepmind
Kapitan
Image courtesy of Deepmind (Alphabet Inc.)
Repeated code and
copy and pasting
will definitely lead
to mistakes
A lack of clarity and
readability will also
lead to confusion
and mistakes
Tooling can help
maintain clean code
in complex
deployments
Keep it DRY Clarity Tooling
Lessons Learned (repo structures)
Concluding Remarks
comments, or ideas
Questions

More Related Content

Similar to Misadventures With Terraform

Similar to Misadventures With Terraform (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019Terraform modules and some of best-practices - March 2019
Terraform modules and some of best-practices - March 2019
 
Terraform modules and (some of) best practices
Terraform modules and (some of) best practicesTerraform modules and (some of) best practices
Terraform modules and (some of) best practices
 
Terraform -- Infrastructure as Code
Terraform -- Infrastructure as CodeTerraform -- Infrastructure as Code
Terraform -- Infrastructure as Code
 
Terraform + ansible talk
Terraform + ansible talkTerraform + ansible talk
Terraform + ansible talk
 
The hitchhiker's guide to terraform your infrastructure
The hitchhiker's guide to terraform your infrastructureThe hitchhiker's guide to terraform your infrastructure
The hitchhiker's guide to terraform your infrastructure
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform Training
 
Crushing Latency with Vert.x
Crushing Latency with Vert.xCrushing Latency with Vert.x
Crushing Latency with Vert.x
 
London HUG 12/4
London HUG 12/4London HUG 12/4
London HUG 12/4
 
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
 
"Continuously delivering infrastructure using Terraform and Packer" training ...
"Continuously delivering infrastructure using Terraform and Packer" training ..."Continuously delivering infrastructure using Terraform and Packer" training ...
"Continuously delivering infrastructure using Terraform and Packer" training ...
 
Scaling terraform
Scaling terraformScaling terraform
Scaling terraform
 
Terraform modules restructured
Terraform modules restructuredTerraform modules restructured
Terraform modules restructured
 
Terraform Modules Restructured
Terraform Modules RestructuredTerraform Modules Restructured
Terraform Modules Restructured
 
Terraform 0.12 + Terragrunt
Terraform 0.12 + TerragruntTerraform 0.12 + Terragrunt
Terraform 0.12 + Terragrunt
 
Terracotta DSO
Terracotta DSOTerracotta DSO
Terracotta DSO
 
Testing & deploying terraform
Testing & deploying terraformTesting & deploying terraform
Testing & deploying terraform
 
DotDotPwn Fuzzer - Black Hat 2011 (Arsenal)
DotDotPwn Fuzzer - Black Hat 2011 (Arsenal)DotDotPwn Fuzzer - Black Hat 2011 (Arsenal)
DotDotPwn Fuzzer - Black Hat 2011 (Arsenal)
 
Terraform Best Practices - DevOps Unicorns 2019
Terraform Best Practices - DevOps Unicorns 2019Terraform Best Practices - DevOps Unicorns 2019
Terraform Best Practices - DevOps Unicorns 2019
 
Deep Dive into Futures and the Parallel Programming Library
Deep Dive into Futures and the Parallel Programming LibraryDeep Dive into Futures and the Parallel Programming Library
Deep Dive into Futures and the Parallel Programming Library
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Misadventures With Terraform

Editor's Notes

  1. PERSONAL PAST EXPERIENCES CURRENTLY work for a company called OPENCREDO as a SENIOR CONSULTANT OC is a SOFTWARE DEVELOPMENT CONSULTANCY We SPECIALISE in … CLOUD NATIVE ARCHITECTURE DATA ENGINEERING HELP OUR CLIENTS to ADAPT and to ADOPT new TECHNOLOGIES and PRACTICES I myself STARTED MY CAREER as a SYSTEMS ENGINEER It was my job to MANAGE SERVERS AS TIME AND TECHNOLOGY MOVED ON ... I deal a lot more with DEVOPS TOOLS and MANAGING CLOUD PLATFORMS Rather than MANAGING SERVERS
  2. When I FIRST STARTED managing CLOUD INFRASTRUCTURE Things were relatively PRIMITIVE MOSTLY still done using BASH and PYTHON… (same as before) SOME PLATFORM SPECIFIC TOOLS SUCH AS CLOUDFORMATION DIFFICULT TO WORK WITH
  3. Then around 2015, I started using TERRAFORM It was a GAME CHANGER This was AROUND THE TIME Terraform 0.6 was released Given that the LATEST RELEASE IS 0.12, I guess it was about HALF WAY THERE This was an INTERESTING TIME for Terraform No WAY TO DEFINE STATE BACKEND IN CODE No IMPORT command No STATE command No NATIVE STATEFILE LOCKING Many NOT-SO-FUNNY incidents… When SOMEONE DESTROYED PRODUCTION because they’d configured the wrong backend When a REFACTORING PROCESS was a MARATHON of MANUALLY EDITING state files
  4. THANKFULLY those days are BEHIND US If you’ve just started using Terraform, you’ll never know the horror! I’m aware that makes me sound like someone’s grandad going on about “how things were in my day”, but it happens to be true HOWEVER there are MANY problems I’VE EXPERIENCED which I THINK STILL APPLY
  5. When I FIRST USED TERRAFORM LARGE COMPANY Went through the ARCHETYPAL JOURNEY AS SEEN ON YOUTUBE / AT HASHICONF
  6. STARTED with a SINGLE FILE and STATE STARTED BREAKING DOWN Into MULTIPLE STATES and REUSABLE MODULES
  7. Modules DEVELOPED as a CENTRAL TEAM Modules INTENDED to be CONSUMED by INTERNAL TEAMS Approach GENERALLY WORKED REALLY WELL MODULES FOR VPC ELK STACK COMMON APP PATTERNS THEN MODULES STARTED GETTING SMALLER
  8. Started making MODULES for SINGLE RESOURCES EC2 INSTANCE LOAD BALANCER AUTOSCALE GROUP Done for a SPECIFIC GOAL; TAGGING SEEMED like a NEAT SOLUTION PROVIDE PIECEMEAL BUILDING BLOCKS Problem: DIDN”T WORK AS PLANNED, WHEN TEAMS TRIED TO ADOPT THINGS GOT COMPLICATED
  9. Deployments had SO MANY MODULES TERRAFORM INIT TAKING AGES Modules NESTED several LAYERS DEEP Modules needed to REFERENCE EACH OTHER NESTING made this very DIFFICULT REMINISCENT of NORMAL FORMS in DATABASES THERE IS A POINT where the OVERHEAD and COMPLEXITY is no longer worth it Fix: Company also had AUDITING SOFTWARE CHECKED each ACCOUNT and sent REPORTS (NAGGING EMAILS) to TEAM LEADS / HEADS OF TECH No REAL NEED to enforce through MODULES
  10. VALUE SMALL modules CAN BE USEFUL, but NEED TO ADD VALUE E.G. SECURITY GROUP RULE modules COMPLEXITY IS IT WORTH THE PERCEIVED VALUE USABILITY INTENDED CONSUMER
  11. Setup: If we only run code once, can we be sure it will work again? Once I worked at a company, where we BUILT a SERVICE
  12. Setup (cont): Done RIGHT with TERRAFORM And THEN we built A SECOND SERVICE
  13. Setup (cont): And we BUILT it RIGHT TOO, with TERRAFORM And then we DECIDED they needed to TALK TO EACH OTHER
  14. Setup (cont): We UPDATED the TERRAFORM and EVERYTHING worked FINE CLEAN TERRAFORM PLAN Problem: One day, we DISCOVERED that the ENVIRONMENT had been COMPROMISED
  15. COMPANY POLICY was that the ENVIRONMENT should be DECOMMISSIONED and a NEW ENVIRONMENT CREATED It was at about THIS TIME that we DISCOVERED that our TERRAFORM DIDN’T WORK FROM SCRATCH EACH SERVICE was in its OWN STATE, and REFERENCED SG GROUP ID from the others REMOTE STATE FINE when BOTH ALREADY EXISTED BOTH FAILED when NEITHER EXISTED A -> B -> A
  16. ULTIMATELY we needed MORE TESTING TERRATEST was NOT ENOUGH Needed to TEST FULL DEPLOYMENT DEDICATED ACCOUNT FOR IAC TESTING
  17. Setup: A COMMON EXERCISE; SEGMENT MODULES / STATES We DECIDED to SPLIT STATES based on TYPE OF RESOURCE So we had a STATE for NETWORKING resources SECURITY resources Etc. It looked A LITTLE BIT LIKE THIS
  18. In reality a bit MORE COMPLICATED TO START WITH this was absolutely FINE EVERYTHING was in a logical PLACE, we knew WHERE to look to UPDATE our DEPLOYMENT Where THINGS started to BREAK DOWN was when THE COMPANY STARTED GROWING We were CREATING new TEAMS and SERVICES almost CONSTANTLY Every TEAM needed IAM ROLES, S3 BUCKET for ARTIFACTS, amongst other things Every SERVICE needed DNS RECORDS, a DATABASE, S3 BUCKET etc. etc. Problem: UPDATE 5-6 Terraform STATES STATES GROWING in size with each ADDITION, making UPDATES SLOWER BLAST RADIUS Our DATABASE STATE contained ALL the DATABASES, so a MISTAKE could AFFECT MANY SERVICES FEW PEOPLE responsible for ALL THE TERRAFORM Fix: We NEEDED a bit of a RETHINK… and we REALISED THINGS we built once per ENVIRONMENT, TEAM, SERVICE So we REFACTORED our STATES and MODULES to look a bit more like this...
  19. Fix (cont): This WORKED VERY WELL CREATED NEW ENVIRONMENT with several STATES, but once we’d provisioned, those were mostly left alone If a NEW TEAM was being created we had a TEMPLATE and CREATED A NEW STATE for each, which again remained mostly static once provisioned The same for a NEW SERVICE, but even better, once a TEAM was set up, they could MANAGE THEIR OWN TERRAFORM This also meant that MISTAKES would only AFFECT ONE specific SERVICE
  20. SCALABILITY States should not grow exponentially BLAST RADIUS States should have limited scope to minimise impact of mistakes OWNERSHIP Teams should be able to manage Terraform independently
  21. Setup: This is a problem that hasn’t happened JUST ONCE, it come up pretty much EVERY TIME And it’s one I don’t think has REALLY BEEN SOLVED YET How SHOULD we STRUCTURE our DEPLOYMENT REPOS
  22. Setup (cont): Here’s a typical REPO LAYOUT, where the code for DEV and PROD is pretty much the same SIMPLIFIED but main point is that DEV and PROD contain the same files The main.tf may LOOK LIKE THIS...
  23. ONLY difference between DEV and PROD -> VERSION NUMBERS And the TERRAFORM.TFVARS SIMPLY HAS...
  24. ASSIGNING the VARIABLE And the BACKEND.TF...
  25. ONLY DIFFERENCE is the KEY
  26. IF THIS SIMPLE…. OK IS AT LEAST CLEAR, but not DRY Problem: The MAIN ISSUE with this is that UPDATES WENT INTO DEV, and then copied over to PROD OVER THE YEARS I’ve seen MANY ATTEMPTS to RESOLVE… GIT BRANCH SYMLINKING WORKSPACES; HARD TO SEE HOW MANY-> WHAT IS DEPLOYED IN EACH -> NOT INTENDED FOR ISOLATED ENVS Fix: ONE SOLUTION THAT WORKED QUITE WELL WAS A TOOL CALLED TERRAGRUNT
  27. TERRAGRUNT Thin WRAPPER for TERRAFORM EASIER management of TERRAFORM BACKENDS REDUCES amount of REPEATED CODE DEVELOPED BY GRUNTWORKS INC.
  28. So with TERRAGRUNT the repo would look like this Significantly FEWER FILES NOTE, the common.tfvars
  29. DEFINE BACKEND ONCE DYNAMICALLY TG HAS BUILT IN FUNCS TO FILL BACKEND SET DEFAULT FOR VARIABLE AND THEN IN EACH terraform.tfvars ...
  30. IMPORT common.tfvars DEFINE SOURCE OVERRIDE VARIABLES FOR SPECIFIC ENV
  31. The ONLY ISSUE we experienced was that TERRAGRUNT EXPECTED ONE MODULE PER STATE THIS WAS NOT HOW WE WANTED TO WORK We WORKED AROUND that with some META MODULES WASN’T IDEAL but it solved most of the issues TERRAGRUNT UPDATED NOW SUPPORTS MULTI MODULES IT ALSO HAS A NICE ‘apply-all’ FEATURE ONE MORE TOOL HONOURABLE MENTION...
  32. KAPITAN GENERIC TEMPLATING TO AUTO-GENERATE CODE OUTPUT IN YAML OR JSON USES JSONNET AND JINJA Developed by DEEPMIND USEFUL IF USE KUBERNETES + TERRAFORM + ANSIBLE + PACKER TRADE OFF: COMPLEXITY
  33. KEEP IT DRY REPEATED CODE WILL LEAD TO ERRORS CLARITY ALSO IMPORTANT; LACK CAUSES CONFUSION TOOLING LEVERAGE TOOLING
  34. The SCENARIOS I’ve DISCUSSED covering SHAPE and SIZE of MODULE CONFIDENCE in RUNNING DEPLOYMENTS STRUCTURING STATES STRUCTURING DEPLOYMENT REPOS KEY THEMES FIRSTLY, SAME AS SOFTWARE DEVELOPMENT TESTING + CLEAN CODE MORE IMPORTANTLY... PROBLEMS often arise due to ARCHITECTURAL DECISIONS; DON’T CONSIDER THE NATURE OF THE ORG + CHANGES MOST IMPORTANT LESSON LEARNED IN MY EXPERIENCE AND MISADVENTURES WITH TERRAFORM NO DEFINITIVE LOGICAL PRINCIPAL The SHAPE, SIZE, and CULTURE of an ORGANISATION are IMPORTANT CONSIDER THESE WHEN DESIGNING DEPLOYMENT STRUCTURE