oracle-quickstart / oci-fortinet

Terraform module to deploy Fortinet on Oracle Cloud Infrastructure (OCI)
Apache License 2.0
5 stars 8 forks source link

Error: Invalid index data.oci_identity_availability_domains.ads.availability_domains is list of object with 1 element #8

Open brokedba opened 1 year ago

brokedba commented 1 year ago

I am running this drg-ha-use-case terraform configuration a region that has only one Availability domain (ca-toronto-1) and the data sourcing of the availability domain is failing the whole execution plan in terraform .

see error below

env variable
export TF_VAR_availability_domain_number="1"
$ terraform plan
│ 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.
╵
╷
│ 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 0
│     │ 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: the given index is greater than or equal to the length of the collection.
╵
╷
│ Error: Invalid index
│
│   on compute.tf line 276, in resource "oci_core_instance" "db-vms":
│  276:   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.
╵
╷
│ Error: Invalid index
│
│   on compute.tf line 276, in resource "oci_core_instance" "db-vms":
│  276:   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 0
│     │ 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: the given index is greater than or equal to the length of the collection.

This makes me think this tf stack was not tested in a Region with unique availability domain. Could you please help with a fix that doesn't imply hardcoding “availability_domain”?

count.index will always be equal 0 on single AD regions .

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

thank you

 terraform --version
Terraform v1.0.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/oci v4.104.2
+ provider registry.terraform.io/hashicorp/template v2.2.0
brokedba commented 1 year ago

Dear team , please check your tf code next time . I wasted quite a time debugging this . Problem fixed You forgot to put the full availability domain conditional expression on 2 of your vm resources (4 vms total). "web-vms" and "db-vms" You should add this : ( length(data.oci_identity_availability_domains.ads.availability_domains) == 1 ? data.oci_identity_availability_domains.ads.availability_domains[0].name like you did for Primary/Secondary FortiGate VM.

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))
  ...

I don't even understand the logic of seeking the name of the count.index+1 availability domain . say you want to create 3 vms and your region has 2 Availability domain . count.index+1 = 1 then >> 2 then >>3

The 2nd and 3rd one will always fail because there is only 2 Availability domain hence index list [0,1].

Anyway I can close the issue now I guess