Infrastructure as Code with Terraform
Tim Berry
About Me
Google Certified Professional Cloud
Architect + Data Engineer
Some RedHat certs
Run the Google Cloud Platform SRE
Team at Claranet
Have a familiar and boring origin
story based around a ZX Spectrum
linkedin.com/in/timhberry
Definition
Infrastructure as code (IaC) is the
process of managing and provisioning
computer data centers through
machine-readable definition files, rather
than physical hardware configuration or
interactive configuration tools.
(Wikipedia)
• Why Infrastructure as Code?
• The Terraform Lifecycle
• Best Practices
Overview
Why Infrastructure as Code?
Infrastructure as Code with Terraform
Infrastructure as Code with Terraform
Infrastructure as Code with Terraform
A software engineering approach to
an operations function:
• Automated
• Quality Managed
• Modular/Re-usable
• Collaborative
• Auditable
Infrastructure as Code
• Developed by HashiCorp
• First released July 2014
• Open source (MPLv2.0)
• Written in Go
• Cloud Agnostic Neutral
• Rapid development
Terraform
• Major cloud vendors (AWS, GCP, Azure)
• PaaS (Heroku)
• SaaS (PagerDuty, DNSimple, CloudFlare)
• Databases (PostgreSQL, MySQL)
• Community providers
Terraform Providers
The Terraform Lifecycle
• Define your infrastructure
• Initialise your execution environment
• Plan your execution
• Apply your changes
• Destroy your infrastructure
Terraform Lifecycle
• A collection of configuration files in a single directory
• Expressed in HCL Terraform syntax or JSON
• Declarations of:
- Providers
- Resources
- Data Sources
Infrastructure Definition
• A collection of configuration files in a single directory
• Expressed in HCL Terraform syntax or JSON
• Declarations of:
- Providers
- Resources
- Data Sources
Infrastructure Definition
s u f n o b
• A collection of configuration files in a single directory
• Expressed in HCL Terraform syntax or JSON
• Declarations of:
- Providers
- Resources
- Data Sources
Infrastructure Definition
s u f n o b
s u f n u y u n o f
• Parameters can be set via TF or environment variables
• Local gcloud auth or service account (recommended)
Provider
• Resource definitions declare what should exist
• Unique to the provider (GCP resources != AWS resources)
• Cloud resources supported by the vendor API generally supported by
Terraform
• Each resource will require a number of arguments
• Each resource can provide attributes for further use
Resources
• Resource type is google_compute_network
• Resource name is bookface
• Everything else is optional
Resources: Define a Network
• Our subnetwork knows its parent network from this interpolation
• Accessing resource attributes: “${<type>.<name>.<attribute>}”
Resources: Define a Subnetwork
• Many resources require other resources to exist first
- subnetwork required network
• Start with the resource you need and work backwards
• A google_compute_instance will require
- subnetwork
- zone
- image
• Terraform will build a Directed Acyclic Graph of resources to ensure
dependencies are met
Resource Dependencies
• Retrieve or compute values to be used in configuration
• Avoids hard-coding
• Gives dynamic values for things like:
- Zones available in a region
- Images available for Compute Engine
Data Sources
Data Sources
• Find available zones in
europe-west2
• All arguments are optional for
google_compute_zones
• Retrieve a link to the latest
version of the image in the
ubuntu-1710 family
• From the ubuntu-os-cloud
project
Resources: Define a VM
• Prepares the environment
• Runs syntax checks
• Downloads providers
• Populates a .terraform directory
terraform init
terraform init
• Creates a plan for what actions to perform
• In other words, a DAG of changes
• Makes a three-way comparison between configuration, stored state
and actual state
• Outputs a plan confirming what will happen
• Use the saved plan file to apply changes
terraform plan
terraform plan
terraform plan
• Actually makes changes
• Will run another plan if you don’t provide one
• Takes anywhere from a few seconds to many minutes depending on
resources being created
• Changes are written to a JSON state file
- Defaults to a local terraform.tfstate
- Previous state preserved in terraform.tfstate.backup
terraform apply
Infrastructure as Code with Terraform
terraform apply
terraform apply
Magic
• Destroys all the resources in state
• Provides a plan of what will be destroyed and prompts you to confirm
• There is no Undo!
terraform destroy
terraform destroy
Wait, seriously, why
Infrastructure as Code?
Living The Infrastructure Dream:
AUDITABLE - REPEATABLE - DEPENDABLE
• Interacting with a UI is none of those things
• Imperatively using a CLI is maybe 1 of those things
• Infrastructure gets BIG
• You don’t want to start from scratch with every project
Because
Best Practices
terraform fmt
• Rewrites configuration files to
a canonical style and format
terraform validate
• Validates syntax of
configuration files
Lint
• Store infrastructure state in the
project itself
• State locks itself during
operations
• Makes code portable and
enables collaboration
• Git all the things
Remote State
• Maintain separate variables per
environment
• Use Makefile to interact with
terraform
params/dev.tfvars
Environments
Makefile
• Reusable collections of resource
definitions and/or data sources
• Can accept arguments and
provide attributes
• Basically just a bunch of .tf files
in a directory
• Community modules available for
deploying common groups of
resources or applications
tf-gcp-standard-subnetwork
• Standard subnetwork
• HA NAT instances
• Public routes and default NAT routes
Modules
• Terrafile: https://github.com/claranet/python-terrafile
Version management for modules
• TfEnv: https://github.com/kamatama41/tfenv
Version management for TF itself
• Exekube: https://github.com/exekube/exekube
Combines Terraform with Helm for complete IaC on Kubernetes
• Pipelines!
(didn’t have time for this talk, sorry)
Worth a Look
• Terraform’s lifecycle is: init, plan, apply and eventually destroy
• Use data sources and variables and be as generic as possible
• Split variables into environment-specific parameter files
• Use git and remote backends for portability
• Use modules to save time
Summary
Questions?
linkedin.com/in/timhberry
www.claranet.co.uk/about-us/careers

