Search code examples
terraformterraform-aws-modules

How to run multipul modules in same file in terraform?


I using https://github.com/cloudposse/terraform-aws-acm-request-certificate to generate certificate using terraform and aws.

How to run multipul domains in the same file in terraform? (not subdomain)

I try this but I have error 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"
}

I looking for solution like:

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

Solution

  • First of all, you are using the same name for both modules. They should be different, e.g.:

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

    Also, in terraform 0.13 you can use foreach for modules.

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

    See the details in the release notes.