learn-terraform

Course outlines for learning terraform.

View the Project on GitHub robertdebock/learn-terraform

local values

expected time requirements
45 minutes A computer with Terraform installed, terraform knowledge.

Goal: Be able to create and use locals to further abstract your data.

Explanation

Sometimes using input variables is not enough. For example: imagine you want to ask a user for a size, which can be”

And where the deployment uses much more technical terms to indicate the size. In that case, using a locals is a great way to map input variables to sizes.

You can also describe locals as variables that are not exposed; a user shouldn’t/can’t overwrite them. In situations where you refer to a certain value many times, you can place them in a local variable.

Howto

Let’s split up into a “simple” and “complex” example.

Simple

You typically add locals to a new file, like locals.tf:

locals {
  my_local = "xyz"
}

You can reuse these locals in your terraform code:

resource "x" "default" {
  parameter = local.my_local
}

More complicated

locals.tf

You can also use locals to lookup values and define data structures.

Here you define a map of data.

locals {
  azure_data_map = {
    development = {
      location = "West Europe"
      size     = "small"
    }
    production = {
      location = "Central US"
      size     = "medium"
    }
  }
  location  = local.azure_data_map[var.environment].location
  size      = local.azure_data_map[var.environment].size
}

In the example above, you need variable called environment to map values.

variables.tf

variable "environment" {
  type    = string
  default = "development"
  validation {
    condition     = contains(["development", "production"])
    error_message = "Please use "development" or "production" for the environment."
  }
}

Now you can simply call local.location or local.size in your main.tf. (Or anywhere else…)

main.tf

resource "azurerm_resource_group" "rg" {
  name     = "rg-robertdebock-sbx"
  location = local.location
}

Assignment Azure

variable "size" {
  description = "The size of the deployment."
  type        = string
  default     = "small"
  validation {
    condition     = contains(["small", "medium", "large"], var.size)
    error_message = "Please use \"small\", \"medium\" or \"large\"."
  }
}

resource "azurerm_virtual_machine" "default" {
  name    = "my_name"
  vm_size = "? FILL ME IN ?"
  ...
}

Assignment GCP

The is a value tier = "db-f1-micro" that needs to be simplified.

simple_name tier name RAM (GB)
small db-f1-micro 0.6
medium db-n1-standard-2 8
large db-n1-standard-4 16

Questions

  1. What is the difference between variable and locals?

Solution

variable "size" {
  description = "The size of the deployment."
  type        = string
  default     = "small"
  validation {
    condition     = contains(["small", "medium", "large"], var.size)
    error_message = "Please use \"small\", \"medium\" or \"large\"."
  }
}

locals {
  _vm_sizes = {
    small  = "Standard_A1_v2"
    medium = "Standard_A2_v2"
    large  = "Standard_A4_v2"
  }
  vm_size = local._vm_sizes[var.size]
}

resource "azurerm_virtual_machine" "default" {
  name    = "my_name"
  vm_size = local.vm_size
  ...
}

Sources