IaC基础设施即代码:Terraform 使用for_each 创建DNS资源副本

发布时间:2024年01月19日

目录

??一、实验

1.环境

2.Terraform 使用 for_each 创建资源副本 (DNS)


??一、实验

1.环境

(1)主机

表1-1 主机

主机系统软件工具备注
jia

Windows?

Terraform 1.6.6VS Code、?PowerShell、?Chocolatey

2.Terraform 使用 for_each 创建资源副本 (DNS)

(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)登录阿里云系统查看域名

域名解析已清空

文章来源:https://blog.csdn.net/cronaldo91/article/details/135693593
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。