All technological notes.
Variable
3 Types of variables in tf:
Input Variables
Terraform module, so users can customize behavior without editing the source.variable "var_name" {}Output Values
Terraform module.output "output_name" {}Local Values
locals {}Input Variables
variable blockVariable Name
label after the variable keyword is a name for the variable, which must be unique among all variables in the same module.variable "image_id" {
type = string
}
variable "availability_zone_names" {
type = list(string)
default = ["us-west-1a"]
}
variable "docker_ports" {
type = list(object({
internal = number
external = number
protocol = string
}))
default = [
{
internal = 8300
external = 8300
protocol = "tcp"
}
]
}
| optional arguments | desc |
|---|---|
default |
default value . |
type |
specifies what value types are accepted for the variable. |
description |
specifies the input variable’s documentation. |
validation |
A block to define validation rules, usually in addition to type constraints. |
sensitive |
Limits Terraform UI output when the variable is used in configuration. |
nullable |
Specify if the variable can be null within the module. |
any:indicate that any type is acceptable.stringnumberboolset(<TYPE>)list(<TYPE>)tuple([<TYPE>, ...])map(<TYPE>)object({<ATTR NAME> = <TYPE>, ... })main.tf# define a variable
variable "mystr" {
type = string
default = "hello terrform"
}
variable "mymap" {
type = map(string)
default = {
mykey = "my value"
}
}
variable "mylist" {
type = list(any)
default = [1, 2, 3]
}
terrafor console
# > var.mystr
# "hello terrform"
# > var.mymap
# tomap({
# "mykey" = "my value"
# })
# > var.mymap["mykey"]
# "my value"
# > var.mylist
# tolist([
# 1,
# 2,
# 3,
# ])
# > var.mylist[0]
# 1
custom validation rules for a particular variable by adding a validation block within the corresponding variable block.variable "image_id" {
type = string
description = "The id of the machine image (AMI) to use for the server."
validation {
condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
}
}
User case:
When the sensitive imformation become the part of the id of the resource, it’ll be disclosed.
variable "password" {
type = string
sensitive = true
}
resource "resource_type" "resource_name" {
parameter = var.password
}
var.<NAME>, where <NAME> matches the label given in the declaration block.var.resource "aws_instance" "example" {
instance_type = "t2.micro"
ami = var.image_id
}
-var option when running the terraform plan and terraform apply commands:-var option multiple times in a single command to set several different variables.terraform apply -var="image_id=ami-abc123"
terraform apply -var='image_id_list=["ami-abc123","ami-def456"]' -var="instance_type=t2.micro"
terraform apply -var='image_id_map={"us-east-1":"ami-abc123","us-east-2":"ami-def456"}'
# The above examples show appropriate syntax for Unix-style shells, such as on Linux or macOS.
.tfvars) Filesvariable definitions file uses the same basic syntax as Terraform language files, but consists only of variable name assignments:image_id = "ami-abc123"
availability_zone_names = [
"us-east-1a",
"us-west-1c",
]
variable definitions file (with a filename ending in either .tfvars or .tfvars.json) and then specify that file on the command line with -var-file:# Linux, Mac OS, and UNIX:
terraform apply -var-file="testing.tfvars"
# PowerShell:
terraform apply -var-file='testing.tfvars'
# Windows cmd.exe:
terraform apply -var-file="testing.tfvars"
Terraform also automatically loads a number of variable definitions files if they are present:
terraform.tfvars or terraform.tfvars.json..auto.tfvars or .auto.tfvars.json.Files whose names end with .json are parsed instead as JSON objects, with the root object properties corresponding to variable names:
{
"image_id": "ami-abc123",
"availability_zone_names": ["us-west-1a", "us-west-1c"]
}
As a fallback for the other ways of defining variables, Terraform searches the environment of its own process for environment variables named TF_VAR_ followed by the name of a declared variable.
export TF_VAR_image_id=ami-abc123
terraform plan
Output values
| Argument | desc |
|---|---|
value |
Mandatory, the value of the output variable |
description |
the description of the output variable |
sensitive |
Whether the variable is sensitive |
depends_on |
Specify the another resource on which the variable depends |
output keywordroot module, this name is displayed to the user;child module, it can be used to access the output’s value.output "instance_ip_addr" {
value = aws_instance.server.private_ip
# In this example, the expression refers to the private_ip attribute exposed by an aws_instance resource defined elsewhere in this module (not shown).
}
local value
Local values are like a function’s temporary local variables.
locals {
service_name = "forum"
owner = "Community Team"
}
locals {
# Ids for multiple sets of EC2 instances, merged together
instance_ids = concat(aws_instance.blue.*.id, aws_instance.green.*.id)
}
locals {
# Common tags to be assigned to all resources
common_tags = {
Service = local.service_name
Owner = local.owner
}
}
Once a local value is declared, you can reference it in expressions as local.<NAME>.
resource "aws_instance" "example" {
# ...
tags = local.common_tags
}