Issues with dynamic block in a nested block

Thanks, that worked.

Currently I have my setup as

  dynamic "package_repositories" {
    for_each = var.package_repositories
    content {
      dynamic "apt" {
        for_each = lookup(package_repositories.value, "apt", [])
        content {
          archive_type = apt.value["archive_type"]
          uri          = apt.value["uri"]
          distribution = apt.value["distribution"]
          components   = apt.value["components"]
          gpg_key      = apt.value["gpg_key"]
        }
      }
      dynamic "yum" {
        for_each = lookup(package_repositories.value, "yum", null)
        content {
          id           = yum.value["id"]
          display_name = yum.value["display_name"]
          base_url     = yum.value["base_url"]
          gpg_keys     = yum.value["gpg_keys"]
        }
      }
    }
  }

variables.tf

  type = list(object({
    apt = list(object({
      archive_type = string
      uri          = string
      distribution = string
      components   = list(string)
      gpg_key      = string
    }))
    yum = list(object({
      id           = string
      display_name = string
      base_url     = string
      gpg_keys     = list(string)
    }))
  }))
  default = []
}

I receive an error about The given value is not valid for variable "package_repositories": element 0: attribute "yum" is required.

What I’d like to have is my variable for package_repositories be descriptive enough to explain what is expected when others use it but ideally you would only need to use apt or yum and not both.

The lookup statement to pass in either [] or null makes sense but it seems like the variable requires yum if it is not declared in my tfvars.

.tfvars

package_repositories = [
  {
    apt = [{
      archive_type = "DEB"
      uri          = "https://packages.cloud.google.com/apt"
      distribution = "cloud-sdk-stretch"
      components   = ["main"]
      gpg_key      = "https://packages.cloud.google.com/apt/doc/apt-key.gpg.asc"
    }]
  },
  {
    apt = [{
      archive_type = "DEB_SRC"
      uri          = "https://packages.cloud.google.com/apt"
      distribution = "cloud-sdk-stretch"
      components   = ["main"]
      gpg_key      = "https://packages.cloud.google.com/apt/doc/apt-key.gpg.asc"
    }]
  }
]