trickyricky
trickyricky

Reputation: 1

Terraform - Environment Handling Variables

I am trying to create DNS records for my various environments but for production I would like to use the apex domain directly; for most environments it returns dev or tst but I'd like for prd to return just the apex domain, without the environment subdomain

Currently:

run.tf

# Create Environment Hosted Zone
resource "aws_route53_zone" "r53_zone_env" {
    name    = "${var.Env}.${var.ApexDomain}"
}

This returns = dev.google.com or tst.google.com

I tried something along these lines with no luck:

resource "aws_route53_zone" "r53_zone_env2" {
    comment = "dummy"

    dynamic "name" {
        for_each = var.Env != "prd" ? [] : ["1"]
        content {
            name = "${var.Env}.google.com"
        }
    },
    dynamic "name" {
        for_each = var.Env == "prd" ? [] : ["1"]
        content {
            name = "google.com"
        }
    }
}

Any guidance is much appreciated!

R,

Upvotes: 0

Views: 57

Answers (1)

luk2302
luk2302

Reputation: 57184

Create a local variable

locals {    
    domain = var.Env == "prd" ? var.ApexDomain : "${var.Env}.${var.ApexDomain}"
}

and then use it via

resource "aws_route53_zone" "r53_zone_env" {
    name = local.domain
}

Upvotes: 1

Related Questions