More Related Content

PPTX
Comprehensive Terraform Training
PDF
Building infrastructure as code using Terraform - DevOps Krakow
PPTX
Terraform modules restructured
PPTX
Terraform
PDF
Terraform: An Overview & Introduction
PDF
Terraform
PPTX
PPTX
Terraform
Comprehensive Terraform Training
Building infrastructure as code using Terraform - DevOps Krakow
Terraform modules restructured
Terraform
Terraform: An Overview & Introduction
Terraform
Terraform

What's hot (20)

PDF
Terraform introduction
PDF
Terraform 0.12 + Terragrunt
PPTX
Terraform on Azure
PPTX
Infrastructure-as-Code (IaC) using Terraform
PPTX
Effective terraform
PPTX
Terraform on Azure
PDF
Getting Started with Infrastructure as Code
PDF
Terraform modules and best-practices - September 2018
PPTX
Infrastructure as code (iac) - Terraform for AWS
PDF
PDF
Terraform -- Infrastructure as Code
PDF
Advanced Terraform
PDF
Terraform Introduction
PPTX
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
PPTX
Terraform
PDF
Terraform
PPTX
Infrastructure as Code with Terraform.pptx
PPTX
Introduction To Terraform
PPTX
Final terraform
PDF
Best Practices of Infrastructure as Code with Terraform
Terraform introduction
Terraform 0.12 + Terragrunt
Terraform on Azure
Infrastructure-as-Code (IaC) using Terraform
Effective terraform
Terraform on Azure
Getting Started with Infrastructure as Code
Terraform modules and best-practices - September 2018
Infrastructure as code (iac) - Terraform for AWS
Terraform -- Infrastructure as Code
Advanced Terraform
Terraform Introduction
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Terraform
Terraform
Infrastructure as Code with Terraform.pptx
Introduction To Terraform
Final terraform
Best Practices of Infrastructure as Code with Terraform
Ad

Similar to Infrastructure as Code with Terraform (20)

PPTX
Infrastructure as code with terraform and packer
PDF
Azure Day Rome 2019 Reloaded - Effettuare il provisioning su Azure utilizzand...
PDF
Provisioning with Terraform - AzureDay Reloaded
PDF
Introduction to IAC and Terraform
PDF
Terraform vs Pulumi
PPTX
A brief introduction to IaC with Terraform by Kenton Robbins (codeHarbour May...
PPTX
Azure enterprise integration platform
PDF
Terraform: Infrastructure as Code
PPTX
Infrastructure as Code Presentation v5.pptx
PDF
Oracle Cloud deployment with Terraform
PDF
Terraform - Taming Modern Clouds
PPTX
DCRUG: Achieving Development-Production Parity
PPTX
BSidesDFW2022-PurpleTeam_Cloud_Identity.pptx
PDF
Infrastructure as Code for Azure: ARM or Terraform?
PPTX
Cloudify workshop at CCCEU 2014
PPTX
Storage Requirements and Options for Running Spark on Kubernetes
PPTX
Integration-Monday-Terraform-Serverless
PDF
Hpc lunch and learn
PPTX
Spark volume requirements 2018
PDF
20170831 - Greg Palmier: Terraform & AWS at Tempus
Infrastructure as code with terraform and packer
Azure Day Rome 2019 Reloaded - Effettuare il provisioning su Azure utilizzand...
Provisioning with Terraform - AzureDay Reloaded
Introduction to IAC and Terraform
Terraform vs Pulumi
A brief introduction to IaC with Terraform by Kenton Robbins (codeHarbour May...
Azure enterprise integration platform
Terraform: Infrastructure as Code
Infrastructure as Code Presentation v5.pptx
Oracle Cloud deployment with Terraform
Terraform - Taming Modern Clouds
DCRUG: Achieving Development-Production Parity
BSidesDFW2022-PurpleTeam_Cloud_Identity.pptx
Infrastructure as Code for Azure: ARM or Terraform?
Cloudify workshop at CCCEU 2014
Storage Requirements and Options for Running Spark on Kubernetes
Integration-Monday-Terraform-Serverless
Hpc lunch and learn
Spark volume requirements 2018
20170831 - Greg Palmier: Terraform & AWS at Tempus
Ad

Recently uploaded (20)

DOCX
Powerful Ways AIRCONNECT INFOSYSTEMS Pvt Ltd Enhances IT Infrastructure in In...
PDF
Lean-Manufacturing-Tools-Techniques-and-How-To-Use-Them.pdf
PPTX
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
PDF
Understand the Gitlab_presentation_task.pdf
PDF
si manuel quezon at mga nagawa sa bansang pilipinas
PPT
Ethics in Information System - Management Information System
PPTX
Layers_of_the_Earth_Grade7.pptx class by
PDF
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
PPT
250152213-Excitation-SystemWERRT (1).ppt
PDF
Exploring The Internet Of Things(IOT).ppt
PPTX
TITLE DEFENSE entitle the impact of social media on education
PDF
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PPTX
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
PDF
Course Overview and Agenda cloud security
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
PDF
Exploring VPS Hosting Trends for SMBs in 2025
PDF
simpleintnettestmetiaerl for the simple testint
PDF
SlidesGDGoCxRAIS about Google Dialogflow and NotebookLM.pdf
Powerful Ways AIRCONNECT INFOSYSTEMS Pvt Ltd Enhances IT Infrastructure in In...
Lean-Manufacturing-Tools-Techniques-and-How-To-Use-Them.pdf
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
Understand the Gitlab_presentation_task.pdf
si manuel quezon at mga nagawa sa bansang pilipinas
Ethics in Information System - Management Information System
Layers_of_the_Earth_Grade7.pptx class by
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
250152213-Excitation-SystemWERRT (1).ppt
Exploring The Internet Of Things(IOT).ppt
TITLE DEFENSE entitle the impact of social media on education
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
Alethe Consulting Corporate Profile and Solution Aproach
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
Course Overview and Agenda cloud security
Alethe Consulting Corporate Profile and Solution Aproach
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
Exploring VPS Hosting Trends for SMBs in 2025
simpleintnettestmetiaerl for the simple testint
SlidesGDGoCxRAIS about Google Dialogflow and NotebookLM.pdf

Infrastructure as Code with Terraform

  • 1. Infrastructure as Code with Terraform Tim Berry
  • 2. About Me Google Certified Professional Cloud Architect + Data Engineer Some RedHat certs Run the Google Cloud Platform SRE Team at Claranet Have a familiar and boring origin story based around a ZX Spectrum linkedin.com/in/timhberry
  • 3. Definition Infrastructure as code (IaC) is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. (Wikipedia)
  • 4. • Why Infrastructure as Code? • The Terraform Lifecycle • Best Practices Overview
  • 9. A software engineering approach to an operations function: • Automated • Quality Managed • Modular/Re-usable • Collaborative • Auditable Infrastructure as Code
  • 10. • Developed by HashiCorp • First released July 2014 • Open source (MPLv2.0) • Written in Go • Cloud Agnostic Neutral • Rapid development Terraform
  • 11. • Major cloud vendors (AWS, GCP, Azure) • PaaS (Heroku) • SaaS (PagerDuty, DNSimple, CloudFlare) • Databases (PostgreSQL, MySQL) • Community providers Terraform Providers
  • 13. • Define your infrastructure • Initialise your execution environment • Plan your execution • Apply your changes • Destroy your infrastructure Terraform Lifecycle
  • 14. • A collection of configuration files in a single directory • Expressed in HCL Terraform syntax or JSON • Declarations of: - Providers - Resources - Data Sources Infrastructure Definition
  • 15. • A collection of configuration files in a single directory • Expressed in HCL Terraform syntax or JSON • Declarations of: - Providers - Resources - Data Sources Infrastructure Definition s u f n o b
  • 16. • A collection of configuration files in a single directory • Expressed in HCL Terraform syntax or JSON • Declarations of: - Providers - Resources - Data Sources Infrastructure Definition s u f n o b s u f n u y u n o f
  • 17. • Parameters can be set via TF or environment variables • Local gcloud auth or service account (recommended) Provider
  • 18. • Resource definitions declare what should exist • Unique to the provider (GCP resources != AWS resources) • Cloud resources supported by the vendor API generally supported by Terraform • Each resource will require a number of arguments • Each resource can provide attributes for further use Resources
  • 19. • Resource type is google_compute_network • Resource name is bookface • Everything else is optional Resources: Define a Network
  • 20. • Our subnetwork knows its parent network from this interpolation • Accessing resource attributes: “${<type>.<name>.<attribute>}” Resources: Define a Subnetwork
  • 21. • Many resources require other resources to exist first - subnetwork required network • Start with the resource you need and work backwards • A google_compute_instance will require - subnetwork - zone - image • Terraform will build a Directed Acyclic Graph of resources to ensure dependencies are met Resource Dependencies
  • 22. • Retrieve or compute values to be used in configuration • Avoids hard-coding • Gives dynamic values for things like: - Zones available in a region - Images available for Compute Engine Data Sources
  • 23. Data Sources • Find available zones in europe-west2 • All arguments are optional for google_compute_zones • Retrieve a link to the latest version of the image in the ubuntu-1710 family • From the ubuntu-os-cloud project
  • 25. • Prepares the environment • Runs syntax checks • Downloads providers • Populates a .terraform directory terraform init
  • 27. • Creates a plan for what actions to perform • In other words, a DAG of changes • Makes a three-way comparison between configuration, stored state and actual state • Outputs a plan confirming what will happen • Use the saved plan file to apply changes terraform plan
  • 30. • Actually makes changes • Will run another plan if you don’t provide one • Takes anywhere from a few seconds to many minutes depending on resources being created • Changes are written to a JSON state file - Defaults to a local terraform.tfstate - Previous state preserved in terraform.tfstate.backup terraform apply
  • 34. Magic
  • 35. • Destroys all the resources in state • Provides a plan of what will be destroyed and prompts you to confirm • There is no Undo! terraform destroy
  • 38. Living The Infrastructure Dream: AUDITABLE - REPEATABLE - DEPENDABLE • Interacting with a UI is none of those things • Imperatively using a CLI is maybe 1 of those things • Infrastructure gets BIG • You don’t want to start from scratch with every project Because
  • 40. terraform fmt • Rewrites configuration files to a canonical style and format terraform validate • Validates syntax of configuration files Lint
  • 41. • Store infrastructure state in the project itself • State locks itself during operations • Makes code portable and enables collaboration • Git all the things Remote State
  • 42. • Maintain separate variables per environment • Use Makefile to interact with terraform params/dev.tfvars Environments Makefile
  • 43. • Reusable collections of resource definitions and/or data sources • Can accept arguments and provide attributes • Basically just a bunch of .tf files in a directory • Community modules available for deploying common groups of resources or applications tf-gcp-standard-subnetwork • Standard subnetwork • HA NAT instances • Public routes and default NAT routes Modules
  • 44. • Terrafile: https://github.com/claranet/python-terrafile Version management for modules • TfEnv: https://github.com/kamatama41/tfenv Version management for TF itself • Exekube: https://github.com/exekube/exekube Combines Terraform with Helm for complete IaC on Kubernetes • Pipelines! (didn’t have time for this talk, sorry) Worth a Look
  • 45. • Terraform’s lifecycle is: init, plan, apply and eventually destroy • Use data sources and variables and be as generic as possible • Split variables into environment-specific parameter files • Use git and remote backends for portability • Use modules to save time Summary