1

我使用https://github.com/cloudposse/terraform-aws-acm-request-certificateterraform 和 aws 生成证书。

如何在 terraform 的同一文件中运行多个域?(不是子域)

我试试这个,但我有错误Error: Duplicate module call

module "acm_request_certificate" {
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "example.com"
  process_domain_validation_options = true
  ttl                               = "300"
}

module "acm_request_certificate" {
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "otherexample.com"
  process_domain_validation_options = true
  ttl                               = "300"
}

我正在寻找解决方案,例如:

const domains = ["example.com", "otherexample.com"]

foreach(domain of domains) {
 module "acm_request_certificate" {
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = domain
  process_domain_validation_options = true
  ttl                               = "300"
 }
}
4

1 回答 1

4

首先,您对两个模块使用相同的名称。它们应该不同,例如:

module "acm_request_certificate_example" {
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "otherexample.com"
  process_domain_validation_options = true
  ttl                               = "300"
}

module "acm_request_certificate_other_example" {
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "otherexample.com"
  process_domain_validation_options = true
  ttl                               = "300"
}

此外,在 terraform 0.13 中,您可以将 foreach 用于模块。

# my_buckets.tf
module "bucket" {
  for_each = toset(["assets", "media"])
  source   = "./publish_bucket"
  name     = "${each.key}_bucket"
}

请参阅发行说明中的​​详细信息。

于 2020-09-19T23:06:31.187 回答