Error: Invalid index data.*.availability_domains is list of object with 1 elmt given index is > = to the length of collection

Hi Hashi team,
I’m having this index count error which I already encountered in the past each time I am running a terraform configuration a region that has only one AZ/AV (not just oracle cloud).
it seems it only works when the region has 2 or more Availability Zone like Toronto. here’s how it happens : I’m just tired of hardcoding availability domain(zones) each time .
Thanks alot : )

1. env variable

export TF_VAR_availability_domain_number="1"
----- Variables :
variable "availability_domain_name" {
  default     = ""
  description = "Availability Domain"
}

variable "availability_domain_number" {
  default     = 1
  description = "OCI Availability Domains: 1,2,3  (subject to region availability)"
}

2. data source the availability domain + fault domain

# ------ Get the Tenancy ID and AD Number
data "oci_identity_availability_domain" "ad" {
  compartment_id = var.tenancy_ocid
  ad_number      = var.availability_domain_number  <<--
}


# ------ Get the Tenancy ID and ADs
data "oci_identity_availability_domains" "ads" {
  #Required
  compartment_id = var.tenancy_ocid
}

# ------ Get the Faulte Domain within AD 
data "oci_identity_fault_domains" "fds" {
  availability_domain = "${data.oci_identity_availability_domain.ad.name}"
  compartment_id      = var.compute_compartment_ocid

  depends_on = [
    data.oci_identity_availability_domain.ad,
  ]
}

3. the reference :

# ------ Create Web Standalone VMs
resource "oci_core_instance" "web-vms" {
  count = 2

  availability_domain = (var.availability_domain_name != "" ? var.availability_domain_name : data.oci_identity_availability_domains.ads.availability_domains[count.index + 1].name)
  compartment_id      = var.compute_compartment_ocid
  display_name        = "${var.vm_display_name_web}-${count.index + 1}"
  shape               = var.spoke_vm_compute_shape
  fault_domain        = data.oci_identity_fault_domains.fds.fault_domains[count.index].name

  dynamic "shape_config" {
    for_each = local.is_spoke_flex_shape
    content {
      ocpus = shape_config.value
    }
  }

4. The index error

│ Error: Invalid index
│
│   on compute.tf line 235, in resource "oci_core_instance" "web-vms":
│  235:   availability_domain = (var.availability_domain_name != "" ? var.availability_domain_name : data.oci_identity_availability_domains.ads.availability_domains[count.index + 1].name)
│     ├────────────────
│     │ count.index is 1
│     │ data.oci_identity_availability_domains.ads.availability_domains is list of object with 1 element
│
│ The given key does not identify an element in this collection value.
there is only one ad here .

Question: Anything I can do to fix this without hardcoding “availability_domain”? I say this because there are tons of reference stack coded this way.

Problem Fixed .
add this : ( length(data.oci_identity_availability_domains.ads.availability_domains) == 1 ? data.oci_identity_availability_domains.ads.availability_domains[0].name
to add the case when there’s only one availability zone(domain).

resource "oci_core_instance" "web-vms" {
  count = 2
  availability_domain = (var.availability_domain_name != "" ? var.availability_domain_name : ( length(data.oci_identity_availability_domains.ads.availability_domains) == 1 ? data.oci_identity_availability_domains.ads.availability_domains[0].name : data.oci_identity_availability_domains.ads.availability_domains[count.index + 1].name))
  ...

can be closed
Thanks