amazon-web-services - 将输出变量与 terraform 一起使用时,“不是模块的有效输出”

标签 amazon-web-services terraform infrastructure

我正在尝试在 AWS 上使用 Hashicorp Terraform 为新项目设置一些 IaC。我使用模块是因为我希望能够在多个环境(登台、生产、开发等)中重用东西

我正在努力理解必须在模块中的何处设置输出变量,以及如何在另一个模块中使用它。任何对此的指示将不胜感激!

在创建 EC2 计算机时,我需要使用在 VPC 模块中创建的一些内容(子网 ID)。我的理解是,您无法在另一个模块中引用一个模块中的某些内容,因此我尝试使用 VPC 模块中的输出变量。

我的网站中有以下内容 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 只需设置一些值并使用我的 vpc 模块:

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"
}

在我的 vpc main.tf 中,在 aws_vpcaws_subnet 资源(显示子网资源)之后,我在最后有以下内容:

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}"
}

当我运行terraform plan时,我收到以下错误消息:

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

最佳答案

每次都需要通过每个模块显式地传递输出。

例如,如果您想从嵌套在另一个模块下的模块将变量输出到屏幕,您将需要如下所示的内容:

子模块.tf

output "child_foo" {
  value = "foobar"
}

父模块.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}"
}

关于amazon-web-services - 将输出变量与 terraform 一起使用时,“不是模块的有效输出”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48084463/

相关文章:

python - AWS EMR Spark "No Module named pyspark"

amazon-web-services - 云信息模板错误

amazon-web-services - 如何将 Terraform 状态与我的 AWS 基础设施同步

azure - Azure 上的 Terraform 公共(public) IP 输出

sql-server - 在远程位置之间共享数据

使用 Consul Registrator 时 ServiceAddress 为空

amazon-web-services - 跨多个环境共享 SQS

postgresql - 从 RDS postgres 只读副本和 sqlalchemy 流式传输大型结果集过早终止

chef-infra - Terraform:配置 Chef

performance - 是否有关于 Azure VM 本地驱动器速度的数据?