Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 4Naam in de cursus: Objective 4a–4c: resources, references, variables and outputs

Write resources, data sources, variables and outputs

A Terraform configuration is made of blocks: resources create things, data sources read things that exist, variables make the code flexible, outputs return values, locals name repeated expressions.

  • 6 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • resource- en data-blokken, verwijzingen, variabelen en outputs

4.1

Use resource and data blocks

A resource is managed by Terraform (created, changed, deleted). A data source only reads something that already exists.

main.tf
data "azurerm_client_config" "current" {}
 
data "azurerm_resource_group" "shared" {
  name = "rg-shared"
}
 
resource "azurerm_virtual_network" "app" {
  name                = "vnet-app-${var.env}"
  location            = data.azurerm_resource_group.shared.location
  resource_group_name = data.azurerm_resource_group.shared.name
  address_space       = ["10.30.0.0/16"]
}
BlockAddress in codeManaged by Terraform
resource "TYPE" "NAME"TYPE.NAMEYes
data "TYPE" "NAME"data.TYPE.NAMENo, read only

4.2

Reference attributes across resources

Use one resource's attributes in another: TYPE.NAME.ATTRIBUTE. The reference also tells Terraform the order.

network.tf
resource "azurerm_subnet" "web" {
  name                 = "snet-web"
  resource_group_name  = azurerm_virtual_network.app.resource_group_name
  virtual_network_name = azurerm_virtual_network.app.name
  address_prefixes     = ["10.30.1.0/24"]
}
 
resource "azurerm_network_interface" "web" {
  name                = "nic-web1"
  location            = azurerm_virtual_network.app.location
  resource_group_name = azurerm_virtual_network.app.resource_group_name
  ip_configuration {
    name                          = "ipconfig1"
    subnet_id                     = azurerm_subnet.web.id
    private_ip_address_allocation = "Dynamic"
  }
}

4.3

Declare input variables

A variable is a parameter of the configuration, with a type, a default, a description and optional validation.

variables.tf
variable "env" {
  type        = string
  description = "Environment name: dev, test or prod"
  validation {
    condition     = contains(["dev", "test", "prod"], var.env)
    error_message = "env must be dev, test or prod."
  }
}
 
variable "vm_count" {
  type    = number
  default = 2
}
 
variable "admin_ip" {
  type      = string
  sensitive = true
}

4.4

Give values to variables

Values come from several places. Later ones in this list win.

Source (low → high priority)Example
default in the variabledefault = 2
Environment variableexport TF_VAR_env=dev
terraform.tfvars, *.auto.tfvarsenv = "dev"
-var-file=FILEterraform plan -var-file=prod.tfvars
-var on the command lineterraform plan -var env=prod
prod.tfvars
env      = "prod"
vm_count = 3

4.5

Return values with outputs

Outputs print values after apply, and pass values from a module to its caller.

outputs.tf
output "vnet_id" {
  value       = azurerm_virtual_network.app.id
  description = "ID of the app VNet"
}
 
output "db_password" {
  value     = random_password.db.result
  sensitive = true
}
terraform output
terraform output -raw vnet_id
terraform output -json | jq .

4.6

Name repeated values with locals

Locals give a name to an expression used many times — common tags, name prefixes.

locals.tf
locals {
  prefix = "shop-${var.env}"
  tags = {
    env     = var.env
    owner   = "it"
    managed = "terraform"
  }
}
 
resource "azurerm_resource_group" "app" {
  name     = "rg-${local.prefix}"
  location = "westeurope"
  tags     = local.tags
}

Onthoud

var. = input from outside, local. = computed inside, output = returned to the outside.

Alle commando's in dit hoofdstuk