'Not a valid output for module' when using output variable with terraform

对着背影说爱祢 提交于 2019-11-28 05:28:55

问题


I'm trying to setup some IaC for a new project using Hashicorp Terraform on AWS. I'm using modules because I want to be able to reuse stuff across multiple environments (staging, prod, dev, etc.)

I'm struggling to understand where I have to set an output variable within a module, and how I then use that in another module. Any pointers to this would be greatly appreciated!

I need to use some things created in my VPC module (subnet IDs) when creating EC2 machines. My understanding is that you can't reference something from one module in another, so I am trying to use an output variable from the VPC module.

I have the following in my site main.tf

module "myapp-vpc" {
  source     = "dev/vpc"
  aws_region = "${var.aws_region}"
}

module "myapp-ec2" {
 source     = "dev/ec2"
 aws_region = "${var.aws_region}"
 subnet_id  = "${module.vpc.subnetid"}
}

dev/vpc simply sets some values and uses my vpc module:

module "vpc" {
  source = "../../modules/vpc"

  aws_region = "${var.aws_region}"

  vpc-cidr            = "10.1.0.0/16"
  public-subnet-cidr  = "10.1.1.0/24"
  private-subnet-cidr = "10.1.2.0/24"
}

In my vpc main.tf, I have the following at the very end, after the aws_vpc and aws_subnet resources (showing subnet resource):

resource "aws_subnet" "public" {
  vpc_id                  = "${aws_vpc.main.id}"
  map_public_ip_on_launch = true
  availability_zone       = "${var.aws_region}a"
  cidr_block              = "${var.public-subnet-cidr}"
}

output "subnetid {
  value = "${aws_subnet.public.id}"
}

When I run terraform plan I get the following error message:

Error: module 'vpc': "subnetid" is not a valid output for module "vpc"


回答1:


Outputs need to be passed up through each module explicitly each time.

For example if you wanted to output a variable to the screen from a module nested below another module you would need something like this:

child-module.tf

output "child_foo" {
  value = "foobar"
}

parent-module.tf

module "child" {
  source = "path/to/child"
}

output "parent_foo" {
  value = "${module.child.child_foo}"
}

main.tf

module "parent" {
  source = "path/to/parent"
}

output "main_foo" {
  value = "${module.parent.parent_foo}"
}


来源:https://stackoverflow.com/questions/48084463/not-a-valid-output-for-module-when-using-output-variable-with-terraform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!