目录
2.Terraform 使用 count 创建资源副本 (DNS)
(1)主机
表1-1 主机
主机 | 系统 | 软件 | 工具 | 备注 |
jia | Windows? | Terraform 1.6.6 | VS Code、?PowerShell、?Chocolatey |
(1)查看项目
(2)主配置文件
count.tf
# Configure the Alicloud Provider 默认供应商
provider "alicloud" {
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
# 局部变量
locals {
zone = "maojing.site"
records = ["server1", "server2", "server3"]
#records = ["server1","server3"]
}
# count创建资源副本
resource "alicloud_dns_record" "record" {
count = length(local.records)
name = local.zone
host_record = local.records[count.index]
type = "A"
value = "192.168.1.1"
}
(3) 版本配置文件
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)销毁资源
terraform destroy
(14)登录阿里云系统查看域名
域名解析已清空