如何在 terraform 的同一个文件中 运行 多个模块?

How to run multipul modules in same file in terraform?

我使用 https://github.com/cloudposse/terraform-aws-acm-request-certificate 使用 terraform 和 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"
 }
}

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

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"
}

查看 release notes 中的详细信息。