'Unable to pass output values from a Terraform module which uses a git source back to the caller
I have a Terraform module which is used to build a GCP virtual machine. The module is located at ../modules/vm/main.tf. The problem I have is that inside of main, I also have a source setting which points to a github repo. So when I call my ../modules/vm/main.tf module from my root main.tf module, I'm not able to capture to returns from the git source and pass them all the way back to my root main.tf.
So basically, ../modules/vm/main.tf's git source returns output, that output needs to be returned to ./main.tf. I'm failing epically at this point and need some assistance.
Here is the main.tf in the root:
module "vm1" {
source = ../modules/vm/main.tf
vm.name = var.vm1name
vm.size = var.vm1.size
}
Here is what is in the ../modules/vm/variables.tf file:
variable "name" {
type = "string"
description = "The name of the VM."
}
variable "size" {
type = "string"
description = "The instance size."
}
Here is what is in the main.tf file in ../modules/vm/main.tf
module "virtual_machine" {
source = "git::https://github.myorg.com/repository?ref=v1.0"
name = var.name
size = var.size
}
One of the output values of "git::https://github.myorg.com/repository?ref=v1.0" is the IP address of the virtual machine. It doesn't seem to be an issue with getting the output, but I need to send this output back to ./main.tf from ../modules/vm/main.tf because the next modules used in my root main.tf need the IP address assigned to the VM. That's where I'm having the problem, I don't know exactly how to get that returned IP address into my outputs.tf file to return to my root main.tf.
I hope that makes sense.
Solution 1:[1]
You should create an outputs.tf file in your module
Path: ../modules/vm/outputs.tf
Assuming that the exported variable for the ip address of the https://github.myorg.com/repository?ref=v1.0 module is named ip_address
output "ip_addres" {
description = "Ip address of VM"
value = module.virtual_machine.ip_address
}
Then in your main.tf you can refer to the ip address
module.vm1.ip_address
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tolis Gerodimos |
