6-结构化配置完整实例
展示两种写法(把值写在 variables.tf
和 写在 terraform.tfvars
)
目录结构
project/
├── main.tf
├── variables.tf
├── outputs.tf
main.tf
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = ">= 1.200.0"
}
}
required_version = ">= 1.1.0"
}
provider "alicloud" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}
# 创建多台 ECS 实例
resource "alicloud_instance" "ecs" {
count = var.ecs_count
instance_name = "ecs-${count.index + 1}"
image_id = var.image_id
instance_type = var.instance_type
security_groups = [var.security_group_id]
vswitch_id = var.vswitch_id
internet_max_bandwidth_out = 0
}
# 创建多个 EIP
resource "alicloud_eip" "eip" {
count = var.ecs_count
bandwidth = 5
internet_charge_type = "PayByTraffic"
}
# 绑定 EIP 到对应 ECS
resource "alicloud_eip_association" "assoc" {
count = var.ecs_count
instance_id = alicloud_instance.ecs[count.index].id
allocation_id = alicloud_eip.eip[count.index].id
}
variables.tf
variable "region" {
type = string
default = "cn-beijing"
}
variable "access_key" {
type = string
default = "你的AK"
}
variable "secret_key" {
type = string
default = "你的SK"
}
variable "ecs_count" {
type = number
default = 2
}
variable "image_id" {
type = string
default = "ubuntu_24_04_x64_20G_alibase_20250722.vhd"
}
variable "instance_type" {
type = string
default = "ecs.t5-lc1m1.small"
}
variable "security_group_id" {
type = string
default = "sg-xxxxx"
}
variable "vswitch_id" {
type = string
default = "vsw-xxxxx"
}
outputs.tf
output "private_ips" {
value = [for i in alicloud_instance.ecs : i.private_ip]
}
output "public_ips" {
value = [for e in alicloud_eip.eip : e.ip_address]
}
优点:开箱即用。缺点:所有值都“写死”在 variables.tf
,不适合多环境。
方法二:用 terraform.tfvars
提供实际值(推荐)
目录结构
project/
├── main.tf
├── variables.tf
├── terraform.tfvars
├── outputs.tf
main.tf(跟方法一完全相同)
variables.tf (只定义,不赋默认值)
variable "region" {
type = string
description = "Region of ECS"
}
variable "access_key" {
type = string
description = "Access key"
}
variable "secret_key" {
type = string
description = "Secret key"
}
variable "ecs_count" {
type = number
description = "Number of ECS instances"
}
variable "image_id" {
type = string
description = "Image ID"
}
variable "instance_type" {
type = string
description = "ECS instance type"
}
variable "security_group_id" {
type = string
description = "Security group ID"
}
variable "vswitch_id" {
type = string
description = "VSwitch ID"
}
terraform.tfvars(放真实值)
region = "cn-beijing"
access_key = "你的AK"
secret_key = "你的SK"
ecs_count = 2
image_id = "ubuntu_24_04_x64_20G_alibase_20250722.vhd"
instance_type = "ecs.t5-lc1m1.small"
security_group_id = "sg-xxxxx"
vswitch_id = "vsw-xxxxx"
outputs.tf(跟方法一相同)
output "private_ips" {
value = [for i in alicloud_instance.ecs : i.private_ip]
}
output "public_ips" {
value = [for e in alicloud_eip.eip : e.ip_address]
}
优点:
1. 变量声明和实际值分离,更清晰。
2. 可以有多个 *.tfvars
文件(比如 dev.tfvars
、prod.tfvars
),切换环境时只要:
terraform apply -var-file="prod.tfvars"
总结:
1. 如果只是测试/学习,用 方法一(variables.tf
里赋默认值)就行。
2. 如果要多环境(开发、测试、生产),最好用 方法二(terraform.tfvars
)。