Terraform,资源可以作为变量传递到模块中吗? [英] Terraform, can a resource be passed as a variable into a module?
问题描述
我刚刚开始了解 Terraform(如果这是一个愚蠢的问题,请见谅).
I'm just getting to grips with Terraform (so apologies if this is a stupid question).
我正在设置一个带有一组子网的 azure vnet,每个子网都有一个路由表,通过防火墙发送流量.
I'm setting up an azure vnet with a set of subnets, each subnet has a routing table that sends traffic via a firewall.
看起来子网和路由表的组合将是一个很好的可重用模块.
It looks like the subnet and route table combination would make a good re-useable module.
按照惯例,我想在与父 vnet 相同的资源组和位置中创建子网和路由表.
By convention, I'd like to create the subnet and the route table in the same resource group and location as the parent vnet.
如果我将模块中所需的所有值作为单独的值提供,则模块可以正常工作:)
If I provide all the values needed in the module as individual values, the module works ok :)
我宁愿做的是将代表父 vnet 的资源作为参数"有效地传递到模块中,并让模块直接从 vnet 资源中读取资源组名称、位置和 vnet 名称等内容,所以那:- 我输入更少(我只使用 vnet 创建模块实例,而不是 vnet 名称、资源组名称和位置的单独值)- 它消除了在路由表和子网上设置位置和资源组名称时出错的机会(如果我从模块中的父 vnet 读取值,那么值将与父 vnet 相同)
What I'd rather do is effectively pass the resource that represents the parent vnet into the module as a "parameter" and have the module read things like the resource group name, location and vnet name directly from the vnet resource, so that: - I type less (I create the module instance with just the vnet, rather than seperate values for vnet name, resource group name and location) - it removes the opportunity for error when setting the location and resource group names on the route table and subnet (if I read the values from the parent vnet in the module then the values will be the same as the parent vnet)
目前模块变量定义为:
variable "parent-vnet-name" {}
variable "parent-vnet-resource-group-name" {}
variable "parent-vnet-location" {}
variable "subnet-name" {
type = "string"
}
variable "subnet-address-prefix" {}
variable "firewall-ip-private" {}
模块为:
resource "azurerm_route_table" "rg-mgt-network__testtesttest" {
name = "${var.subnet-name}"
location = "${var.parent-vnet-location}"
resource_group_name = "${var.parent-vnet-resource-group-name}"
route {
name = "Default"
address_prefix = "0.0.0.0/0"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "${var.firewall-ip-private}"
}
}
其实我想做的更像是变量:
Really what I'd like to do is more like variables:
variable "parent-vnet" {}
variable "subnet-name" {
type = "string"
}
variable "subnet-address-prefix" {}
variable "firewall-ip-private" {}
与模块做类似的事情:
resource "azurerm_route_table" "rg-mgt-network__testtesttest" {
name = "${var.subnet-name}"
location = "${var.parent-vnet.location}"
resource_group_name = "${var.parent-vnet.resource-group-name}"
route {
name = "Default"
address_prefix = "0.0.0.0/0"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "${var.firewall-ip-private}"
}
}
我玩过各种各样的事情(例如尝试通过 vnet 而不指定属性名称(验证失败)或使用数据源拉回 vnet(数据源没有资源group info) ) 但什么地方都没有,所以我想知道我是否遗漏了什么?
I've played around with a variety of things (such as trying to pass the vnet without specifying an attribute name (validation failure) or using a data source to pull back the vnet (the data sources doesn't have the resource group info) ) but haven't got anywhere so I'm wondering if I've missed something?
干杯,安迪
推荐答案
目前不可能 - 根据 Terraform 创建模块页面,模块的输入变量只能是标准变量类型:string
、list
、map
.
This isn't currently possible - according to the Terraform Creating Modules page, a module's input variables can only be standard variable types: string
, list
, and map
.
我遇到过同样的用例,不得不为模块提供大量输入列表,这并不理想,但似乎是世界的当前状态.
I've encountered this same use case and have had to provide large lists of inputs to modules, which isn't ideal but seems to be the current state of the world.
这篇关于Terraform,资源可以作为变量传递到模块中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!