Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use terraform output as input variable of another terraform template

Tags:

terraform

Is there any way I can use a Terraform template output to another Terraform template's input?

Ex: I have a Terraform template which creates an ELB and I have another Terraform template which is going to create an auto scale group which need the ELB information as an input variable.

I know I can use shell script to grep and feed in the ELB information, but I'm looking for some Terraform way to doing this.

like image 741
Joy Avatar asked Jan 11 '17 16:01

Joy


People also ask

How do you use Terraform output variables as an input?

Using a variableA variable's value can be accessed from within the terraform module block by using var. <variable_name> . Below we have an example demonstrating this. The variable's value can only be accessed in an expression within the modules where it was declared.

How do you pass output value in Terraform?

Terraform Output Command To get the raw value without quotes, use the -raw flag. To get the JSON-formatted output, we can use the -json flag. This is quite useful when we want to pass the outputs to other tools for automation since JSON is way easier to handle programmatically.

What is output variable in Terraform?

Terraform output variables allow you to display a resource's (e.g., AWS resource) output on the console. Output variables are also used as an input parameter for other resources if declared in an output configuration file.


1 Answers

Have you tried using remote state to populate your second template?

Declare it like this:

resource "terraform_remote_state" "your_state" {
  backend = "s3"
  config {
    bucket = "${var.your_bucket}"
    region = "${var.your_region}"
    key = "${var.your_state_file}"
  }
}

And then you should be able to pull out your resource directly like this:

your_elb = "${terraform_remote_state.your_state.output.your_output_resource}"

If this doesn't work for you, have you tried implementing your ELB in a module and then just using the output?

https://github.com/terraform-community-modules/tf_aws_elb is a good example of how to structure the module.

like image 82
mcheshier Avatar answered Sep 23 '22 00:09

mcheshier