Terraform: data reference from subdirectory

Created on 15 Dec 2017  ยท  3Comments  ยท  Source: hashicorp/terraform

/base/data.tf

data "aws_ami" "ecs" {
  filter {
    name   = "state"
    values = ["available"]
  }
  filter {
    name   = "name"
    values = ["amzn-ami-*-amazon-ecs-optimized"]
  }  most_recent = true
}

/base/test.tf

provider "aws" {
  region = "us-east-1"
}
output "ami" {
  value = "${data.aws_ami.ecs.id}"
}

The above works when both files are in same directory. But how can I refer to ami id from /base/data.tf when test.tf is in a sub directory of base say /base/new/test.tf

question

Most helpful comment

Hi @sivarammedapati

Thanks for opening this issue.

In general we'd recommend asking questions about Terraform Configurations on one of the community sources listed here (this issue tracker is more intended for bugs with Terraform).

That said - this can be achieved using Modules - where the "new directory" in your example exposes Inputs and Outputs as needed for the configuration it contains; and this is then called from the Terraform Configuration in the root. There's more information on this page - however you should be able to reference the module in the sub-directory like so:

module "foo" {
  source = "./new"
  # assign any variables explicitly defined in the module such as
  foo = "bar"
}

output "bar" {
  value = "${module.foo.some_output}"
}

The variable foo needs to be explicitly defined in the child module; as does the output "some_output" - as shown in this example below:

variable "foo" {}

output "some_output" {
  value = "hello"
}

# .. rest of the configuration

Since this isn't a bug in Terraform I'm going to close this issue for the moment - however I believe the examples above + the documentation should be what you're after :)

Thanks!

All 3 comments

Hi @sivarammedapati

Thanks for opening this issue.

In general we'd recommend asking questions about Terraform Configurations on one of the community sources listed here (this issue tracker is more intended for bugs with Terraform).

That said - this can be achieved using Modules - where the "new directory" in your example exposes Inputs and Outputs as needed for the configuration it contains; and this is then called from the Terraform Configuration in the root. There's more information on this page - however you should be able to reference the module in the sub-directory like so:

module "foo" {
  source = "./new"
  # assign any variables explicitly defined in the module such as
  foo = "bar"
}

output "bar" {
  value = "${module.foo.some_output}"
}

The variable foo needs to be explicitly defined in the child module; as does the output "some_output" - as shown in this example below:

variable "foo" {}

output "some_output" {
  value = "hello"
}

# .. rest of the configuration

Since this isn't a bug in Terraform I'm going to close this issue for the moment - however I believe the examples above + the documentation should be what you're after :)

Thanks!

I recently had this problem and this article helped me: https://www.oreilly.com/library/view/terraform-up-and/9781491977071/ch04.html

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pawelsawicz picture pawelsawicz  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

sprokopiak picture sprokopiak  ยท  3Comments

zeninfinity picture zeninfinity  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments