Input variables are defined using a variable
block. For example:
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"
}
]
}
The label after the variable
keyword is a name for the variable, which must be unique among all variables in the same module. You can reference the variable value in the code through var.<name>
.
A variable
block can be declared with the following optional arguments:
default
: specifies the default value of the input variable.type
: specifies that the input variable can only be assigned with a specific value.description
: specifies the description of the input variable.validation
: specifies the validation rules for the input variable.sensitive
: limits Terraform UI output when the variable is used in configuration.nullable
: specifies whether the input variable can be null
or not.A type is defined in an input variable block by type
.
string
, number
, bool
.list(<type>)
, set(<type>)
, map(<type>)
, object({<attr name=""> = <type>, ... })
, tuple([<type>, ...])
.You can briefly describe the purpose of each variable. For example:
variable "image_id" {
type = string
description = "The id of the machine image (AMI) to use for the server."
}
Prior to Terraform 0.13.0, only type constraints could be used to ensure that input arguments were of the correct type. Terraform 0.13.0 introduced custom validation rules for input variables. For example:
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-\"."
}
}
The condition
argument is a bool argument. You can use an expression to determine whether the input variable is valid. When the condition
is true
, the input variable is valid; otherwise, it is not. A condition
expression can reference only the currently defined variable by var.<variable name="">
and must not produce errors. If the failure of an expression is the basis of the validation decision, use the can
function to detect such errors. For example:
variable "image_id" {
type = string
description = "The id of the machine image (AMI) to use for the server."
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("^ami-", var.image_id))
error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
}
}
In the above example, if the input image_id
does not meet the requirements of the regex, the regex function call will throw an error, which will be captured by the can
function to output false
. If the condition expression outputs false
, Terraform will return the error message defined in error_message
, which should fully describe the reason for the failure of the input variable validation, along with the valid constraints on the input variable.
You can access an input variable with var.<variable name="">
only within the module where the variable is declared. For example:
resource "tencentclould_instance" "example" {
instance_type = "t2.micro"
ami = var.image_id
}
To specify individual variables on the command line, you need to use the -var
option when running the terraform plan
and terraform apply
commands. For example:
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"}'
When you set a large number of variables, we recommend you specify their values in the variable argument file (suffixed with .tfvars
or .tfvars.json
) and specify the file on the command line with -var-file
. For example:
terraform apply -var-file="testing.tfvars"
Argument definition files use the same basic syntax as Terraform language files but contain only variable name assignments. For example:
image_id = "ami-abc123"
availability_zone_names = [
"us-east-1a",
"us-west-1c",
]
Terraform automatically loads a number of variable definition files:
terraform.tfvars
or terraform.tfvars.json
..auto.tfvars
or .auto.tfvars.json
..json
files need to be defined with the JSON syntax. For example:{
"image_id": "ami-abc123",
"availability_zone_names": ["us-west-1a", "us-west-1c"]
}
You can specify input variables by defining environment variables prefixed with TF_VAR_
. For example:
export TF_VAR_image_id=ami-abc123
export TF_VAR_availability_zone_names='["us-west-1b","us-west-1d"]'
terraform.tfvars
files (if any)terraform.tfvars.json
files (if any).auto.tfvars
or .auto.tfvars.json
files in alphabetical order-var
or -var-file
command line argument, in the order defined in such argument-input=false
argument that disables value passing on the interactive UI.Output variables make information of the infrastructure available for the command line and other Terraform configurations. An output value is similar to a returned value in traditional programming languages.
terraform apply
is executed.terraform_remote_state
data source.Output variables are declared using an output
block. For example:
output "instance_ip_addr" {
value = tencentclould_instance.server.private_ip
}
output "instance_ip_addr" {
value = tencentclould_instance.server.private_ip
description = "The private IP address of the main server instance."
}
terraform plan
and terraform apply
commands are being executed.image_id
argument used to create a virtual machine is queried by data
, then the virtual machine instance depends on this image's data
. Terraform creates data
first and then the virtual machine resource
after the query result is obtained.data
and resource
is automatically calculated by Terraform and does not need to be explicitly specified by the code writer. However, sometimes there are dependencies that cannot be derived through code analysis, in which case the dependencies can be explicitly declared in the code through depends_on
. For example:output "instance_ip_addr" {
value = tencentclould_instance.server.private_ip
description = "The private IP address of the main server instance."
depends_on = [
# Security group rule must be created before this IP address could
# actually be used, otherwise the services will be unreachable.
tencentclould_security_group_rule.local_access,
]
}
If you need to use a complex expression to calculate a value and use it repeatedly, you can assign the complex expression a local value and then reference it repeatedly. If you see the input variable as the function input and output value as the returned value of the function, the local value is equivalent to a local variable defined in the function.
Local variables are declared using a locals
block. For example:
locals {
service_name = "forum"
owner = "Community Team"
}
Additionally, local variables include not only literal constants but also other variables of the module (variables, resource attributes, or other local values) in order to convert or combine them. For example:
locals {
# Ids for multiple sets of EC2 instances, merged together
instance_ids = concat(tencentclould_instance.blue.*.id, tencentclould_instance.green.*.id)
}
locals {
# Common tags to be assigned to all resources
common_tags = {
Service = local.service_name
Owner = local.owner
}
}
Local variables are referenced through the local.<name>
expression. For example:
resource "tencentclould_instance" "example" {
# ...
tags = local.common_tags
}
Was this page helpful?