Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding Infrastructure as Code with Terraform

Understanding Infrastructure as Code with Terraform

488c726b68a40da4c3d8bbdff2f4ff8f?s=128

Taylor Dolezal
PRO

July 20, 2020
Tweet

Transcript

  1. None
  2. Copyright © 2020 HashiCorp Understanding Infrastructure as Code with Terraform

  3. Agenda

  4. Agenda Infrastructure as Code what is it?

  5. Agenda Infrastructure as Code what is it? Introduction to Terraform

    putting the pieces together
  6. Agenda Infrastructure as Code what is it? Introduction to Terraform

    putting the pieces together Terraform Lifecycle and State expanding your knowledge
  7. Infrastructure as Code

  8. Infrastructure

  9. Infrastructure as Code

  10. Infrastructure as Code ▪ executable documentation

  11. Infrastructure as Code ▪ executable documentation ▪ enables collaboration

  12. Infrastructure as Code ▪ executable documentation ▪ enables collaboration ▪

    safe and predictable
  13. None
  14. HashiCorp Configuration Language

  15. HashiCorp Configuration Language

  16. HashiCorp Configuration Language CODE EDITOR

  17. HashiCorp Configuration Language CODE EDITOR service { key = "value"

    }
  18. HashiCorp Configuration Language CODE EDITOR service { key = "value"

    }
  19. HashiCorp Configuration Language CODE EDITOR

  20. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { }

  21. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { }

  22. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" }
  23. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } }
  24. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } }
  25. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } }
  26. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } }
  27. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } }
  28. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { }
  29. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  30. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  31. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  32. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  33. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  34. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:${var.port}" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  35. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:${var.port}" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  36. None
  37. None
  38. Introducing Terraform

  39. None
  40. Terraform

  41. Terraform 125+ Official Providers AWS, GCP, OpenNebula, etc.

  42. Terraform 125+ Official Providers AWS, GCP, OpenNebula, etc. 175+ Community

    Providers Auth0, Domino’s Pizza etc.
  43. First Steps

  44. Command: terraform init TERMINAL >

  45. Command: terraform init TERMINAL > terraform init

  46. Command: terraform init TERMINAL > terraform init Initializing the backend...

    Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "aws" (terraform-providers/aws) 2.65.0...
  47. Command: terraform init TERMINAL > terraform init Initializing the backend...

    Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "aws" (terraform-providers/aws) 2.65.0... Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
  48. Command: terraform plan TERMINAL >

  49. Command: terraform plan TERMINAL > terraform plan -out="aws.tfplan"

  50. Command: terraform plan TERMINAL > terraform plan -out="aws.tfplan" Terraform will

    perform the following actions: # aws_instance.open_nebula will be created + resource "aws_instance" "open_nebula" Plan: 1 to add, 0 to change, 0 to destroy. This plan was saved to: aws.tfplan
  51. Command: terraform plan TERMINAL > terraform plan -out="aws.tfplan" Terraform will

    perform the following actions: # aws_instance.open_nebula will be created + resource "aws_instance" "open_nebula" Plan: 1 to add, 0 to change, 0 to destroy. This plan was saved to: aws.tfplan
  52. Command: terraform apply TERMINAL > terraform apply "aws.tfplan"

  53. Command: terraform apply TERMINAL > terraform apply "aws.tfplan" aws_instance.open_nebula: Creating...

  54. Command: terraform apply TERMINAL > terraform apply "aws.tfplan" aws_instance.open_nebula: Creating...

    aws_instance.open_nebula: Still creating... [10s elapsed]
  55. Command: terraform apply TERMINAL > terraform apply "aws.tfplan" aws_instance.open_nebula: Creating...

    aws_instance.open_nebula: Still creating... [10s elapsed] aws_instance.open_nebula: Still creating... [20s elapsed] aws_instance.open_nebula: Creation complete after 22s Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  56. Command: terraform output TERMINAL >

  57. Command: terraform output TERMINAL > terraform output

  58. Command: terraform output TERMINAL > terraform output volume_device_name = /dev/sdh

  59. Command: terraform output TERMINAL > terraform output volume_device_name /dev/sdh

  60. Command: terraform destroy TERMINAL

  61. Command: terraform destroy TERMINAL > terraform plan -destroy -out="aws.tfplan"

  62. Command: terraform destroy TERMINAL > terraform plan -destroy -out="aws.tfplan"

  63. Command: terraform destroy TERMINAL > terraform plan -destroy -out="aws.tfplan"

  64. Command: terraform destroy TERMINAL > terraform apply "aws.tfplan"

  65. Command: terraform destroy TERMINAL > terraform apply "aws.tfplan" aws_ebs_volume.open_nebula: Destroying...

    aws_instance.open_nebula: Destroying... aws_volume_attachment.open_nebula: Destroying…
  66. Command: terraform destroy TERMINAL > terraform apply "aws.tfplan" aws_ebs_volume.open_nebula: Destroying...

    aws_instance.open_nebula: Destroying... aws_volume_attachment.open_nebula: Destroying… Apply complete! Resources: 0 added, 0 changed, 3 destroyed.
  67. Command: terraform fmt TERMINAL

  68. Command: terraform fmt TERMINAL > terraform fmt main.tf

  69. Command: terraform fmt TERMINAL > terraform fmt main.tf

  70. Command: terraform validate TERMINAL > terraform fmt main.tf > terraform

    validate Success! The configuration is valid.
  71. Command: terraform validate TERMINAL > terraform fmt main.tf > terraform

    validate Success! The configuration is valid.
  72. Command: terraform help TERMINAL > terraform help Usage: terraform [-version]

    [-help] <command> [args] The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. Common commands: apply Builds or changes infrastructure destroy Destroy Terraform-managed infrastructure fmt Rewrites config files to canonical format output Read an output from a state file
  73. Terraform Lifecycle and State

  74. Terraform lifecycle

  75. Terraform lifecycle ▪ terraform init

  76. Terraform lifecycle ▪ terraform init ▪ terraform fmt

  77. Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform

    validate
  78. Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform

    validate ▪ terraform plan
  79. Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform

    validate ▪ terraform plan -out=“aws.tfplan"
  80. Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform

    validate ▪ terraform plan -out=“aws.tfplan" ▪ terraform apply "aws.tfplan"
  81. Terraform lifecycle ▪ terraform init ▪ terraform fmt ▪ terraform

    validate ▪ terraform plan -out="aws.tfplan" ▪ terraform apply “aws.tfplan" ▪ terraform plan -destroy
  82. None
  83. Terraform State ▪ maps real-world resources to your configuration

  84. Terraform State ▪ maps real-world resources to your configuration ▪

    keeps track of (resource) metadata
  85. Terraform State ▪ maps real-world resources to your configuration ▪

    keeps track of (resource) metadata ▪ improves performance for large infrastructures
  86. Terraform State ▪ maps real-world resources to your configuration ▪

    keeps track of (resource) metadata ▪ improves performance for large infrastructures ▪ stored locally (by default), can be stored remotely
  87. Terraform State ▪ maps real-world resources to your configuration ▪

    keeps track of (resource) metadata ▪ improves performance for large infrastructures ▪ stored locally (by default), can be stored remotely
  88. Review

  89. Review ▪ Infrastructure as Code

  90. Review ▪ Infrastructure as Code ▪ Introduction to Terraform

  91. Review ▪ Infrastructure as Code ▪ Introduction to Terraform ▪

    Terraform Lifecycle and State
  92. Materials ▪ slides: hashi.co/open-nebula-slides

  93. Materials ▪ slides: hashi.co/open-nebula-slides ▪ guides: hashi.co/tf-learn

  94. Get Started with Terraform Cloud

  95. Thank You tdolezal@hashicorp.com