Terraform : Variables - A Beginner's Guide
This article gives an overview of input variables in Terraform.
oracle miscconfigurationintermediate
by OracleDba
14 views
This article gives an overview of input variables in Terraform.
12345678910111213141516171819202122232425
mkdir variables-1-hardcoded
cd variables-1-hardcoded
output "output1" {
value = "hardcoded"
}
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ output1 = "hardcoded"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.123456789101112131415161718192021222324252627282930
mkdir variables-2-defaults
cd variables-2-defaults
variable "my_variable" {
type = string
default = "default-value"
}
output "my_variable_output" {
value = var.my_variable
}
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "default-value"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.1234567891011121314151617181920
cd variables-2-defaults
terraform init
terraform plan -var="my_variable=melon"
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "melon"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.1234567891011121314151617181920212223242526
cd variables-2-defaults
Rem Windows
set TF_VAR_my_variable=banana
# UNIX/Linux
TF_VAR_my_variable=banana
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "banana"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.1234567891011121314151617181920212223242526272829303132
mkdir variables-3-terraform-tfvars
cd variables-3-terraform-tfvars
variable "my_variable" {
type = string
default = "default-value"
}
output "my_variable_output" {
value = var.my_variable
}
my_variable="apple"
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "apple"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
mkdir variables-4-tfvars
cd variables-4-tfvars
variable "my_variable" {
type = string
default = "default-value"
}
output "my_variable_output" {
value = var.my_variable
}
my_variable="orange"
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "banana"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
terraform plan -var-file="my.tfvars"
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "orange"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
terraform plan -var-file="my.tfvars" -var-file="provider.tfvars"1234567891011121314151617181920212223242526272829303132
mkdir variables-5-auto-tfvars
cd variables-5-auto-tfvars
variable "my_variable" {
type = string
default = "default-value"
}
output "my_variable_output" {
value = var.my_variable
}
my_variable="grape"
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = "grape"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
mkdir variables-6-sensitive
cd variables-6-sensitive
variable "my_variable" {
type = string
default = "default-value"
sensitive = true
}
output "my_variable_output" {
value = var.my_variable
}
terraform init
terraform plan -var="my_variable=myPassword"
Error: Output refers to sensitive values
on main.tf line 7:
7: output "my_variable_output" {
Expressions used in outputs can only refer to sensitive values if the
sensitive attribute is true.
variable "my_variable" {
type = string
default = "default-value"
sensitive = true
}
output "my_variable_output" {
value = var.my_variable
sensitive = true
}
terraform plan -var="my_variable=myPassword"
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_variable_output = (sensitive value)
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
mkdir variables-7-validations
cd variables-7-validations
variable "my_password" {
type = string
sensitive = true
validation {
condition = length(var.my_password) >= 8
error_message = "The my_password value must be at least 8 characters long."
}
}
variable "my_email" {
type = string
validation {
condition = can(regex("^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}$", var.my_email))
error_message = "The my_email value must be a valid email address."
}
}
output "my_password_output" {
value = var.my_password
sensitive = true
}
output "my_email_output" {
value = var.my_email
}
terraform init
terraform plan -var="my_password=1234" -var="my_email=me@example"
Error: Invalid value for variable
on main.tf line 1:
1: variable "my_password" {
The my_password value must be at least 8 characters long.
This was checked by the validation rule at main.tf:5,3-13.
terraform plan -var="my_password=12345678" -var="my_email=me@example"
Error: Invalid value for variable
on main.tf line 11:
11: variable "my_email" {
The my_email value must be a valid email address.
This was checked by the validation rule at main.tf:14,3-13.
terraform plan -var="my_password=12345678" -var="[email protected]"
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_email_output = "[email protected]"
+ my_password_output = (sensitive value)
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
mkdir variables-8-complex
cd variables-8-complex
# Variables
variable "my_string_variable" {
type = string
default = "default-value"
}
variable "my_number_variable" {
type = number
default = 0
}
variable "my_boolean_variable" {
type = bool
default = false
}
variable "my_list_variable" {
type = list(string)
default = ["Melon", "Banana", "Apple"]
}
variable "my_object_variable" {
type = object({
name = string
quantity = number
})
default = {name:"Melon", quantity:10}
}
# Outputs
output "my_string_variable_output" {
value = var.my_string_variable
}
output "my_number_variable_output" {
value = var.my_number_variable
}
output "my_boolean_variable_output" {
value = var.my_boolean_variable
}
# Whole list.
output "my_list_variable_output" {
value = var.my_list_variable
}
# First element of list (zero-based).
output "my_list_variable_0_output" {
value = var.my_list_variable[0]
}
# Whole object.
output "my_object_variable_output" {
value = var.my_object_variable
}
# Individual elements of object.
output "my_object_variable_name_output" {
value = var.my_object_variable.name
}
output "my_object_variable_quantity_output" {
value = var.my_object_variable.quantity
}
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ my_boolean_variable_output = false
+ my_list_variable_0_output = "Melon"
+ my_list_variable_output = [
+ "Melon",
+ "Banana",
+ "Apple",
]
+ my_number_variable_output = 0
+ my_object_variable_name_output = "Melon"
+ my_object_variable_output = {
+ name = "Melon"
+ quantity = 10
}
+ my_object_variable_quantity_output = 10
+ my_string_variable_output = "default-value"
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.Please to add comments
No comments yet. Be the first to comment!