Using variables


Figure 1084. Extracting configuration values Slide presentation
# variables.tf
variable "serverName" {
   description = "Project unique s-name"
   type        = string
   nullable    = false
   default     = "hello"
}
# config.auto.tfvars
# Overriding default value "hello"
serverName = "testServer"
# main.tf
resource "hcloud_server" "mySrv" {
  name = var.serverName
  ...
}
terraform apply

...
serverProps = {
  "ip" = "46.62.254.219"
  "name" = "testServer"
}

Figure 1085. Handling secrets Slide presentation
# variables.tf
variable "hcloud_token" {
   description = "Hetzner API token"
   type        = string
   nullable    = false
   sensitive   = true
}
# .env
# Setting environment variable
export TF_VAR_hcloud_token="utRw..."
# main.tf
resource "hcloud_server" "mySrv" {
  name = var.serverName
  ...
}
. .env # Source environment file

echo $TF_VAR_hcloud_token
utRw...

terraform apply
...

exercise No. 15

Using configuration variables

Q:

Start from Adding ssh public keys and firewall and:

  • Define a variable hcloud_token variable like in Figure 1085, “Handling secrets ” for containing you project's Hetzner API token.

    Create a .env file alongside containing the related secret. Source this file and check for the environment variable TF_VAR_hcloud_token to be set correctly.

  • Define the following hcloud_server properties by variables in a config.auto.tfvars file.

The only difference to the previous exercise is thus the extraction of sensitive and non-sensitive values into configuration files.