oracle-terraform-modules/terraform-oci-tdf-network-security

NSG have issue with count, for_each or dynamic block will help ..

tutorialbyexample opened this issue · 0 comments

Thanks for this post, however, I found issues while considering production implementation, as while using count in any resource or module, that resource getting destroy and create again once we are doing any addition or change in between resource as index position getting changed and due to that all other resource destroying and creating again. for example lets see if we want to create 4 NSGs and its created with index
nsgs = merge(
var.nsg_db,
var.nsg_app,
var.test,
var.test1
)

module.oci_security_policies.oci_core_network_security_group.this[0], module.oci_security_policies.oci_core_network_security_group.this[1]
module.oci_security_policies.oci_core_network_security_group.this[2]
module.oci_security_policies.oci_core_network_security_group.this[3]

Later due to any issue if we remove one items form var.test, then all other will recreate due to index position changes and all other application need to updated with newly created nsg ocid.

nsgs = merge(
var.nsg_db,
var.nsg_app,
var.test,
var.test1
)

resource definitions

resource "oci_core_network_security_group" "this" {
count = length(local.nsgs_keys)
compartment_id = var.nsgs[local.nsgs_keys[count.index]].compartment_id != null ? var.nsgs[local.nsgs_keys[count.index]].compartment_id : var.default_compartment_id
vcn_id = var.vcn_id
display_name = local.nsgs_keys[count.index] != null ? local.nsgs_keys[count.index] : "${local.default_nsgs_opt.display_name}-${count.index}"
defined_tags = var.nsgs[local.nsgs_keys[count.index]].defined_tags != null ? var.nsgs[local.nsgs_keys[count.index]].defined_tags : var.default_defined_tags
freeform_tags = var.nsgs[local.nsgs_keys[count.index]].freeform_tags != null ? var.nsgs[local.nsgs_keys[count.index]].freeform_tags : var.default_freeform_tags
}

Either dynamic block or for_each will help, please assist.