0

我正在编写 terraform 脚本来自动为域提供 acm,我面临的问题是如何合并域和 subject_alternative_names,就像它应该从 domain_name 中选择第一个域并将其与 subject_alternative_name 中的第一个块合并并继续。

变量.tf

variable "domain_name" {
  description = "Configuration for alb settings"
  default = [
    "domain.com",
    "helloworld.com",
    "helloworld2.com",
  ]
}
variable "subject_alternative_names" {
  description = "subject_alternative_names"
  default = [ {
    domain.com = {
    "domain.com",
    "domain2.com",
    "domain3.com",
    },
    helloworld.com = {
    "helloworld1.com",
    "helloworld2.com"
    },
    hiworld.com = {
    "hiworld1.com",
    "hiworld2.com"
    }
  }]
}
variable "region" {
  description = "name of the region"
  default     = "us-east-1"
}
variable "validation_method" {
  description = "name of the region"
  default     = "DNS"
}
variable "tags" {
  description = "name of the region"
  default     = "Test"
}

工作变量.tf

variable "domain_name" {
  description = "Configuration for alb settings"
  default     = [
    "domain.com",
    "helloworld.com",
    "helloworld2.com",
    "helloworld1.com",
    "helloworld3.com",
  ]
}
variable "subject_alternative_names"{
  description = "subject_alternative_names"
  default     = [
    "domain.com",
    "helloworld.com",
    "helloworld2.com",
    "helloworld1.com",
    "helloworld3.com",
  ]
}
variable "region" {
  description = "name of the region"
  default     = "us-east-1"
}
variable "validation_method" {
  description = "name of the region"
  default     = "DNS"
}
variable "tags" {
  description = "name of the region"
  default     = "Test"
}

主文件

module "acm" {
  count                     = length(var.domain_name)
  source                    = "./modules/acm"
  domain_name               = var.domain_name[count.index]
  validation_method         = var.validation_method
  tags                      = var.tags
  subject_alternative_names = var.subject_alternative_names
}

资源.tf

variable "domain_name" {
  default     = ""
  description = "Nmae of the domain"
}

variable "validation_method" {
  default     = ""
  description = "Validation method DNS or EMAIL"
}

variable "tags" {
  default     = ""
  description = "tags for the ACM certificate"
}

variable "subject_alternative_names" {
  default     = ""
  description = "subject_alternative_names"
}

resource "aws_acm_certificate" "acm_cert" {
  domain_name               = var.domain_name
  validation_method         = var.validation_method
  subject_alternative_names = var.subject_alternative_names
  lifecycle {
    create_before_destroy = true
  }
  tags = {
    Name = var.tags
  }
}
4

1 回答 1

2

最简单的方法是使用单个地图

variable "domain_name_with_alternate_names" {
  default = {
    "domain.com" = [
      "domain.com",
      "domain2.com",
      "domain3.com",
    ],
    "helloworld.com" = [
      "helloworld1.com",
      "helloworld2.com"
    ],
    "hiworld.com" = [
      "hiworld1.com",
      "hiworld2.com"
    ],
    "hiwodd4.com" = []
  }
}


module "acm" {

  for_each                  = var.domain_name_with_alternate_names
  
  source                    = "./modules/acm"
  domain_name               = each.key
  validation_method         = var.validation_method
  tags                      = var.tags
  subject_alternative_names = each.value
}
于 2021-08-05T06:55:08.793 回答