diff --git a/.gitignore b/.gitignore index 16f29d6..b405283 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Local .terraform directories +_debug **/.terraform/* _scratch diff --git a/modules/1_vpc_support/find_lbs.tf b/modules/1_vpc_support/find_lbs.tf new file mode 100644 index 0000000..c4f560e --- /dev/null +++ b/modules/1_vpc_support/find_lbs.tf @@ -0,0 +1,52 @@ +################################################################ +# Copyright 2025 - IBM Corporation. All rights reserved +# SPDX-License-Identifier: Apache-2.0 +################################################################ + +# ibm_is_lbs - https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_lb#private_ip-6 +# in the resource provider that uses this data, we must add `lifecycle { ignore_changes = all }` + +data "ibm_resource_group" "group" { + name = data.ibm_is_vpc.vpc.resource_group_name +} + +# only get the subnets in the vpc +data "ibm_is_subnets" "subnets" { + vpc = data.ibm_is_vpc.vpc.id + resource_group = data.ibm_resource_group.group.id +} + +# gets all lbs in the region +data "ibm_is_lbs" "lbs" { + // Empty +} + +locals { + subnets_used_in_vpc = [for sn in data.ibm_is_subnets.subnets.subnets: sn.id] + + // Load Balancer Id + subnets_from_lbs_with_ips = flatten([ + for lb in data.ibm_is_lbs.lbs.load_balancers: + flatten([ + for sn in lb.subnets[*]: + flatten([ + for snx in data.ibm_is_subnets.subnets.subnets: + sn.id == snx.id && snx.vpc == data.ibm_is_vpc.vpc.id && length(lb.private_ip) > 0 ? + [{ + id = lb.id + lb_name = lb.name + sn = sn + vpc = data.ibm_is_vpc.vpc.id + private_ip = lb.private_ip[*].address + }] + : [] + ]) + ]) + ]) + + load_balancer_ips = distinct(flatten([for slip in local.subnets_from_lbs_with_ips: + !strcontains(slip.lb_name, "-api-")? + flatten( + [for pip in slip.private_ip: pip]): + []])) +} \ No newline at end of file diff --git a/modules/1_vpc_support/outputs.tf b/modules/1_vpc_support/outputs.tf index d4327da..816a91c 100644 --- a/modules/1_vpc_support/outputs.tf +++ b/modules/1_vpc_support/outputs.tf @@ -26,3 +26,8 @@ output "transit_gateway_name" { output "transit_gateway_status" { value = !var.setup_transit_gateway ? module.existing_gateway[0].existing_tg_status : module.transit_gateway[0].new_tg_status } + +// Highly unlikely this is empty or zero, best to throw an error +output "load_balancer_ips" { + value = load_balancer_ips[0] +} \ No newline at end of file