Home > Net >  How to DRY repeating block lists in Terraform
How to DRY repeating block lists in Terraform

Time:12-19

I have the following configuration and I would like to avoid the repetition of registrant_contact, admin_contact and tech_contact. How can I make this configuration shorter? I have multiple domains and the same block list repeats in the configuration file again and again:

locals {
  contact = {
    address_line_1 = "Berlin"
    city           = "Berlin"
    contact_type   = "PERSON"
    country_code   = "DE"
    email          = var.aws_billing_mails[0]
    first_name     = "Foo"
    last_name      = "Bar"
    zip_code       = "12345"
    phone_number   = " 49.123456789"
  }
}

resource "aws_route53domains_registered_domain" "my_domain" {
  domain_name        = "mydomain.com"
  auto_renew         = true
  transfer_lock      = true
  registrant_privacy = true
  admin_privacy      = true
  tech_privacy       = true

  registrant_contact {
    address_line_1 = local.contact.address_line_1
    city           = local.contact.city
    contact_type   = local.contact.contact_type
    country_code   = local.contact.country_code
    email          = local.contact.email
    first_name     = local.contact.first_name
    last_name      = local.contact.last_name
    zip_code       = local.contact.zip_code
    phone_number   = local.contact.phone_number
  }

  admin_contact {
    address_line_1 = local.contact.address_line_1
    city           = local.contact.city
    contact_type   = local.contact.contact_type
    country_code   = local.contact.country_code
    email          = local.contact.email
    first_name     = local.contact.first_name
    last_name      = local.contact.last_name
    zip_code       = local.contact.zip_code
    phone_number   = local.contact.phone_number
  }

  tech_contact {
    address_line_1 = local.contact.address_line_1
    city           = local.contact.city
    contact_type   = local.contact.contact_type
    country_code   = local.contact.country_code
    email          = local.contact.email
    first_name     = local.contact.first_name
    last_name      = local.contact.last_name
    zip_code       = local.contact.zip_code
    phone_number   = local.contact.phone_number
  }
}

resource "aws_route53domains_registered_domain" "my_other_domain" {
  domain_name        = "myotherdomain.com"
  auto_renew         = true
  transfer_lock      = true
  registrant_privacy = true
  admin_privacy      = true
  tech_privacy       = true

  registrant_contact {
    address_line_1 = local.contact.address_line_1
    city           = local.contact.city
    contact_type   = local.contact.contact_type
    country_code   = local.contact.country_code
    email          = local.contact.email
    first_name     = local.contact.first_name
    last_name      = local.contact.last_name
    zip_code       = local.contact.zip_code
    phone_number   = local.contact.phone_number
  }

  admin_contact {
    address_line_1 = local.contact.address_line_1
    city           = local.contact.city
    contact_type   = local.contact.contact_type
    country_code   = local.contact.country_code
    email          = local.contact.email
    first_name     = local.contact.first_name
    last_name      = local.contact.last_name
    zip_code       = local.contact.zip_code
    phone_number   = local.contact.phone_number
  }

  tech_contact {
    address_line_1 = local.contact.address_line_1
    city           = local.contact.city
    contact_type   = local.contact.contact_type
    country_code   = local.contact.country_code
    email          = local.contact.email
    first_name     = local.contact.first_name
    last_name      = local.contact.last_name
    zip_code       = local.contact.zip_code
    phone_number   = local.contact.phone_number
  }
}

CodePudding user response:

There is nothing you can do about the blocks, but you can reduce the number of resources. So instead of having two aws_route53domains_registered_domain, you can have only one, with the help of for_each. For example:

locals {
 contact = {
    "mydomain.com" = {
        address_line_1 = "Berlin"
        city           = "Berlin"
        contact_type   = "PERSON"
        country_code   = "DE"
        email          = var.aws_billing_mails[0]
        first_name     = "Foo"
        last_name      = "Bar"
        zip_code       = "12345"
        phone_number   = " 49.123456789"
    },
    
    "myotherdomain.com" = {
        address_line_1 = "Berlin"
        city           = "Berlin"
        contact_type   = "PERSON"
        country_code   = "DE"
        email          = var.aws_billing_mails[0]
        first_name     = "Foo"
        last_name      = "Bar"
        zip_code       = "12345"
        phone_number   = " 49.123456789"
    }
  }

then


resource "aws_route53domains_registered_domain" "domain" {
  
  for_each           = local.contact

  domain_name        = each.key
  auto_renew         = true
  transfer_lock      = true
  registrant_privacy = true
  admin_privacy      = true
  tech_privacy       = true

  registrant_contact {
    address_line_1 = each.value.address_line_1
    city           = each.value.city
    contact_type   = each.value.contact_type
    country_code   = each.value.country_code
    email          = each.value.email
    first_name     = each.value.first_name
    last_name      = each.value.last_name
    zip_code       = each.value.zip_code
    phone_number   = each.value.phone_number
  }

  admin_contact {
    address_line_1 = each.value.address_line_1
    city           = each.value.city
    contact_type   = each.value.contact_type
    country_code   = each.value.country_code
    email          = each.value.email
    first_name     = each.value.first_name
    last_name      = each.value.last_name
    zip_code       = each.value.zip_code
    phone_number   = each.value.phone_number
  }

  tech_contact {
    address_line_1 = each.value.address_line_1
    city           = each.value.city
    contact_type   = each.value.contact_type
    country_code   = each.value.country_code
    email          = each.value.email
    first_name     = each.value.first_name
    last_name      = each.value.last_name
    zip_code       = each.value.zip_code
    phone_number   = each.value.phone_number
  }
}
  • Related