【问题标题】:Terraform module - output variable as input for another moduleTerraform 模块 - 输出变量作为另一个模块的输入
【发布时间】:2021-01-11 20:57:21
【问题描述】:

我是 terraform 的新手,正在尝试构建具有两个子网和 VPC 的基础架构。我创建了两个模块

  • VPC
  • 子网

VPC 模块将创建一个 VPC 并将返回 vpc_id 作为输出,这与我尝试在子网模块中使用的相同返回 vpc_id,但是当我运行 terraform 计划时,它要求我输入 vpc_id 输入。

我想要 VPC 模块的输出值中的 vpc_id,任何人都可以帮助我。

下面是代码,

根 tf 文件,

 provider "aws" {
  shared_credentials_file = var.shared_cred
  profile                 = "default" 
  region                  = var.aws_region
}

module "vpc" {
  source = "./vpc"
  name   = "terraformVPC"
  cidr   = "10.50.40.0/27"
}

module "private_subnet" {
  source      = "./subnet"
  subnet_name = "private_subnet"
  subnet_cidr = "10.50.40.16/28"
  #VPC_id = aws_vpc.moduleVPC.id
  VPCid = module.vpc.outvpc_id # this is the issue
}

module "public_subnet" {
  source      = "./subnet"
  subnet_name = "public_subnet"
  subnet_cidr = "10.50.40.0/28"
  VPCid      = module.vpc.outvpc_id
}

子网资源

resource "aws_subnet" "module_subnet" {
  cidr_block = var.subnet_cidr
  vpc_id     = var.VPCid

  tags = {
    Name = var.subnet_name
  }
}

子网模块变量声明

variable "subnet_name" {
  description = " define th subnet name"
}

variable "subnet_cidr" {
  description = "define th subnet cidr block"
}

variable "VPCid" {
  description = "Assign VPC id to subnet"
}

VPC 输出

output "outvpc_id" {
  value = "${aws_vpc.moduleVPC.id}"
}

【问题讨论】:

  • 应该是value = aws_vpc.moduleVPC.id 而不是value = "${aws_vpc.moduleVPC.id}"。我认为您应该有一条错误消息告诉您这一点?
  • 嗨 Liam,我没有收到错误,但要求输入 vpc_id 变量,我认为两者都执行相同的操作 value = aws_vpc.moduleVPC.id 和 value = "${aws_vpc.模块VPC.id}"

标签: amazon-web-services terraform amazon-vpc terraform-modules


【解决方案1】:

这称为“Module Composition”。要记住的重要一点是您引用了另一个模块的输出

格式为:module.<object-name>.<output-name>

module "network" {
  source = "./modules/aws-network"

  base_cidr_block = "10.0.0.0/8"
}

module "consul_cluster" {
  source = "./modules/aws-consul-cluster"

  vpc_id     = module.network.vpc_id       # < output of module.network
  subnet_ids = module.network.subnet_ids   # < output of module.network
}

【讨论】:

    【解决方案2】:

    当我将 terraform 用于 aws 时...我的模块名称是“network.ts”,我认为您不需要两个 tf 文件来管理您的 vpc 和该 VPC 的子网。

    network.tf

    resource "aws_vpc" "vpc" {
      cidr_block           = "10.50.40.0/27"
      enable_dns_hostnames = true // only if you need
      tags                 = {
        Name = "desa-vpc-spotify" //Use your own name
      }
    }
    
    resource "aws_subnet" "private_subnet" {
      vpc_id            = aws_vpc.vpc.id
      availability_zone = "us-east-1a" //your own region
      cidr_block        = "10.50.40.16/28"
      tags = {
        Name = "desa-subnet-private-spotify"
      }
    }
    
    resource "aws_subnet" "public_subnet" {
      vpc_id            = aws_vpc.vpc.id
      availability_zone = "us-east-1a"//your own region
      cidr_block        = "10.50.40.0/28"
      tags = {
        Name = "desa-subnet-public-spotify"
      }
    }
    

    如果你想在另一个 tf 上使用 vpc

    (如果你想拥有两个文件……只需要像这样调用 vpc)

    另一个.tf

    data "aws_vpc" "vpcs" {
      tags = {
        Name = "desa-vpc-spotify" //only put the name of the vpc of the network tf
      }
    }
    
    
    

    【讨论】:

    • 谢谢你,德里克。我只是在测试如何在模块之间调用变量,但看起来我以错误的方式调用它们。
    【解决方案3】:

    我注意到您列出了“VPCid =”。当您运行 terraform validate 时,它​​会引发错误吗?我会尝试将其更改为“vpc_id”,看看是否可行。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 2020-12-08
    • 2017-05-26
    • 2022-07-11
    • 1970-01-01
    • 2013-10-20
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多