Refer to resource created by module TERRAFORM

Viewed 23

got one question about terraform and reference to resource. Long story short: I have module to create AKS cluster (attachment) and I create cluster form one folder. On other folder I have other module to manage kubernetes it self: like create namespace, deployments etc. How can I refer to this cluster form other folder?enter image description here

1 Answers

As Marko mentioned you would use outputs in the same root module, however if you are applying 1 plan, then the other you would likely need to use data sources. Typically in my root modules I have a data-source.tf file with any pre-existing resources that I need to reference in the root module.

data "kubernetes_service" "example" {
  metadata {
    name = "terraform-example"
  }
}

With the above data source defined, you can use it like this in your root module: data.kubernetes_service.example.attribute-you-want-to-retrieve

Here's a good reference: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service

Related