产品概述
产品优势
应用场景
variable块进行定义。例如: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关键字后为变量名。在一个 Terraform 模块(同一个文件夹中的所有 Terraform 代码文件,不包含子文件夹)中变量名必须唯一。在代码中可以通过 var.<NAME> 的方式引用变量的值。
variable块可以使用下列的可选参数进行声明:default:指定输入变量默认值。type:指定输入变量只能被赋予特定的值。description: 指定输入变量的描述。validation:指定输入变量的验证规则。sensitive:在配置中使用变量时限制 Terraform UI 输出。nullable:指定输入变量是否可以为null。string、number、boollist(<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-\\"."}}
var.<变量名称>引用当前定义的变量,并且它的计算不能产生错误。若表达式的计算产生错误是输入变量验证的一种判定手段,那么可以使用 can 函数来判定表达式的执行是否抛错。例如: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-\\"."}}
var.<变量名称> 的形式访问,只能在声明该变量的模块内访问。例如:resource "tencentclould_instance" "example" {instance_type = "t2.micro"ami = var.image_id}
terraform plan 和 terraform apply 命令时使用 -var 选项。例如: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"}'
terraform apply -var-file="testing.tfvars"
image_id = "ami-abc123"availability_zone_names = ["us-east-1a","us-west-1c",]
terraform.tfvars 或 terraform.tfvars.json 的文件。.auto.tfvars 或 .auto.tfvars.json 结尾的文件。.json结尾的文件,需要使用 JSON 语法定义。例如:{"image_id": "ami-abc123","availability_zone_names": ["us-west-1a", "us-west-1c"]}
TF_VAR_为前缀的环境变量来指定输入变量。例如:export TF_VAR_image_id=ami-abc123export TF_VAR_availability_zone_names='["us-west-1b","us-west-1d"]'
terraform.tfvars 文件(若存在)。terraform.tfvars.json 文件(若存在)。.auto.tfvars 或 .auto.tfvars.json 文件,以字母顺序排序处理。terraform apply后在 CLI 输出中打印某些值。output块进行声明。例如: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和terraform apply时,在CLI中隐藏输出变量的值。image_id参数是通过 data查询得到的,那么虚拟机实例就依赖于这个镜像的 data。Terraform 会首先创建 data,得到查询结果后,再创建虚拟机 resource。一般来说,data及 resource之间的创建顺序是由 Terraform 自动计算的,不需要代码的编写者显式指定。但有时有些依赖关系无法通过分析代码得出,此时可以在代码中通过 depends_on显式声明依赖关系。例如: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块进行声明。例如: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>表达式引用本地变量。例如:resource "tencentclould_instance" "example" {# ...tags = local.common_tags}
文档反馈