Course outlines for learning terraform.
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.
Sometimes using input variables is not enough. For example: imagine you want to ask a user for a size
, which can be”
small
medium
large
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.
Let’s split up into a “simple” and “complex” example.
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
}
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
}
locals
to maps size
to these values: Standard_A1_v2, Standard_A2_v2 or Standard_A4_v2 using the below starting point: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 ?"
...
}
The is a value tier = "db-f1-micro"
that needs to be simplified.
Lookup available tier
s here.
Make a locals map:
simple_name | tier name | RAM (GB) |
---|---|---|
small | db-f1-micro | 0.6 |
medium | db-n1-standard-2 | 8 |
large | db-n1-standard-4 | 16 |
google_sql_database_instance
to use your newly created local.variable
and locals
?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
...
}