SlideShare a Scribd company logo
1 of 81
Terraform: Tales from the Trenches
Santa Barbara DevOps
Copyright © HG Data 2019
Presenters
Rob Fox
CTO
Sam Chapin
Chief Architect
Brendan Keane
DevOps Engineer
Chris Deutsch
DevOps Architect
The Story Thus Far...
Rob Fox
Background
Product: https://discovery.hgdata.com
(SaaS Platform and related services)
Issues Includes:
● Previous iteration of infrastructure as code fell way short
○ Rigid configuration
○ Ansible+bash hell
○ No automation
● No ability to validate
● No visibility as to what was actually running and where
● Holes around secrets management
● Had to run on a specific version of Ubuntu
Copyright © HG Data 2019
Why Terraform @ HG Data?
● Infrastructure as Code
● Immutable Infrastructure
● Declarative
● Client Only
● Solves a lot of our current issues
Copyright © HG Data 2019
Success so far...
● Customer facing platform completely refactored using Terraform,
Kubernetes, Vault
● Blue/Green Deployment tied into CI/CD
● Many shared modules and libraries in github created and distributed to
rest of engineering
● Platform rock solid (easy to deploy, easy to manage and monitor)
● Big Data team now adopting Terraform and related tooling to manage
large-scaled Spark clusters and pipeline automation/orchestration
Engineers much happier and way more productive!!
Copyright © HG Data 2019
What we learned along the way...
...A lot!!
What follows are three mini-presentations based on our learnings and
experiences to help you be successful
Enjoy!
Copyright © HG Data 2019
Evolution of a Terraform project
Sam Chapin
me.tf :
Step 1. Experimentation (Someone allowed me to play with cool new toys)
The starting state of the tf project:
- Single developer
- Toy deployment
- No "environment"
- No modularization
- No shared state
Copyright © HG Data 2019
Step 1. Experimentation (The halcyon days)
main.tf:Hierarchy:
terraform apply -auto-approve
Example usage:
Copyright © HG Data 2019
Step 1. Experimentation - What Hurts?
- no sandbox
- isn't parameterizable
- hard to test
- non-DRY
- can't execute anywhere
Copyright © HG Data 2019
Step 2. Reality sets in (My boss tells me I need to actually deploy it)
The OLD State of the tf project:
- Single developer
- Toy deployment
- No "environment"
- No modularization
- No shared state
The NEW State of the tf project:
- Single developer
- Toy deployment Prod/Stage/Dev deployment
- No "environment" Environment management
- No modularization
- No shared state
Copyright © HG Data 2019
Step 2. Reality sets in - Environment via configuration
prod.tfvars:Hierarchy:
terraform apply 
-var-file=prod.tfvars
Example usage:
Copyright © HG Data 2019
Step 2. Reality sets in - Environment via symlinks
conf.auto.tfvars:Hierarchy:
cd prod
terraform apply
Example usage:
Copyright © HG Data 2019
Step 2. Reality sets in - Environment via workspace
conf.auto.tfvars:Hierarchy:
terraform workspace new prod
terraform apply
Example usage:
Copyright © HG Data 2019
Step 2. Reality sets in - Environment via workspace & consul
If you’re lucky enough to already have consul...
Copyright © HG Data 2019
Step 2. Reality sets in - What Hurts?
- no sandbox
- isn't parameterizable
- hard to test
- non-DRY
- can't execute anywhere
Copyright © HG Data 2019
Step 3. Refactor (I panic at how much I've been copy-pasting)
The OLD State of the tf project:
- Single developer
- Prod/Stage/Dev deployment
- Environment management (confs, copies, or workspaces)
- No modularization
- No shared state
The NEW State of the tf project:
- Single developer
- Prod/Stage/Dev deployment
- Environment management (confs, copies, or workspaces)
- No modularization Modularization (inner modules / module library)
- No shared state
Copyright © HG Data 2019
Step 3. Refactor - Modules
Hierarchy: modules/instance/main.tf:
Step 3. Refactor - Modules
Hierarchy: ./main.tf:
Copyright © HG Data 2019
Step 3. Refactor - Modules
Hierarchy:
More on this from Brendan!
Copyright © HG Data 2019
Step 3. Refactor - External Modules
What if we want them to be external libs?
Copyright © HG Data 2019
Step 3. Refactor - What Hurts?
- no sandbox
- isn't parameterizable
- hard to test
- non-DRY
- can't execute anywhere
Copyright © HG Data 2019
Step 4. Make it easy (Someone is going to see the mess I made)
The OLD State of the tf project:
- Single developer
- Prod/Stage/Dev deployment
- Environment management (confs, copies, or workspaces)
- Modularization (inner modules or reusable module library)
- No shared state
The NEW State of the tf project:
- Single developer Multiple developers
- Prod/Stage/Dev deployment
- Environment management (confs, copies, or workspaces)
- Modularization (inner modules or reusable module library)
- No shared state Shared state
Copyright © HG Data 2019
Step 4. Make it easy - Shared state across environments
Hierarchy: ./main.tf:
Modularize this!
Step 4. Make it easy - State locking
Lock your damn state.
All the standard backends support state locking.
Copyright © HG Data 2019
Backend
Step 4. Make it easy - State locking - A tale of two
terraformers
.tfstate
ApplyApply
Copyright © HG Data 2019
Step 4. Make it easy - What Hurts?
- no sandbox
- isn't parameterizable
- hard to test
- non-DRY
- can't execute anywhere
- Collaboration with others (PR plan workflow)
- CI/CD locking your state all the time (Build queue)
Check out https://www.runatlantis.io/
Okay, okay... What Still Hurts?
Copyright © HG Data 2019
In summary...
1. Experiment and get familiar with your new declarative friend
1. Solve your environment problem with convention & opinions
1. Make your codebase small and testable with modules
1. Ease collaboration & FUD via state sharing & locking
Copyright © HG Data 2019
Terraform, Behave!
Brendan Keane
What we needed
● Shared modules
● Certainty on shared code.
● Ability to build on top of shared code, we need a high level of
certainty
Copyright © HG Data 2019
What would make us more certain?
Tests!
But what kind of tests in this scenario?
Copyright © HG Data 2019
Do what I say not what I mean
- Terraform does this for you with every action.
resource “aws_instance” “my_server” {
ami = “ami-0c32356aac847d7e8”
}
resource “aws_rds_instance” “my_database” {
ami = “ami-0c223918e1”
}
Copyright © HG Data 2019
Do what I mean not what I say
- Terraform does not test for what you mean.
subject { “my_server” }
it “should be able to reach the database” do
# db connection test
end
Copyright © HG Data 2019
Do what I say not what I mean
Great for auditing known good configuration
Do what I mean not what I say
Great for iterating towards a good configuration
Copyright © HG Data 2019
Base Case:
Assert: A can ping B
BA
● Tests more than at first sight: security groups, subnets, routing, dns…
● Terraform tells me everything is as I declared it.
● Terraform does not tell me if it does what I want it to do.
Copyright © HG Data 2019
Building Blocks
Building Blocks - Bats!
https://github.com/bats-core/bats-core
● Rspec-ish.No ‘shelling out’, can run where infra is stood up
● Good helpers for stdout, stderr, and exit status.
● Before and after hooks if needed.
Copyright © HG Data 2019
Building Blocks - Templating
https://www.terraform.io/docs/providers/template/d/file.html
● ssh driven: this is a plus and minus.
● Many approaches: scripts, inline, powerful when combined with templating.
Copyright © HG Data 2019
Building Blocks - Remote Exec
https://www.terraform.io/docs/provisioners/null_resource.html
Copyright © HG Data 2019
Building Blocks - Structure
Copyright © HG Data 2019
Spec Run
Host A
- Install bats
- Run bats
Dev Machine
- Terraform apply
1. Spin up
3. Runs tests in cloud context
2. Remote-exe Bats
Host B
Copyright © HG Data 2019
Proof Pudding (Demo Time)
Copyright © HG Data 2019
Concluding Thoughts
● Keep the tests simple, too many null_providers == bad
● Positive assertions are usually functionality: A can ping B
● Negative assertions are usually security: B cannot ping A
Future: dockerize, dashboard of health tests orthogonal from application
health checks.
Copyright © HG Data 2019
Terraforming Blue/Green
Deployments
Chris Deutsch
The problem...
“One of the challenges with automating deployment is the cut-over itself,
taking software from the final stage of testing to live production.”
https://www.martinfowler.com/bliki/BlueGreenDeployment.html
Copyright © HG Data 2019
Example basic setup
Copyright © HG Data 2019
Blue and green deployments
Copyright © HG Data 2019
Users are routed to green (live)
Copyright © HG Data 2019
Dev/QA are routed to blue (next)
Copyright © HG Data 2019
Bringing a new release live
Copyright © HG Data 2019
Oh no!
Copyright © HG Data 2019
Recovery: flip live back to green
Copyright © HG Data 2019
Considerations
While the idea is relatively simple, here are three important things to
keep in mind.
Copyright © HG Data 2019
1. Infrastructure
● We have two clusters: blue and green
● Each cluster should be configured in the same way
● Each cluster should operate independently and be easy to maintain
In addition, the more we split things up, the more value we can get out
of blue/green deploys for our infrastructure. But we also increase cost
and complexity.
Copyright © HG Data 2019
2. Configuration Management
Applications should have the same configuration regardless of whether
they are blue or green.
Applications should have the same configuration regardless whether
they are the live version or the next version.
(Well… as much as is reasonable.)
Copyright © HG Data 2019
3. Application State
“Twelve-factor processes are stateless and share-nothing. Any data that
needs to persist must be stored in a stateful backing service, typically a
database.”
https://12factor.net/processes
Copyright © HG Data 2019
Terraform + ELB + Kube + RDS + ElastiCache
Copyright © HG Data 2019
Module 1: Terraforming a Load Balancer
Copyright © HG Data 2019
Starting the app_load_balancer module
Copyright © HG Data 2019
Add an aws_lb_target_group...
Copyright © HG Data 2019
And an aws_lb_listener_rule...
Copyright © HG Data 2019
Okay! Time to use it
Copyright © HG Data 2019
Module 2: the blue deployment
Starting the app_cluster module
Copyright © HG Data 2019
Attaching the Instance to a Target Group
my_arns = {
next = “some_next_arn”
live = “some_live_arn”
}
lookup(var.my_arns, “next”)
=> “some_next_arn”
Excellent! Time to use it
Copyright © HG Data 2019
Module 3: the green deployment
Copyright © HG Data 2019
(It’s actually almost the same as blue)
Copyright © HG Data 2019
(It’s actually almost the same as blue)Color flipping, Terraform style
Copyright © HG Data 2019
1. Change Green From Next to Live
Copyright © HG Data 2019
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Copyright © HG Data 2019
Now both are live!
Copyright © HG Data 2019
Verify With Curl
$ curl http://live.example.com/
I'm an app running in the production environment on
green
$ curl http://live.example.com/
I'm an app running in the production environment on blue
Copyright © HG Data 2019
2. Move Blue From Live To Next
Copyright © HG Data 2019
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Copyright © HG Data 2019
Now green is live, but blue is next
Copyright © HG Data 2019
Verify With Curl
$ curl http://live.example.com/
I'm an app running in the production environment on
green
$ curl http://next.example.com/
503 Service Temporarily Unavailable
# Wait a couple of minutes...
$ curl http://next.example.com/
I'm an app running in the production environment on blue
Copyright © HG Data 2019
Deploy Complete!
Copyright © HG Data 2019
Q&A
Thank you!
Follow us online @ https://www.hgdata.com

More Related Content

Similar to Terraform: Tales from the Trenches

GDG Cloud Southlake #8 Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...
GDG Cloud Southlake #8  Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...GDG Cloud Southlake #8  Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...
GDG Cloud Southlake #8 Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...James Anderson
 
Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...
Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...
Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...Cloud Native Day Tel Aviv
 
Docman - The swiss army knife for Drupal multisite docroot management and dep...
Docman - The swiss army knife for Drupal multisite docroot management and dep...Docman - The swiss army knife for Drupal multisite docroot management and dep...
Docman - The swiss army knife for Drupal multisite docroot management and dep...Aleksey Tkachenko
 
Open Source - NOVALUG January 2019
Open Source  - NOVALUG January 2019Open Source  - NOVALUG January 2019
Open Source - NOVALUG January 2019plarsen67
 
Introduction to Go
Introduction to GoIntroduction to Go
Introduction to GoSimon Hewitt
 
CI/CD patterns for dbt Projects
CI/CD patterns for dbt ProjectsCI/CD patterns for dbt Projects
CI/CD patterns for dbt ProjectsHostedbyConfluent
 
Future of Data Platform in Cloud Native world
Future of Data Platform in Cloud Native worldFuture of Data Platform in Cloud Native world
Future of Data Platform in Cloud Native worldSrivatsan Srinivasan
 
"Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"...
"Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"..."Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"...
"Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"...Edge AI and Vision Alliance
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsGR8Conf
 
OSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando Honig
OSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando HonigOSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando Honig
OSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando HonigNETWAYS
 
RIMA-Infrastructure as a code with Terraform.pptx
RIMA-Infrastructure as a code with Terraform.pptxRIMA-Infrastructure as a code with Terraform.pptx
RIMA-Infrastructure as a code with Terraform.pptxMrJustbis
 
"You don't need a bigger boat": serverless MLOps for reasonable companies
"You don't need a bigger boat": serverless MLOps for reasonable companies"You don't need a bigger boat": serverless MLOps for reasonable companies
"You don't need a bigger boat": serverless MLOps for reasonable companiesData Science Milan
 
Http Services in Rust on Containers
Http Services in Rust on ContainersHttp Services in Rust on Containers
Http Services in Rust on ContainersAnton Whalley
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCDamienCarpy
 
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform DevOpsColumbia
 
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on TerraformDevops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on TerraformDrew Malone
 
GitOps for Helm Users by Scott Rigby
GitOps for Helm Users by Scott RigbyGitOps for Helm Users by Scott Rigby
GitOps for Helm Users by Scott RigbyWeaveworks
 
Implementing Cloud-Based DevOps for Distributed Agile Projects
Implementing Cloud-Based DevOps for Distributed Agile ProjectsImplementing Cloud-Based DevOps for Distributed Agile Projects
Implementing Cloud-Based DevOps for Distributed Agile ProjectsTechWell
 
Open up your platform with Open Source and GitHub
Open up your platform with Open Source and GitHubOpen up your platform with Open Source and GitHub
Open up your platform with Open Source and GitHubScott Graham
 

Similar to Terraform: Tales from the Trenches (20)

GDG Cloud Southlake #8 Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...
GDG Cloud Southlake #8  Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...GDG Cloud Southlake #8  Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...
GDG Cloud Southlake #8 Steve Cravens: Infrastructure as-Code (IaC) in 2022: ...
 
Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...
Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...
Kubernetes is hard! Lessons learned taking our apps to Kubernetes - Eldad Ass...
 
Docman - The swiss army knife for Drupal multisite docroot management and dep...
Docman - The swiss army knife for Drupal multisite docroot management and dep...Docman - The swiss army knife for Drupal multisite docroot management and dep...
Docman - The swiss army knife for Drupal multisite docroot management and dep...
 
Open Source - NOVALUG January 2019
Open Source  - NOVALUG January 2019Open Source  - NOVALUG January 2019
Open Source - NOVALUG January 2019
 
Introduction to Go
Introduction to GoIntroduction to Go
Introduction to Go
 
CI/CD patterns for dbt Projects
CI/CD patterns for dbt ProjectsCI/CD patterns for dbt Projects
CI/CD patterns for dbt Projects
 
Future of Data Platform in Cloud Native world
Future of Data Platform in Cloud Native worldFuture of Data Platform in Cloud Native world
Future of Data Platform in Cloud Native world
 
"Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"...
"Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"..."Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"...
"Using TensorFlow Lite to Deploy Deep Learning on Cortex-M Microcontrollers,"...
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails Projects
 
OSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando Honig
OSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando HonigOSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando Honig
OSDC 2019 | RTO & RPO – Best Practices in Hybrid Architectures by Fernando Honig
 
RIMA-Infrastructure as a code with Terraform.pptx
RIMA-Infrastructure as a code with Terraform.pptxRIMA-Infrastructure as a code with Terraform.pptx
RIMA-Infrastructure as a code with Terraform.pptx
 
"You don't need a bigger boat": serverless MLOps for reasonable companies
"You don't need a bigger boat": serverless MLOps for reasonable companies"You don't need a bigger boat": serverless MLOps for reasonable companies
"You don't need a bigger boat": serverless MLOps for reasonable companies
 
Http Services in Rust on Containers
Http Services in Rust on ContainersHttp Services in Rust on Containers
Http Services in Rust on Containers
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaC
 
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
 
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on TerraformDevops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
Devops Columbia October 2020 - Gabriel Alix: A Discussion on Terraform
 
GitOps for Helm Users by Scott Rigby
GitOps for Helm Users by Scott RigbyGitOps for Helm Users by Scott Rigby
GitOps for Helm Users by Scott Rigby
 
Implementing Cloud-Based DevOps for Distributed Agile Projects
Implementing Cloud-Based DevOps for Distributed Agile ProjectsImplementing Cloud-Based DevOps for Distributed Agile Projects
Implementing Cloud-Based DevOps for Distributed Agile Projects
 
Open up your platform with Open Source and GitHub
Open up your platform with Open Source and GitHubOpen up your platform with Open Source and GitHub
Open up your platform with Open Source and GitHub
 

Recently uploaded

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 

Recently uploaded (20)

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 

Terraform: Tales from the Trenches

  • 1. Terraform: Tales from the Trenches Santa Barbara DevOps Copyright © HG Data 2019
  • 2. Presenters Rob Fox CTO Sam Chapin Chief Architect Brendan Keane DevOps Engineer Chris Deutsch DevOps Architect
  • 3. The Story Thus Far... Rob Fox
  • 4. Background Product: https://discovery.hgdata.com (SaaS Platform and related services) Issues Includes: ● Previous iteration of infrastructure as code fell way short ○ Rigid configuration ○ Ansible+bash hell ○ No automation ● No ability to validate ● No visibility as to what was actually running and where ● Holes around secrets management ● Had to run on a specific version of Ubuntu Copyright © HG Data 2019
  • 5. Why Terraform @ HG Data? ● Infrastructure as Code ● Immutable Infrastructure ● Declarative ● Client Only ● Solves a lot of our current issues Copyright © HG Data 2019
  • 6. Success so far... ● Customer facing platform completely refactored using Terraform, Kubernetes, Vault ● Blue/Green Deployment tied into CI/CD ● Many shared modules and libraries in github created and distributed to rest of engineering ● Platform rock solid (easy to deploy, easy to manage and monitor) ● Big Data team now adopting Terraform and related tooling to manage large-scaled Spark clusters and pipeline automation/orchestration Engineers much happier and way more productive!! Copyright © HG Data 2019
  • 7. What we learned along the way... ...A lot!! What follows are three mini-presentations based on our learnings and experiences to help you be successful Enjoy! Copyright © HG Data 2019
  • 8. Evolution of a Terraform project Sam Chapin
  • 10. Step 1. Experimentation (Someone allowed me to play with cool new toys) The starting state of the tf project: - Single developer - Toy deployment - No "environment" - No modularization - No shared state Copyright © HG Data 2019
  • 11. Step 1. Experimentation (The halcyon days) main.tf:Hierarchy: terraform apply -auto-approve Example usage: Copyright © HG Data 2019
  • 12. Step 1. Experimentation - What Hurts? - no sandbox - isn't parameterizable - hard to test - non-DRY - can't execute anywhere Copyright © HG Data 2019
  • 13. Step 2. Reality sets in (My boss tells me I need to actually deploy it) The OLD State of the tf project: - Single developer - Toy deployment - No "environment" - No modularization - No shared state The NEW State of the tf project: - Single developer - Toy deployment Prod/Stage/Dev deployment - No "environment" Environment management - No modularization - No shared state Copyright © HG Data 2019
  • 14. Step 2. Reality sets in - Environment via configuration prod.tfvars:Hierarchy: terraform apply -var-file=prod.tfvars Example usage: Copyright © HG Data 2019
  • 15. Step 2. Reality sets in - Environment via symlinks conf.auto.tfvars:Hierarchy: cd prod terraform apply Example usage: Copyright © HG Data 2019
  • 16. Step 2. Reality sets in - Environment via workspace conf.auto.tfvars:Hierarchy: terraform workspace new prod terraform apply Example usage: Copyright © HG Data 2019
  • 17. Step 2. Reality sets in - Environment via workspace & consul If you’re lucky enough to already have consul... Copyright © HG Data 2019
  • 18. Step 2. Reality sets in - What Hurts? - no sandbox - isn't parameterizable - hard to test - non-DRY - can't execute anywhere Copyright © HG Data 2019
  • 19. Step 3. Refactor (I panic at how much I've been copy-pasting) The OLD State of the tf project: - Single developer - Prod/Stage/Dev deployment - Environment management (confs, copies, or workspaces) - No modularization - No shared state The NEW State of the tf project: - Single developer - Prod/Stage/Dev deployment - Environment management (confs, copies, or workspaces) - No modularization Modularization (inner modules / module library) - No shared state Copyright © HG Data 2019
  • 20. Step 3. Refactor - Modules Hierarchy: modules/instance/main.tf:
  • 21. Step 3. Refactor - Modules Hierarchy: ./main.tf: Copyright © HG Data 2019
  • 22. Step 3. Refactor - Modules Hierarchy: More on this from Brendan! Copyright © HG Data 2019
  • 23. Step 3. Refactor - External Modules What if we want them to be external libs? Copyright © HG Data 2019
  • 24. Step 3. Refactor - What Hurts? - no sandbox - isn't parameterizable - hard to test - non-DRY - can't execute anywhere Copyright © HG Data 2019
  • 25. Step 4. Make it easy (Someone is going to see the mess I made) The OLD State of the tf project: - Single developer - Prod/Stage/Dev deployment - Environment management (confs, copies, or workspaces) - Modularization (inner modules or reusable module library) - No shared state The NEW State of the tf project: - Single developer Multiple developers - Prod/Stage/Dev deployment - Environment management (confs, copies, or workspaces) - Modularization (inner modules or reusable module library) - No shared state Shared state Copyright © HG Data 2019
  • 26. Step 4. Make it easy - Shared state across environments Hierarchy: ./main.tf: Modularize this!
  • 27. Step 4. Make it easy - State locking Lock your damn state. All the standard backends support state locking. Copyright © HG Data 2019
  • 28. Backend Step 4. Make it easy - State locking - A tale of two terraformers .tfstate ApplyApply Copyright © HG Data 2019
  • 29. Step 4. Make it easy - What Hurts? - no sandbox - isn't parameterizable - hard to test - non-DRY - can't execute anywhere - Collaboration with others (PR plan workflow) - CI/CD locking your state all the time (Build queue) Check out https://www.runatlantis.io/ Okay, okay... What Still Hurts? Copyright © HG Data 2019
  • 30. In summary... 1. Experiment and get familiar with your new declarative friend 1. Solve your environment problem with convention & opinions 1. Make your codebase small and testable with modules 1. Ease collaboration & FUD via state sharing & locking Copyright © HG Data 2019
  • 32. What we needed ● Shared modules ● Certainty on shared code. ● Ability to build on top of shared code, we need a high level of certainty Copyright © HG Data 2019
  • 33. What would make us more certain? Tests! But what kind of tests in this scenario? Copyright © HG Data 2019
  • 34. Do what I say not what I mean - Terraform does this for you with every action. resource “aws_instance” “my_server” { ami = “ami-0c32356aac847d7e8” } resource “aws_rds_instance” “my_database” { ami = “ami-0c223918e1” } Copyright © HG Data 2019
  • 35. Do what I mean not what I say - Terraform does not test for what you mean. subject { “my_server” } it “should be able to reach the database” do # db connection test end Copyright © HG Data 2019
  • 36. Do what I say not what I mean Great for auditing known good configuration Do what I mean not what I say Great for iterating towards a good configuration Copyright © HG Data 2019
  • 37. Base Case: Assert: A can ping B BA ● Tests more than at first sight: security groups, subnets, routing, dns… ● Terraform tells me everything is as I declared it. ● Terraform does not tell me if it does what I want it to do. Copyright © HG Data 2019
  • 39. Building Blocks - Bats! https://github.com/bats-core/bats-core ● Rspec-ish.No ‘shelling out’, can run where infra is stood up ● Good helpers for stdout, stderr, and exit status. ● Before and after hooks if needed. Copyright © HG Data 2019
  • 40. Building Blocks - Templating https://www.terraform.io/docs/providers/template/d/file.html ● ssh driven: this is a plus and minus. ● Many approaches: scripts, inline, powerful when combined with templating. Copyright © HG Data 2019
  • 41. Building Blocks - Remote Exec https://www.terraform.io/docs/provisioners/null_resource.html Copyright © HG Data 2019
  • 42. Building Blocks - Structure Copyright © HG Data 2019
  • 43. Spec Run Host A - Install bats - Run bats Dev Machine - Terraform apply 1. Spin up 3. Runs tests in cloud context 2. Remote-exe Bats Host B Copyright © HG Data 2019
  • 44. Proof Pudding (Demo Time) Copyright © HG Data 2019
  • 45. Concluding Thoughts ● Keep the tests simple, too many null_providers == bad ● Positive assertions are usually functionality: A can ping B ● Negative assertions are usually security: B cannot ping A Future: dockerize, dashboard of health tests orthogonal from application health checks. Copyright © HG Data 2019
  • 47. The problem... “One of the challenges with automating deployment is the cut-over itself, taking software from the final stage of testing to live production.” https://www.martinfowler.com/bliki/BlueGreenDeployment.html Copyright © HG Data 2019
  • 48. Example basic setup Copyright © HG Data 2019
  • 49. Blue and green deployments Copyright © HG Data 2019
  • 50. Users are routed to green (live) Copyright © HG Data 2019
  • 51. Dev/QA are routed to blue (next) Copyright © HG Data 2019
  • 52. Bringing a new release live Copyright © HG Data 2019
  • 53. Oh no! Copyright © HG Data 2019
  • 54. Recovery: flip live back to green Copyright © HG Data 2019
  • 55. Considerations While the idea is relatively simple, here are three important things to keep in mind. Copyright © HG Data 2019
  • 56. 1. Infrastructure ● We have two clusters: blue and green ● Each cluster should be configured in the same way ● Each cluster should operate independently and be easy to maintain In addition, the more we split things up, the more value we can get out of blue/green deploys for our infrastructure. But we also increase cost and complexity. Copyright © HG Data 2019
  • 57. 2. Configuration Management Applications should have the same configuration regardless of whether they are blue or green. Applications should have the same configuration regardless whether they are the live version or the next version. (Well… as much as is reasonable.) Copyright © HG Data 2019
  • 58. 3. Application State “Twelve-factor processes are stateless and share-nothing. Any data that needs to persist must be stored in a stateful backing service, typically a database.” https://12factor.net/processes Copyright © HG Data 2019
  • 59. Terraform + ELB + Kube + RDS + ElastiCache Copyright © HG Data 2019
  • 60. Module 1: Terraforming a Load Balancer Copyright © HG Data 2019
  • 61. Starting the app_load_balancer module Copyright © HG Data 2019
  • 64. Okay! Time to use it Copyright © HG Data 2019
  • 65. Module 2: the blue deployment
  • 66. Starting the app_cluster module Copyright © HG Data 2019
  • 67. Attaching the Instance to a Target Group my_arns = { next = “some_next_arn” live = “some_live_arn” } lookup(var.my_arns, “next”) => “some_next_arn”
  • 68. Excellent! Time to use it Copyright © HG Data 2019
  • 69. Module 3: the green deployment Copyright © HG Data 2019
  • 70. (It’s actually almost the same as blue) Copyright © HG Data 2019
  • 71. (It’s actually almost the same as blue)Color flipping, Terraform style Copyright © HG Data 2019
  • 72. 1. Change Green From Next to Live Copyright © HG Data 2019
  • 73. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Copyright © HG Data 2019
  • 74. Now both are live! Copyright © HG Data 2019
  • 75. Verify With Curl $ curl http://live.example.com/ I'm an app running in the production environment on green $ curl http://live.example.com/ I'm an app running in the production environment on blue Copyright © HG Data 2019
  • 76. 2. Move Blue From Live To Next Copyright © HG Data 2019
  • 77. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Copyright © HG Data 2019
  • 78. Now green is live, but blue is next Copyright © HG Data 2019
  • 79. Verify With Curl $ curl http://live.example.com/ I'm an app running in the production environment on green $ curl http://next.example.com/ 503 Service Temporarily Unavailable # Wait a couple of minutes... $ curl http://next.example.com/ I'm an app running in the production environment on blue Copyright © HG Data 2019
  • 81. Q&A Thank you! Follow us online @ https://www.hgdata.com