ehime
ehime

Reputation: 8375

Unable to get outputs from Terraform v0.12+

I'm migrating to the newest version of Terraform and am not able to reproduce what the documentation outlines as how to get outputs from an apply. The documentation states

data.terraform_remote_state.vpc.vpc_id

So getting these out of a module should be something to the effect of

data.module_name.remote_state.vpc.vpc_id I would think?

here's my module call

module "vpc" {
  source             = "github.com/terraform-aws-modules/terraform-aws-vpc"
  name               = "apigee"
  cidr               = "10.0.0.0/16"
  azs                = [data.aws_availability_zones.available.names[0], data.aws_availability_zones.available.names[1]]
  private_subnets    = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets     = ["10.0.101.0/24", "10.0.102.0/24"]
  enable_nat_gateway = true
  single_nat_gateway = true

  tags = {
    Owner       = "212743998"
    Environment = "sandbox"
  }
}

so I would assume.... that I should do something to the effect of

data.vpc.terraform.vpc.vpc_id

here's the outline of my directory, listing the states file

○ → tree
.
├── [gabel   45]  data.tf
├── [gabel  529]  modules.tf
├── [gabel  112]  outputs.tf
├── [gabel   41]  providers.tf
├── [gabel  36K]  terraform.tfstate
├── [gabel  157]  terraform.tfstate.backup
├── [gabel   21]  terraform.tfvars
└── [gabel  121]  variables.tf

But... I also notice the way they're calling the id in the module and now... well now I'm just confused....

What is the CORRECT WAY to call a modules output in Terraform v0.12+ ?

Upvotes: 0

Views: 520

Answers (2)

user1847067
user1847067

Reputation:

I am not sure I get your question right because modules and remote_state files are different things.

When you use modules the 'parent' or 'caller' or whatever you want to call it will get the values from module.modulename., as you put it and in your case you can get the vpc_id directly from module.vpc.vpc_id.

When you want to get values for variables from a "remote data source" you need to "publish" the data first using "outputs" and then you have the data available on the remote_state file, and for this case TF changed the format of the output on 0.12.x In the new version you need to refer to the outputs as data.terraform_remote_state.vpc.outputs.vpc_id

I believe the documentation does not reflect the change.

Upvotes: 0

ehime
ehime

Reputation: 8375

Yeah, well this works... but still doesn't answer how I would call things that do not have explicit outputs from the modules source...

output "vpc_id" {
  description = "The ID of the VPC"
  value       = module.vpc.vpc_id
}

Output

terraform refresh
data.aws_availability_zones.available: Refreshing state...
module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-xxxx
module.vpc.aws_eip.nat[0]: Refreshing state... [id=eipalloc-xxxx
module.vpc.aws_internet_gateway.this[0]: Refreshing state... [id=igw-xxxx
module.vpc.aws_route_table.public[0]: Refreshing state... [id=rtb-xxxx
module.vpc.aws_subnet.public[0]: Refreshing state... [id=subnet-xxxx
module.vpc.aws_subnet.private[0]: Refreshing state... [id=subnet-xxxx
module.vpc.aws_subnet.private[1]: Refreshing state... [id=subnet-xxxx
module.vpc.aws_subnet.public[1]: Refreshing state... [id=subnet-xxxx
module.vpc.aws_route_table.private[0]: Refreshing state... [id=rtb-xxxx
module.vpc.aws_route.public_internet_gateway[0]: Refreshing state... [id=r-xxxx
module.vpc.aws_route_table_association.private[0]: Refreshing state... [id=rtbassoc-xxxx
module.vpc.aws_route_table_association.private[1]: Refreshing state... [id=rtbassoc-xxxx
module.vpc.aws_route_table_association.public[1]: Refreshing state... [id=rtbassoc-xxxx
module.vpc.aws_route_table_association.public[0]: Refreshing state... [id=rtbassoc-xxxx
module.vpc.aws_nat_gateway.this[0]: Refreshing state... [id=nat-xxxx
module.vpc.aws_route.private_nat_gateway[0]: Refreshing state... [id=r-xxxx

Outputs:

vpc_id = vpc-xxxx

Leaving this answer here for future searchers.

Upvotes: 1

Related Questions