terraform - 使用模块时无法创建 Terraform 资源组
问题描述
我正在使用模块优化我的 terraform 代码。当我创建一个资源组模块时,它工作得很好,但它创建了两个资源组,即
- Temp-AppConfiguration-ResGrp
- Temp-AppServices-ResGrp
相反,它应该只创建
Temp-AppConfiguration-ResGrp
代码资源组.tf 。
resource "azurerm_resource_group" "resource" {
name = "${var.environment}-${var.name_apptype}-ResGrp"
location = var.location
tags = {
environment = var.environment
}
}
output "resource_group_name" {
value = "${var.environment}-${var.name_apptype}-ResGrp"
}
output "resource_group_location" {
value = var.location
}
变量.tf
variable "name_apptype" {
type = string
default = "AppServices"
}
variable "environment" {
type = string
default = "Temp"
}
variable "location" {
type = string
default = "eastus"
}
主文件
module "resourcegroup" {
source = "../Modules"
name_apptype = "AppConfiguration"
}
我想在调用资源组模块时在 main.tf中传递name_apptype 。这样我就不需要每次都更新 variable.tf 了。
任何我做错的建议。另外我也无法输出该值,我需要它以便我可以在我要创建的下一个模块中传递资源组名称。
谢谢
解决方案
你需要在Main.tf
module "resourcegroup" {
source = "../Modules"
name_apptype = "AppConfiguration"
}
module "resourcegroup-appservices" {
source = "../Modules"
name_apptype = "AppServices"
}
这些使用您需要的值创建了 2 个资源组,此外,您可以从name_apptype
变量中删除默认值。
如果要使用相同的模块创建两个资源组,则需要使用 count 来遍历名称数组
推荐阅读
- python - 尝试在 db TypeError 中添加条目时出现问题:__init__() 错误
- java - 如何使用 docker 与 java 应用程序一起运行 shell 脚本?
- html - 为什么除了高度和宽度之外的任何 CSS 都不能在我的 div 上工作?
- r - 有效地改变许多索引范围的向量值
- model - Promela, Ispin:带旋转的模型检查器 promela 建模
- python-3.x - cumsum() 按月计算,但如果该月没有数据,则重复这些值
- php - 如何在数组中使用 chr() 函数从 PHP 中的危险字符中清除字符串?
- windows - 如何在 PowerShell 中合并控制台输出并作为 HTML 格式的电子邮件发送
- python - 为什么 parseargs 将参数存储在列表中
- android - 在调用 onPause() 后,如何停止在后台 android 中循环的异步任务?