簡體   English   中英

Terraform --創建多個托管分區並為每個分區分配不同的記錄

[英]Terraform --Create multiple hosted zoned and assign different records for each zones

我是 terraform 的新手,正在嘗試更改現有腳本,我們用來創建 1 個 53 路區域和相應的 53 路記錄,現在的要求是再添加一個區域和 53 條記錄(相應地),我正在嘗試多級 map,我需要你的幫助來更正我的代碼

tf.vars
variable  "facade_hostname" = {
   type = "map"
   default = {
     old_mobile_facade_hostname = "xxx.morgen.nl" 
     new_mobile_facade_hostname = "xxx.test.nl" 
  }
}


dns_config = {

    old_dns_records  = {
      mobile_facade = {
        name    = "xxx.morgen.nl",
        ttl     = "5",
        type    = "A",
        records = [
          "1.2.3.4"]
      }
    },

    new_dns_records  = {
      mobile_facade = {
        name    = "xxx.test.nl",
        ttl     = "5",
        type    = "A",
        records = [
          "5.6.7.8"]
      }
    }

}
varibles.tf

variable "dns_config" {
  type    = map(object({
    name  = string
    ttl =   string
    type  = string
    records = string
  }))
  default = {}
}

variable "facade_hostname" {
  type    = map(object({
    old_mobile_facade_hostname  = string
    new_mobile_facade_hostname =   string
  }))
  default = {}
}

最后是我的資源創建

resource "aws_route53_zone" "private" {
  for_each = var.facade_hostname
  count = var.dns_config != "" && var.facade_hostname != "" ? 1 : 0
  name          = var.facade_hostname
  force_destroy = true
  vpc {
    vpc_id = module.vpc_private.vpc_id
  }
}

resource "aws_route53_record" "A" {
   for_each = var.facade_hostname
  count   = var.dns_config != "" && var.facade_hostname!= "" ? 1 : 0
  zone_id = aws_route53_zone.private[count.index].zone_id
  name    = var.dns_config.facade_hostname.name
  ttl     = var.dns_config.facade_hostname.ttl
  type    = var.dns_config.facade_hostname.type
  records = var.dns_config.facade_hostname.records

  allow_overwrite = true
}

運行 terraform init 時遇到的錯誤

╷
│ Error: Invalid combination of "count" and "for_each"
│
│   on route53.tf line 2, in resource "aws_route53_zone" "private":
│    2:   for_each = var.facade_hostname
│
│ The "count" and "for_each" meta-arguments are mutually-exclusive, only one
│ should be used to be explicit about the number of resources to be created.
╵

╷
│ Error: Invalid combination of "count" and "for_each"
│
│   on route53.tf line 12, in resource "aws_route53_record" "A":
│   12:    for_each = var.facade_hostname
│
│ The "count" and "for_each" meta-arguments are mutually-exclusive, only one
│ should be used to be explicit about the number of resources to be created.
╵

aws-vault: error: exec: Failed to wait for command termination: exit status 1

謝謝

最后,在花了一些時間之后,這似乎是一個可行的解決方案,以防它在未來對任何人有幫助,創建幾個托管區域並根據托管區域創建不同的 A 記錄,

resource "aws_route53_zone" "private" {
  for_each      = var.mobile_facade_hostname
  name          = each.key
  force_destroy = true
  vpc {
    vpc_id = module.vpc_private.vpc_id
  }
}

resource "aws_route53_record" "A" {
  for_each = aws_route53_zone.private
  zone_id  = each.value["zone_id"]
  name     = trimsuffix(each.value["name"], ".")
  type     = "A"
  ttl      = "5"
  records  = [var.mobile_facade_hostname[trimsuffix(each.value["name"], ".")]]

我的變量

mobile_facade_hostname  = { "x.y.nl" = "1.2.3.4", "a.b.nl" = "5.6.7.8" }

變量.tf

variable "mobile_facade_hostname" {
  type    = map(string)
  default = {}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM