Skip to content

Terraform

Commands

  • init: applies terraform configuration changes; e.g. adding a new provider
  • plan: shows what actions would be performed
  • apply: performs actions to ensure any changes from the spec files with the state-file are applied
  • output: shows output variables defined
  • destroy: destroys all resources that have been provisioned so far

Templates

  • a text file that contains terraform templates constructs
    • interpolated references such as ${var1}
    • conditional text blocks (similar to jinja)
  • They can be read and evaluated by templatefile(<file-path>, {var1: var.my_key, ...}) function

Terraform language

  • top-level sections
  • functions: allow to derive values based on existing values. e.g. cidersubnet(aws_vpc.my_web.cider_block, 3, 1) creates a subnet within a VPC
  • interpolation: reference a terraform expression inside a string; e.g. "${path.module}/scripts/run.sh"
  • references: to attributes of other sections including variables

Sections

  • terraform: configuration, typically contains required_providers that pins provider versions
  • provider <provider-name>: configuration specific to a provider
  • variable <var-name>: defines a variable that can be injected using terraform CLI or an environment variable with a prefix TF_VAR_<variable-name>
  • data <type> <name>: queries the applicable provider to obtain a resource that matches specific criteria; e.g. instead of hard-coding AMI, search for the latest AMI that has a specific prefix (e.g. debian11-*)
  • resource <type> <name>: defines a resource with a type (e.g. awc_vpc) that is specific for a provider
  • output <name>: defines a values that can be printed using output command