目录
2.Terraform 使用 for_each 创建资源副本 (DNS)
(1)主机
表1-1 主机
主机 | 系统 | 软件 | 工具 | 备注 |
jia | Windows? | Terraform 1.6.6 | VS Code、?PowerShell、?Chocolatey |
(1)查看项目
(2)函数配置文件
count.tf
locals {
zone = "maojing.site"
records = ["server1", "server2", "server3"]
#records = ["server1","server3"]
}
resource "alicloud_dns_record" "eachrecord" {
for_each = toset(local.records)
name = local.zone
host_record = each.value
type = "A"
value = "192.168.1.1"
}
(3) 版本配置文件
versions.tf
terraform {
required_version = "1.6.6"
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "1.214.1"
}
}
}
(4)变量配置文件
variables.tf
variable "access_key" {
description = "access_key"
}
variable "secret_key" {
description = "secret_key"
}
variable "region" {
type = string
description = "region name"
default = "cn-hangzhou"
sensitive = true
}
(5) 密钥配置文件
terraform.tfvars
(6)登录阿里云系统查看域名
域名状态正常
目前域名解析为空
(7)初始化
terraform init
(8)格式化代码
terraform fmt
(9)验证代码
terraform validate
(10)计划与预览
terraform plan
(11)申请资源
terraform apply
(12)展示资源
terraform show
(12)登录阿里云系统查看域名
域名解析已新增3个
(13)修改函数配置文件
for_each.tf ,移除server2
locals {
zone = "maojing.site"
#records = ["server1", "server2", "server3"]
records = ["server1","server3"]
}
resource "alicloud_dns_record" "eachrecord" {
for_each = toset(local.records)
name = local.zone
host_record = each.value
type = "A"
value = "192.168.1.1"
}
?(14)计划与预览
terraform plan
(15)?申请资源
terraform apply
(16)?展示资源
terraform show
(17)登录阿里云系统查看域名
server2域名解析已移除
(18)销毁资源
terraform destroy
(19)登录阿里云系统查看域名
域名解析已清空