Search code examples
terraformoracle-cloud-infrastructure

Terraform error because data block output returns Missing resource instance key


I am not sure what I am doing wrong but I just want to ignore the data block output while running plan step using Terraform OCI provider and I am trying to apply snapshot policy to my oci_file_storage_file_system resource. I have tried using the conditional count check but that does not seem to be helping.

What I expect: The snapshot policy ID is an optional parameter to oci_file_storage_file_system resource and ideally it should not fail if I do not provide any input but because I am doing a data lookup using a data block, if I do not provide any input to the data block it fails. So what I want is to ignore the data block output if I do not pass any value through the tfvars. And if I do not pass any value it should just read the state if exists and not fail during plan or apply

Here is the datablock

data "oci_file_storage_filesystem_snapshot_policy" "filesystem_snapshot_policy" {
    count        = var.internal ? 1 : 0
    availability_domain = var.availability_domain
    compartment_id = var.policy_tf_compartment_id
    display_name = var.filesystem_snapshot_policy_display_name
}

module code with resource block


resource "oci_file_storage_file_system" "file_system" {
  #Required
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_id

  #Optional
  defined_tags       = var.defined_tags
  display_name       = var.display_name
  freeform_tags      = var.freeform_tags
  kms_key_id         = var.kms_key_id
  source_snapshot_id = var.source_snapshot_id
  filesystem_snapshot_policy_id = var.internal ? data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy.filesystem_snapshot_policies[0].id : data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy.filesystem_snapshot_policies[0].id
}

error

 Error: Missing resource instance key
 
   on .terraform/file-storage/fss/main.tf line 19, in resource "oci_file_storage_file_system" "file_system":
   19:   filesystem_snapshot_policy_id = var.internal ? data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy.filesystem_snapshot_policies[0].id : data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy.filesystem_snapshot_policies[0].id
 
 Because
 data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy
 has "count" set, its attributes must be accessed on specific instances.
 
 For example, to correlate with indices of a referring resource, use:
     data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy[count.index]

Although I am not sure why I am getting this error as I am already calling the resource as an instance of the list.

Any help will be appreciated in helping how to ignore the data block output and use that in the resource block


Solution

  • so after some struggle I realized what I was doing wrong. Apparently I had to check the length of the returned array from the data block and if the length returns false then skip else check for the next condition. To return the array I did not need the filesystem_snapshot_policies[0].id and I did not need a count variable instead I could do this: filesystem_snapshot_policy_id = length(data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy.filesystem_snapshot_policies) != 0 ? data.oci_file_storage_filesystem_snapshot_policy.filesystem_snapshot_policy.filesystem_snapshot_policies[0].id : ""

    And that solved the problem