There might be situations where you would like to create a resource conditionally if it does not exist. For that particular case, you can use count property.
resource "aws_route53_zone" "primary" {
count = var.create_dns_zone ? 1 : 0
name = var.domain
}
data "aws_route53_zone" "primary" {
count = var.create_dns_zone ? 0 : 1
name = var.domain
}
locals {
dns_zone_id = var.create_dns_zone ? aws_route53_zone.primary[0].name : data.aws_route53_zone.primary[0].zone_id
subdomain = var.environment_name == "prod" ? "" : "${var.environment_name}."
}