带有条件的 Terraform 部署模块
Terraform deploy module with a condition
我想要完成什么?
仅当输入变量大于 0
时,我才尝试部署 terraform 模块
我尝试了什么?
var.blocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
结果如何?
Error: Unsupported attribute
on ..\mssql-tf-docs\modules\db\main.tf line 27, in module "diagnostic_mssql_db":
27: "Blocks" = var.blocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
|----------------
| module.diagnostic_mssql_db is object with no attributes
This object does not have an attribute named "target_resource_id".
你的示例代码在哪里?
请注意下面的部分“main.tf”
部分“ds_log_api_endpoints”
main.tf
# SQL Server Database within a SQL Server Server
resource "azurerm_mssql_database" "db" {
name = var.name
server_id = var.server_id
collation = var.collation
license_type = var.license_type
sku_name = var.sku_name
max_size_gb = var.max_size_gb
zone_redundant = var.zone_redundant
read_scale = var.read_scale
read_replica_count = var.read_replica_count
auto_pause_delay_in_minutes = var.sku_name == "GP_S_Gen5_1" ? var.auto_pause_delay_in_minutes : 0
min_capacity = var.sku_name == "GP_S_Gen5_1" ? var.min_capacity : 0
}
# Diagnostic setting
module "diagnostic_mssql_db" {
source = "github.com/faraday23/terraform-azurerm-monitor-diagnostic-setting.git"
storage_account = var.sa_storage_account
sa_resource_group = var.storage_account_resource_group
target_resource_id = azurerm_mssql_database.db.id
target_resource_name = azurerm_mssql_database.db.name
ds_allmetrics_rentention_days = var.metric
ds_log_api_endpoints = { "AutomaticTuning" = var.automatic_tuning > 0 ? module.diagnostic_mssql_db.target_resource_id : 1
"Blocks" = var.blocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"DatabaseWaitStatistics" = var.database_wait_statistics > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"Deadlocks" = var.deadlocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"Errors" = var.error_log > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"Timeouts" = var.timeouts > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"QueryStoreRuntimeStatistics" = var.query_store_runtime_statistics > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"QueryStoreWaitStatistics" = var.query_store_wait_statistics > 0? module.diagnostic_mssql_db.target_resource_id : 1,
"SQLinsights" = var.sql_insights > 0 ? module.diagnostic_mssql_db.target_resource_id : 1
}
}
variables.tf
#######
# Name
#######
variable "name" {
description = "The name of the Ms SQL Database. Changing this forces a new resource to be created."
type = string
}
############
# Server Id
############
variable "server_id" {
description = "The id of the Ms SQL Server on which to create the database. Changing this forces a new resource to be created."
type = string
default = ""
}
#######
# Flags
#######
# Audit Log Enabled
variable "audit_log_enabled" {
description = "The audit log enabled of the resource."
type = bool
default = false
}
variable "log_retention_days" {
description = "Specifies the number of days to keep in the Threat Detection audit logs"
type = number
default = 7
}
variable "collation" {
description = "Specifies the collation of the database. Changing this forces a new resource to be created."
type = string
default = "SQL_Latin1_General_CP1_CI_AS"
}
variable "license_type" {
description = "Specifies the license type applied to this database. Possible values are LicenseIncluded and BasePrice."
type = string
default = "LicenseIncluded"
}
variable "sku_name" {
description = "Specifies the name of the sku used by the database. Changing this forces a new resource to be created. For example, GP_S_Gen5_2,HS_Gen4_1,BC_Gen5_2, ElasticPool, Basic,S0, P2 ,DW100c, DS100."
type = string
default = "GP_S_Gen5_1"
validation {
condition = can(regex("GP_S_Gen5_1|GP_S_Gen5_2|HS_Gen4_1|BC_Gen5_2|ElasticPool|Basic|S0|P2|DW100c|DS100", var.sku_name))
error_message = "Sku name invalid. Must be GP_S_Gen5_1,GP_S_Gen5_2,HS_Gen4_1,BC_Gen5_2, ElasticPool, Basic,S0, P2 ,DW100c, DS100."
}
}
variable "min_capacity" {
type = number
default = 0.5
}
variable "auto_pause_delay_in_minutes" {
type = number
default = -1
}
variable "max_size_gb" {
type = number
default = 4
}
variable "zone_redundant" {
type = bool
default = false
}
variable "read_scale" {
type = bool
default = false
}
variable "read_replica_count" {
type = number
default = 0
}
##
# Diagnostic setting variable
##
variable "sa_storage_account" {
description = "This blob storage will hold all Threat Detection audit logs. Required if state is Enabled."
type = string
default = ""
}
variable "storage_account_resource_group" {
description = "Azure resource group where the storage account resides."
type = string
}
variable "automatic_tuning" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "database_wait_statistics" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "query_store_runtime_statistics" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "query_store_wait_statistics" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "error_log" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "sql_insights" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "deadlocks" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "timeouts" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "metric" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "blocks" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
这个问题看起来有点糊涂,但是如果换个角度思考,那就很容易解决了。
您似乎想在模块中创建一个 MySQL 数据库和其他资源。但其他资源取决于条件。想一想,Terraform 中的所有资源都有一个计数属性,如果计数等于0。这意味着你只需要为所有条件资源设置计数变量。在模块内部,直接使用变量target_resource_id
。
Terraform 0.13 引入了对 for_each
和 count
模块的支持。
有关更多详细信息和示例,请参阅 https://www.terraform.io/docs/configuration/modules.html#multiple-instances-of-a-module。
例如:
module "diagnostic_mssql_db" {
count = var.blocks > 0 ? 1 : 0
. . .
}
根据 var.blocks
的值,这将为您提供 0 或 1 个 module.diagnostic_mssql_db
资源。
我想要完成什么?
仅当输入变量大于 0
我尝试了什么?
var.blocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
结果如何?
Error: Unsupported attribute
on ..\mssql-tf-docs\modules\db\main.tf line 27, in module "diagnostic_mssql_db":
27: "Blocks" = var.blocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
|----------------
| module.diagnostic_mssql_db is object with no attributes
This object does not have an attribute named "target_resource_id".
你的示例代码在哪里?
请注意下面的部分“main.tf”
部分“ds_log_api_endpoints”
main.tf
# SQL Server Database within a SQL Server Server
resource "azurerm_mssql_database" "db" {
name = var.name
server_id = var.server_id
collation = var.collation
license_type = var.license_type
sku_name = var.sku_name
max_size_gb = var.max_size_gb
zone_redundant = var.zone_redundant
read_scale = var.read_scale
read_replica_count = var.read_replica_count
auto_pause_delay_in_minutes = var.sku_name == "GP_S_Gen5_1" ? var.auto_pause_delay_in_minutes : 0
min_capacity = var.sku_name == "GP_S_Gen5_1" ? var.min_capacity : 0
}
# Diagnostic setting
module "diagnostic_mssql_db" {
source = "github.com/faraday23/terraform-azurerm-monitor-diagnostic-setting.git"
storage_account = var.sa_storage_account
sa_resource_group = var.storage_account_resource_group
target_resource_id = azurerm_mssql_database.db.id
target_resource_name = azurerm_mssql_database.db.name
ds_allmetrics_rentention_days = var.metric
ds_log_api_endpoints = { "AutomaticTuning" = var.automatic_tuning > 0 ? module.diagnostic_mssql_db.target_resource_id : 1
"Blocks" = var.blocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"DatabaseWaitStatistics" = var.database_wait_statistics > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"Deadlocks" = var.deadlocks > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"Errors" = var.error_log > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"Timeouts" = var.timeouts > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"QueryStoreRuntimeStatistics" = var.query_store_runtime_statistics > 0 ? module.diagnostic_mssql_db.target_resource_id : 1,
"QueryStoreWaitStatistics" = var.query_store_wait_statistics > 0? module.diagnostic_mssql_db.target_resource_id : 1,
"SQLinsights" = var.sql_insights > 0 ? module.diagnostic_mssql_db.target_resource_id : 1
}
}
variables.tf
#######
# Name
#######
variable "name" {
description = "The name of the Ms SQL Database. Changing this forces a new resource to be created."
type = string
}
############
# Server Id
############
variable "server_id" {
description = "The id of the Ms SQL Server on which to create the database. Changing this forces a new resource to be created."
type = string
default = ""
}
#######
# Flags
#######
# Audit Log Enabled
variable "audit_log_enabled" {
description = "The audit log enabled of the resource."
type = bool
default = false
}
variable "log_retention_days" {
description = "Specifies the number of days to keep in the Threat Detection audit logs"
type = number
default = 7
}
variable "collation" {
description = "Specifies the collation of the database. Changing this forces a new resource to be created."
type = string
default = "SQL_Latin1_General_CP1_CI_AS"
}
variable "license_type" {
description = "Specifies the license type applied to this database. Possible values are LicenseIncluded and BasePrice."
type = string
default = "LicenseIncluded"
}
variable "sku_name" {
description = "Specifies the name of the sku used by the database. Changing this forces a new resource to be created. For example, GP_S_Gen5_2,HS_Gen4_1,BC_Gen5_2, ElasticPool, Basic,S0, P2 ,DW100c, DS100."
type = string
default = "GP_S_Gen5_1"
validation {
condition = can(regex("GP_S_Gen5_1|GP_S_Gen5_2|HS_Gen4_1|BC_Gen5_2|ElasticPool|Basic|S0|P2|DW100c|DS100", var.sku_name))
error_message = "Sku name invalid. Must be GP_S_Gen5_1,GP_S_Gen5_2,HS_Gen4_1,BC_Gen5_2, ElasticPool, Basic,S0, P2 ,DW100c, DS100."
}
}
variable "min_capacity" {
type = number
default = 0.5
}
variable "auto_pause_delay_in_minutes" {
type = number
default = -1
}
variable "max_size_gb" {
type = number
default = 4
}
variable "zone_redundant" {
type = bool
default = false
}
variable "read_scale" {
type = bool
default = false
}
variable "read_replica_count" {
type = number
default = 0
}
##
# Diagnostic setting variable
##
variable "sa_storage_account" {
description = "This blob storage will hold all Threat Detection audit logs. Required if state is Enabled."
type = string
default = ""
}
variable "storage_account_resource_group" {
description = "Azure resource group where the storage account resides."
type = string
}
variable "automatic_tuning" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "database_wait_statistics" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "query_store_runtime_statistics" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "query_store_wait_statistics" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "error_log" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "sql_insights" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "deadlocks" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "timeouts" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "metric" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
variable "blocks" {
description = "Retention only applies to storage account. Retention policy ranges from 1 to 365 days. If you do not want to apply any retention policy and retain data forever, set retention (days) to 0."
type = string
}
这个问题看起来有点糊涂,但是如果换个角度思考,那就很容易解决了。
您似乎想在模块中创建一个 MySQL 数据库和其他资源。但其他资源取决于条件。想一想,Terraform 中的所有资源都有一个计数属性,如果计数等于0。这意味着你只需要为所有条件资源设置计数变量。在模块内部,直接使用变量target_resource_id
。
Terraform 0.13 引入了对 for_each
和 count
模块的支持。
有关更多详细信息和示例,请参阅 https://www.terraform.io/docs/configuration/modules.html#multiple-instances-of-a-module。
例如:
module "diagnostic_mssql_db" {
count = var.blocks > 0 ? 1 : 0
. . .
}
根据 var.blocks
的值,这将为您提供 0 或 1 个 module.diagnostic_mssql_db
资源。