简体   繁体   中英

How to access terraform resource attributes of parent directory

I have a terraform module that creates a kafka cluster, within that directory there is a sub directory called "kafka_topics" and within that a terraform module to create kafka topics.

The issue I am having is that I do not know how to access the attributes of the cluster resource from the topic submodule, below is the code that creates the cluster, references to the topic module, calling the cluster from the topic module, and finally the error I get.

Any suggestions on what the correct syntax or best practice on dividing up complex terraform configurations is?

kafka_cluster/main.tf

resource "confluent_kafka_cluster" "basic" {
  display_name = "basic_kafka_cluster"
  availability = "SINGLE_ZONE"
  cloud        = "AWS"
  region       = "eu-central-1"
  basic {}

  environment {
    id = confluent_environment.development.id
  }
}

module "kafka_topics" {
  source = "./kafka_topics"

  confluent_kafka_cluster_id = confluent_kafka_cluster.basic.id
  confluent_kafka_cluser_rest_endpoint = confluent_kafka_cluster.basic.rest_endpoint
}

kafka_cluster/kafka_topics/main.tf

resource "confluent_kafka_topic" "test" {
  kafka_cluster {
    id = module.kafka_cluster.confluent_kafka_cluster.basic.id
  }
  topic_name    = "test"
  rest_endpoint = module.kafka_cluster.confluent_kafka_cluster.basic.rest_endpoint
}

Error message

╷
│ Error: Unsupported argument
│ 
│   on kafka_cluster/main.tf line 74, in module "kafka_topics":
│   74:   confluent_kafka_cluster_id = confluent_kafka_cluster.basic.id
│ 
│ An argument named "confluent_kafka_cluster_id" is not expected here.
╵
╷
│ Error: Unsupported argument
│ 
│   on kafka_cluster/main.tf line 75, in module "kafka_topics":
│   75:   confluent_kafka_cluser_rest_endpoint = confluent_kafka_cluster.basic.rest_endpoint
│ 
│ An argument named "confluent_kafka_cluser_rest_endpoint" is not expected here.

The examples you've shared suggest some confusion about the difference between input variables and output values .

If you are familiar with some general-purpose programming languages it might be helpful to think of a module block as being quite like a function call. The input variables are like the parameters to the function, while the output values are like its return values.

A child module does not have any direct access to data in its caller, but you can declare input variables inside the child module which will then allow the calling module to pass in the necessary data explicitly.

For example, you could write the following in your child module (in the kafka_cluster/kafka_topics directory):

variable "confluent_kafka_cluster_id" {
  type = string
}

variable "confluent_kafka_cluster_rest_endpoint" {
  type = string
}

resource "confluent_kafka_topic" "test" {
  kafka_cluster {
    id = var.confluent_kafka_cluster_id
  }
  topic_name    = "test"
  rest_endpoint = var.confluent_kafka_cluster_rest_endpoint
}

The variable blocks declare which names are allowed in a module block that is calling this module. So now in your calling module (the kafka_cluster directory) you can pass the values into those variables just like you showed in the first example in your question:

module "kafka_topics" {
  source = "./kafka_topics"

  confluent_kafka_cluster_id            = confluent_kafka_cluster.basic.id
  confluent_kafka_cluster_rest_endpoint = confluent_kafka_cluster.basic.rest_endpoint
}

Your kafka_topics module doesn't have any output values yet, but just to give a more complete explanation I'll also show a possible output value so you can see how to pass data "upwards" from the child module back to its parent.

In the child module again, you can declare an output value to return the topic ID:

output "id" {
  value = confluent_kafka_topic.test.id
}

Declaring that output value will then make it valid for your calling module (the kafka_cluster directory) to refer to module.kafka_cluster.id to get the ID. All other details about the topic remain encapsulated inside the module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM