Creating terraform for OCI provider. I am trying to create an OCI instance and then later attach a VNIC to it. I am attempting to do this by creating an output list with the display name and id of the compute instance.
Then after this, I have a module which creates the VNIC and I state the display_name of the compute instance I want to attach it to, searching the output from the compute instance module by the display_name and returning the instance id. The problem I have is when I don’t know how to create a list from the output of the instance and it turns into an object.
this is the error message,
Error: Error in function call
on ../common/networking/vnic_attachment/main.tf line 11, in resource "oci_core_vnic_attachment" "this"
11: instance_id = var.compute_ids[index(var.compute_ids.*.name, var.vnic_attachments[count.index].instance_name)].ocid
|----------------
| count.index is 0
| var.compute_ids is object with 2 attributes
| var.vnic_attachments is list of object with 2 elements
Call to function "index" failed: item not found.
This is my code,
creating compute instance,
locals {
compute_ocids = oci_core_instance.this.*.id
compute_names = oci_core_instance.this.*.display_name
}
resource oci_core_instance this {
count = length(var.compute_instances)
availability_domain = var.compute_ad
compartment_id = var.compute_compartment_ocid
display_name = var.compute_instances[count.index].instance_name
shape = var.compute_instances[count.index].instance_shape
state = var.compute_instances[count.index].instance_state # ToDo: Change to parameter, if this needs to be configurable
fault_domain = var.compute_instances[count.index].instance_fault_domain
create_vnic_details {
subnet_id = var.vcn_subnets[index(var.vcn_subnets.*.name, var.compute_instances[count.index].instance_subnet)].ocid # Lookup Subnet OCID from the Subnet Map using the Subnet Name
assign_public_ip = false
}
source_details {
boot_volume_size_in_gbs = var.compute_instances[count.index].instance_block_volume
source_type = var.compute_instances[count.index].instance_source_type
source_id = var.compute_instances[count.index].instance_source_ocid
}
shape_config {
memory_in_gbs = var.compute_instances[count.index].instance_shape_mem
ocpus = var.compute_instances[count.index].instance_shape_ocpus
}
}
output compute_ocids {
value = {
name = local.compute_names
ocid = local.compute_ocids
}
description = "Compute OCIDs"
}
The code which creates the VNIC,
resource oci_core_vnic_attachment this {
count = length(var.vnic_attachments)
create_vnic_details {
display_name = var.vnic_attachments[count.index].vnic_name
nsg_ids = var.vnic_attachment_create_vnic_details_nsg_ids
subnet_id = var.vcn_subnets[index(var.vcn_subnets.*.name, var.vnic_attachments[count.index].vnic_subnet)].ocid
}
instance_id = var.compute_ids[index(var.compute_ids.*.name, var.vnic_attachments[count.index].instance_name)].ocid
}
How do I make the output of a looped resource a list?