You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何将Terraform中转模块调用的Git源模块输出值传递至根模块

Solution: Forward GitHub Module Outputs to Your Root Terraform Module

Got it, let's work through this output forwarding issue step by step. The core problem here is that your local vm module is using a nested GitHub-hosted module, but you haven't set up your local module to pass those nested outputs up to the root module. Here's exactly how to fix it:

Step 1: Create an outputs.tf in your local vm module

First, navigate to ../modules/vm/ and create a new file named outputs.tf. This file acts as a bridge, taking outputs from the GitHub module and exposing them to your root module.

You'll need to reference the exact output names from the GitHub module (you mentioned you can already access these, so you should know what they're called—let's assume the IP output is named instance_ip for this example). Here's what the file should look like:

# ../modules/vm/outputs.tf
output "instance_ip" {
  type        = string
  description = "Public IP address of the provisioned GCP virtual machine"
  value       = module.virtual_machine.instance_ip # Match the GitHub module's output name here
}

If the GitHub module has other outputs you need (like a private IP or instance ID), add additional output blocks the same way:

output "private_instance_ip" {
  type        = string
  description = "Private IP address of the provisioned GCP virtual machine"
  value       = module.virtual_machine.private_instance_ip
}

Step 2: Access the output from your root module

Now that your local vm module exposes the GitHub module's outputs, you can reference them in your root main.tf just like any other module output. For example, if you need to pass this IP to another module:

# Root directory main.tf
module "vm1" {
  source = "../modules/vm" # Note: You don't need to specify /main.tf here—Terraform automatically looks for main.tf in the module directory
  name   = var.vm1name
  size   = var.vm1size
}

# Example: Using the IP in another module
module "monitoring_agent" {
  source = "../modules/monitoring"
  target_vm_ip = module.vm1.instance_ip
}

Quick Fix: Simplify Your Root Module's Source Path

I noticed in your root main.tf you have source = "../modules/vm/main.tf"—you can simplify this to source = "../modules/vm" since Terraform automatically uses the main.tf file in the module directory by default.

How This Works

Terraform modules don't automatically pass through outputs from nested modules—you have to explicitly declare which outputs you want to expose. By creating the outputs.tf in your local vm module, you're telling Terraform: "Take this value from the nested GitHub module and make it available to anyone who uses me."

内容的提问来源于stack exchange,提问作者Richard Wolford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:22:29