terraform-ibm-modules / terraform-ibm-observability-instances

Deploys services for Log Analysis, Activity Tracker, Monitoring, and Cloud Logs
Apache License 2.0
2 stars 2 forks source link

terraform modules cannot self reference #607

Closed 65156 closed 1 hour ago

65156 commented 4 days ago

We are having some issues trying to utilize this module, I believe the problem is coming from the modules self reference.

Blame: https://github.com/terraform-ibm-modules/terraform-ibm-observability-instances/blame/main/examples/advanced/main.tf#L235-L236

Update: ok this seems to be an issue only when using this module in conjunction with a for_each or count as soon as you utilize the at_cloud_logs_targets block.

╷
│ Error: Cycle: module.observability_instances.module.activity_tracker (close), module.observability_instances (close), module.observability_instances.module.activity_tracker.time_sleep.wait_for_cloud_logs_auth_policy, module.observability_instances.module.activity_tracker.ibm_atracker_target.atracker_cloud_logs_targets, module.observability_instances.module.activity_tracker.local.cloud_log_targets (expand), module.observability_instances.module.activity_tracker.local.activity_tracker_targets (expand), module.observability_instances.module.activity_tracker.output.activity_tracker_targets (expand), module.observability_instances.output.activity_tracker_targets (expand), module.observability_instances.var.activity_tracker_routes (expand), module.observability_instances.module.activity_tracker.var.activity_tracker_routes (expand), module.observability_instances.module.activity_tracker.ibm_atracker_route.atracker_routes, module.observability_instances.module.activity_tracker.local.activity_tracker_routes (expand), module.observability_instances.module.activity_tracker.output.activity_tracker_routes (expand), module.observability_instances.output.activity_tracker_routes (expand), module.observability_instances.var.at_cloud_logs_targets (expand), module.observability_instances.module.activity_tracker.var.cloud_logs_targets (expand), module.observability_instances.module.activity_tracker.ibm_iam_authorization_policy.atracker_cloud_logs
│ 
│ 

our provisioning code was as follows

module "observability_instances" {
  for_each  = { for entry in local.observability : entry.id => entry }
  source    = "terraform-ibm-modules/observability-instances/ibm"
  version   = "3.3.1" 
  resource_group_id     = local.resource_group[each.value.rg]
  region                = local.location
  cloud_logs_data_storage = {
    logs_data = {
      enabled         = true
      bucket_crn      = ibm_cos_bucket.cos_bucket[each.value.bucket_ref].crn
      bucket_endpoint = ibm_cos_bucket.cos_bucket[each.value.bucket_ref].resource_instance_id
    }
  }
  at_cloud_logs_targets = [
    {
      instance_id   = module.observability_instances[each.value.id].cloud_logs_crn
      target_region = local.location
      target_name   = join("-", [each.value.name, "at-target"])
    }
  ]
  activity_tracker_routes = [
    {
      locations  = ["*", "global"]
      target_ids = [module.observability_instances[each.value.id].activity_tracker_targets[join("-", [each.value.name, "at-target"])].id]
      route_name = join("-", [each.value.name, "at-route"])
    }
  ]
}
65156 commented 3 days ago

I think it would make a lot more sense if at_cloud_logs_targets, and activity_tracker_routes, instead of consuming the modules own output, by default set these values inside at the resource or submodule level, or if instance id is external to the root module allowed this as input, however conditional logic should be used to differentiate if the root module is to deploy an instance or reference an already provisioned instance.

ocofaigh commented 1 hour ago

Worked with the consumer internally. Each submodule is available to be called on its own if needed: https://github.com/terraform-ibm-modules/terraform-ibm-observability-instances/tree/main/modules

However it is also possible to self reference resources, just like we do in the example: https://github.com/terraform-ibm-modules/terraform-ibm-observability-instances/blob/76577069900611729501d37489e263f21201cab6/examples/advanced/main.tf#L235