Adam Matan
Adam Matan

Reputation: 136191

Output a field from a module

Code

Consider a terraform module:

module "blah-asg" {
  source = "asg"

  asg_max_size       = 1
  asg_min_size       = "${var.min_blah}"
  ...
}

My problem

How do I output variables from it?

What have I tried

output "blah-es-asg" {
    value = "${asg.blah-asg.arn}"
}

Which failed with

Error getting plugins: module root: 1 error(s) occurred: * output 'blah-asg': unknown resource 'asg.blah' referenced in variable asg.blah-asg.arn

My question

How can I output module fields in Terraform?

Upvotes: 41

Views: 70095

Answers (5)

Romojo
Romojo

Reputation: 91

I found a different answer, which I'll add here as (I hope) it's clearer.

My Setup: three folders: dev, tst, uat and 'modules\fabric'. The Terraform plan is preceded by a 'cd ' to the right folder (also the environment, of course).

in the modules\fabric folder, I have two files. The first is 'main.tf' with content:

output "write_module_environment" {
    value = "${var.environment}"
}

Second is 'variables.tf' with a definition of 'environment' as a string.

The 'main.tf' file in each folder (dev, tst, uat) has this part ..

    module "fabric" {
      source = "../modules/fabric"
    
      environment           = "dev"
}

(environment varies...) and then calls that output command in the module this way:

output module_passed_in_name {
  value = "${module.fabric.write_module_environment}"
}

(Made up as module.'name of module'.'output command in module main.tf')

Then among my plan displays when I run it, I get ..

 + module_passed_in_name = "dev"

Trick is that the main.tf 'output' outputs a qualified 'output' command from the module, rather than a variable.

My respects to BMW and slm above. Without their post above, I wouldn't have got there.

This is Terraform 1.8.5.

Upvotes: 0

Heather Herbert
Heather Herbert

Reputation: 31

Say your module looks like this in your main.tf file:

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"

Your output can be accessed this way in outputs.tf file

output "vpc_id" {
value = module.vpc.vpc_id
}

Upvotes: 3

lars1595
lars1595

Reputation: 925

Use module.[module-name].[output-variable-name]

e.g. in Azure for create a resource_group with the name og the module output

resource "azurerm_resource_group" "my-test-rg" {
     name =  module.blah-asg.blah-es-asg  
}

Upvotes: 3

BMW
BMW

Reputation: 45243

So first, you need to set the output in the module asg:

$ cat asg/output.tf

output "blah-es-asg" {
    value = "${aws_autoscaling_group.blah-asg.arn}"
}

Then you call the module with source = "asg":

module "blah-asg" {
  source = "asg"

  asg_max_size       = 1
  asg_min_size       = "${var.min_blah}"
  ...
}

You can output it in current code with this format now:

output "blah-es-asg" {
    value = "${module.blah-asg.blah-es-asg}"
}

Upvotes: 87

James Thorpe
James Thorpe

Reputation: 32202

The module itself knows nothing of the name blah-asg - that's just in the script that's calling it - indeed it could be called multiple times with different names and parameters.

The output should just reference things within the module the same way you would elsewhere in that same module. For instance, if you wanted to output the arn of the following resource:

resource "aws_lb" "test" {
  # ...
}

You would use:

output "blah-es-asg" {
    value = "${aws_lb.test.arn}"
}

Note that the output is defined along side the rest of the module code, not in the script that's calling it.

This output can then by used by the script calling the module as ${module.blah-asg.blah-es-asg}

Upvotes: 7

Related Questions