【问题标题】:terraform v0.12.12 passing data between modules - How Is This Supposed To Work?terraform v0.12.12 在模块之间传递数据 - 这应该如何工作?
【发布时间】:2021-03-19 03:08:13
【问题描述】:

我很难理解 Terraform (v0.12.12) 中数据传入和传出模块的方式。我有一个我认为非常简单的例子,但无法理解数据应该如何在模块之间传递。我能找到的大多数例子要么不完整,要么已经过时。

我创建了一个包含两个模块的简单示例。一个创建 vpc 和子网的网络模块,以及一个创建 EC2 实例的计算模块。我只是想为计算模块提供 EC2 实例应该去的子网的 ID。但我不明白:

  1. 如何从创建的网络模块中获取子网 ID 子网给其他模块能用吗?
  2. 如何让计算模块使用子网ID?

基本结构如下

. ├── main.tf └── modules ├── compute │ └── main.tf └── network ├── main.tf └── output.tf

# main.tf
provider "aws" {
    region     = "eu-west-1"
}

module "m_network" {
    source      = "./modules/network"
}

# The problem is how to make that subnet id available to the compute module
# so the ec2 instance can be added to it? 
module "m_compute" {
    source     = "./modules/compute"
    # I wondered if the m_compute module should pass in a parameter, but 
    # Any attempt to pass a parameter gives an error: An argument "subnet_id" is not expected here.
    #xxx = "xxx" # This fails to.
    # subnet_id = module.m_network.subnet_id
}

resource "aws_vpc" "myvpc" {
  cidr_block = "10.0.0.0/16"
}

# Create subnets in each availability zone to launch our instances into, each with address blocks within the VPC:
resource "aws_subnet" "myvpc_subnet" {
  vpc_id                  = "${aws_vpc.myvpc.id}"
  cidr_block              = "10.0.1.0/24"
}

# Generates subnet attributes that can be passed to other modules
output "myvpc_subnet_id" {
    description = "Subnet ID"
    value = "${aws_subnet.myvpc_subnet.id}"
}

resource "aws_instance" "app" {
    ami           = "ami-13be557e"
    instance_type = "t2.micro"
    subnet_id     = aws_subnet.myvpc_subnet_id # What should go here?
}

【问题讨论】:

  • 您的./modules/compute 模块中有variable "subnet_id" 块吗? (有关此类块内容的更多详细信息,请参阅Declaring an Input Variable。)

标签: module output terraform


【解决方案1】:

您需要将variables.tf 文件添加到您的compute 模块,以便它可以接收来自network 模块的subnet_id

检查variables.tf文件内容和compute模块的main.tf 看看如何访问输入变量。

一个例子的结构应该如下。

.
├── main.tf
└── modules
    ├── compute
    │   ├── main.tf
    │   └── variables.tf
    └── network
        ├── main.tf
        └── output.tf

然后在每个文件中你可以做这样的事情。

# main.tf

provider "aws" {
    region     = "eu-west-1"
}

# Call network module and receive output
module "m_network" {
    source      = "./modules/network"
}

module "m_compute" {
    source     = "./modules/compute"
    # pass the output of the network module
    # as input variables for the compute module
    subnet_id  = module.m_network.output_subnet_id
}
# compute module | variables.tf

# declare a input variable for the compute module
variable "subnet_id" {
  description = "The subnet ID from the network module"

  # You can also enforce the type with
  # type = string OR number OR etc.
}

# compute module | main.tf

resource "aws_instance" "app" {
    ami           = "ami-13be557e"
    instance_type = "t2.micro"
    # you can use variables with var.{name}
    # access the subnet id variable
    subnet_id     = var.subnet_id
}
# network module | main.tf

resource "aws_vpc" "myvpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "myvpc_subnet" {
  vpc_id                  = "${aws_vpc.myvpc.id}"
  cidr_block              = "10.0.1.0/24"
}
# network module | output.tf

output "output_subnet_id" {
    description = "Subnet ID"
    value = "${aws_subnet.myvpc_subnet.id}"
}

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多