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 = numberexternal = numberprotocol = string}))default = [{internal = 8300external = 8300protocol = "tcp"}]}
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.type.string, number, bool.list(<TYPE>), set(<TYPE>), map(<TYPE>), object({<ATTR NAME> = <TYPE>, ... }), tuple([<TYPE>, ...]).variable "image_id" {type = stringdescription = "The id of the machine image (AMI) to use for the server."}
variable "image_id" {type = stringdescription = "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-\\"."}}
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 = stringdescription = "The id of the machine image (AMI) to use for the server."validation {# regex(...) fails if it cannot find a matchcondition = can(regex("^ami-", var.image_id))error_message = "The image_id value must be a valid AMI id, starting with \\"ami-\\"."}}
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.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}
-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"}'
.tfvars or .tfvars.json) and specify the file on the command line with -var-file. For example:terraform apply -var-file="testing.tfvars"
image_id = "ami-abc123"availability_zone_names = ["us-east-1a","us-west-1c",]
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"]}
TF_VAR_. For example:export TF_VAR_image_id=ami-abc123export 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.terraform apply is executed.terraform_remote_state data source.output block. For example:output "instance_ip_addr" {value = tencentclould_instance.server.private_ip}
output "instance_ip_addr" {value = tencentclould_instance.server.private_ipdescription = "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. In general, the order for creating 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_ipdescription = "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,]}
locals block. For example:locals {service_name = "forum"owner = "Community Team"}
locals {# Ids for multiple sets of EC2 instances, merged togetherinstance_ids = concat(tencentclould_instance.blue.*.id, tencentclould_instance.green.*.id)}locals {# Common tags to be assigned to all resourcescommon_tags = {Service = local.service_nameOwner = local.owner}}
local.<NAME> expression. For example:resource "tencentclould_instance" "example" {# ...tags = local.common_tags}
Feedback