使用因变量时terraform代码报错

问题描述 投票:0回答:0

terraform-azurerm-servciebus 命名空间代码:

main.tf

resource "azurerm_servicebus_namespace" "servicebus_namespace" {
  name                          = regex("^[-\\w\\._\\(\\)]+$", substr("sbns-${var.environment_name}-${var.application_name}-${var.location}", 0, 50))
  resource_group_name           = var.resource_group_name
  location                      = var.location
  sku                           = var.sku
  capacity                      = var.capacity
  tags                          = local.tags
  public_network_access_enabled = false
  minimum_tls_version           = "1.2"
  local_auth_enabled            = false
  identity                      = var.identity
  customer_managed_key {
    key_vault_key_id = var.key_vault_key_id
    identity_id      = var.identity_id
  }
}

outputs.tf

output "id" {
  value = azurerm_servicebus_namespace.servicebus_namespace.id
}
output "endpoint" {
  value = azurerm_servicebus_namespace.servicebus_namespace.endpoint
}

变量.tf

variable "location" {
  type        = string
  description = "Location of the resource group"
}

variable "environment_name" {
  type        = string
  description = <<EOT
    (Optional)  The name of the environment where the resources will be deployed.

    Options:
      - dev
      - uat
      - test
      - prod

    Default: dev
  EOT

  default = "dev"

  validation {
    condition     = can(regex("(dev|uat|test|prod)", var.environment_name))
    error_message = "Err: environment name is not valid."
  }
}
variable "department" {
  type        = string
  description = "Name of the department"
}
variable "cost_centre" {
  type        = string
  description = "Cost Centre for the Resource or Resource Group"

}
variable "application_name" {
  type        = string
  description = "Name of the application"

}

variable "resource_group_name" {
  type        = string
  description = "Name of the Resource Group where service plan is to be deployed"

}

variable "sku" {
  type        = string
  description = "(Required) Defines which tier to use. Options are Basic, Standard or Premium. Please note that setting this field to Premium will force the creation of a new resource."

  validation {
    condition     = can(index(["Basic", "Standard", "Premium"], var.sku))
    error_message = "Invalid value for my_variable. Allowed values are value1, value2, or value3."
  }
}
variable "capacity" {
  type        = number
  description = "(Optional) Specifies the capacity. When sku is Premium, capacity can be 1, 2, 4, 8, or 16. When sku is Basic or Standard, capacity can be 0 only."

  default = 0 # Set a default value, assuming Basic or Standard sku

  validation {
    condition     = var.sku == "Premium" ? can(index([1, 2, 4, 8, 16], var.capacity)) : var.capacity == 0
    error_message = "Invalid value for capacity. When sku is Premium, capacity can be 1, 2, 4, 8, or 16. When sku is Basic or Standard, capacity can be 0 only."
  }
}
variable "zone_redundant" {
  type        = bool
  description = "(Optional) Whether or not this resource is zone redundant. sku needs to be Premium. Changing this forces a new resource to be created."

  default = false # Set a default value

  validation {
    condition     = var.sku == "Premium" ? !var.zone_redundant : var.zone_redundant
    error_message = "Invalid value for zone_redundant. When sku is Premium, zone_redundant should be true. When sku is Basic or Standard, zone_redundant should be false."
  }
}
variable "identity" {
  type    = any
  default = null
}

locals.tf

locals {
  tags = {
    "Environment" = var.environment_name
    "Department"  = var.department
    "Managed By"  = "Terraform Open Source"
    "Cost Centre" = var.cost_centre
  }
}

我现在调用模块中的代码如下,不幸的是,它报错如下

module "servicebus_namespace" {
  source              = "./.."
  location            = "australiaeast"
  environment_name    = "dev"
  department          = "Data Services"
  cost_centre         = "ABC101"
  application_name    = "demo"
  resource_group_name = "rg-dev-demo-australiaeast"

}

错误:

terraform init

Initializing the backend...
Initializing modules...
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
╷
│ Error: Invalid reference in variable validation
│ 
│   on ../variables.tf line 64, in variable "capacity":
│   64:     condition     = var.sku == "Premium" ? can(index([1, 2, 4, 8, 16], var.capacity)) : var.capacity == 0
│ 
│ The condition for variable "capacity" can only refer to the variable itself, using var.capacity.
╵

╷
│ Error: Invalid reference in variable validation
│ 
│   on ../variables.tf line 75, in variable "zone_redundant":
│   75:     condition     = var.sku == "Premium" ? !var.zone_redundant : var.zone_redundant
│ 
│ The condition for variable "zone_redundant" can only refer to the variable itself, using var.zone_redundant.

无法理解如何解决此错误,因为我希望容量取决于用户选择的 SKU。 zone_redundant 的情况相同。

terraform azureservicebus terraform-provider-azure
© www.soinside.com 2019 - 2024. All rights reserved.