Search code examples
dnsterraformconditional-statementsamazon-route53amazon-vpc

Using Terraform, how to conditionally include a VPC in a Route53 definition


Using Terraform, I need to associate a second VPC, in my AWS Private Route53 Host Zone definition; but only in my development account. I do not want this second VPC association in test or production accounts. Logically, this is what I would like to do.

resource "aws_route53_zone" "private_dom" {
  name    = "private.dom."
  comment = "Hosted zone for private.dom"

  vpc {
    vpc_id = aws_vpc.application.id
  }

  vpc {
    count  = var.account_name == "development" ? 1 : 0
    vpc_id = aws_vpc.management.id
  }
}

But, Terraform complains that An argument named "count" is not expected here. Any recommendations for how I can make this work?


Solution

  • Use dynamic blocks for it. In your case, it would look like this.

    resource "aws_route53_zone" "private_dom" {
      name    = "private.dom."
      comment = "Hosted zone for private.dom"
    
      vpc {
        vpc_id = aws_vpc.application.id
      }
    
      dynamic "vpc" {
        for_each = var.account_name == "development" ? [1] : []
        content {
          vpc_id = aws_vpc.management.id
        }  
      }
    }