Terraform 模块 - 输出变量作为另一个模块的输入 [英] Terraform module - output variable as input for another module
问题描述
我是 terraform 的新手,正在尝试构建具有两个子网和 VPC 的基础架构.我创建了两个模块
I am new to terraform and trying to build an infrastructure with two subnets and VPC. I have created two modules
- VPC
- 子网
VPC 模块将创建一个 VPC 并将返回 vpc_id 作为输出,与我尝试在子网模块中使用的相同返回 vpc_id,但是当我运行 terraform 计划时,它会要求我输入 vpc_id 输入.
The VPC module will create a VPC and will return vpc_id as output, the same return vpc_id I am trying to use in the subnet module, but when I run the terraform plan, it asks me for the enter vpc_id input.
我想要 VPC 模块的输出值中的 vpc_id,任何人都可以帮助我.
I want the vpc_id from the output value of the VPC module, can anyone please help me on the same.
下面是代码,
根 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}"
}
推荐答案
这叫做 "模块组成".要记住的重要一点是您引用了另一个模块的输出.
This is called "Module Composition". The important thing to remember is that you reference outputs of another module.
格式为:module.
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
}
这篇关于Terraform 模块 - 输出变量作为另一个模块的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!