在Azure中创建PostgreSQL服务器后Terraform抛出错误

问题描述

创建Postgresql服务器后Terraform引发错误

##我尝试过的事情


- 更改var.ad_admin_login_name的管理员名称
- 搜索了文档,但找不到任何解决方案。
- 活动目录admin不存在。而且我不需要导入它。

套用地形后输出错误代码

Error: A resource with the ID "/subscriptions/[redacted]/resourceGroups/app-postgresql-sandbox-useast1/providers/Microsoft.DBforPostgreSQL/servers/postgresql-sandbox-postgres02/administrators/activeDirectory" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_postgresql_active_directory_administrator" for more information.

  on ..\postgreSQL_module\postgreSQL\main.tf line 132,in resource "azurerm_postgresql_active_directory_administrator" "aduser2":
 132: resource "azurerm_postgresql_active_directory_administrator" "aduser2" {

main.tf

# toggles on/off auditing and advanced threat protection policy for sql server
locals {
    if_threat_detection_policy_enabled = var.enable_threat_detection_policy ? [{}] : []                
}

# Configure the Azure Provider
provider "azurerm" {
  version = ">=2.2.0"
  features {}
}

# creates random password for postgresSQL admin account
resource "random_password" "primary_pw" {
  length      = 24
  special     = true
}

# creates random password for postgresSQL admin account
resource "random_password" "replica_pw" {
  length      = 24
  special     = true
}

# Manages a PostgreSQL Server
resource "azurerm_postgresql_server" "primary" {
  name                = "${var.names.product_name}-${var.names.environment}-postgres${var.srvr_id}"
  location            = var.location
  resource_group_name = var.resource_group_name
  tags                = var.tags

  administrator_login          = var.administrator_login
  administrator_login_password = random_password.primary_pw.result

  sku_name   = var.sku_name
  version    = var.db_version
  storage_mb = var.storage_mb

  backup_retention_days             = var.backup_retention_days
  geo_redundant_backup_enabled      = var.geo_redundant_backup_enabled
  auto_grow_enabled                 = var.auto_grow_enabled
  public_network_access_enabled     = var.public_network_access_enabled
  infrastructure_encryption_enabled = var.infrastructure_encryption_enabled
  ssl_enforcement_enabled           = true
  ssl_minimal_tls_version_enforced  = "TLS1_2"

  dynamic "threat_detection_policy" {
      for_each = local.if_threat_detection_policy_enabled
      content {
          storage_endpoint           = var.storage_endpoint
          storage_account_access_key = var.storage_account_access_key 
          retention_days             = var.log_retention_days
      }
  }
}

# Manages a PostgreSQL Server
resource "azurerm_postgresql_server" "replica" {
  count               = var.enable_replica ? 1 : 0
  name                = "${var.names.product_name}-${var.names.environment}-postgres${var.srvr_id_replica}"
  location            = var.replica_server_location
  resource_group_name = var.resource_group_name

  administrator_login          = var.administrator_login
  administrator_login_password = random_password.replica_pw.result

  sku_name   = var.sku_name
  version    = var.db_version
  storage_mb = var.storage_mb

  backup_retention_days             = var.backup_retention_days
  geo_redundant_backup_enabled      = var.geo_redundant_backup_enabled
  auto_grow_enabled                 = var.auto_grow_enabled
  public_network_access_enabled     = var.public_network_access_enabled
  infrastructure_encryption_enabled = var.infrastructure_encryption_enabled
  ssl_enforcement_enabled           = true
  ssl_minimal_tls_version_enforced  = "TLS1_2"
  create_mode                       = var.create_mode
  creation_source_server_id         = azurerm_postgresql_server.primary.id

  dynamic "threat_detection_policy" {
      for_each = local.if_threat_detection_policy_enabled
      content {
          storage_endpoint           = var.storage_endpoint
          storage_account_access_key = var.storage_account_access_key 
          retention_days             = var.log_retention_days
      }
  }
}

# Manages a PostgreSQL Database within a PostgreSQL Server
resource "azurerm_postgresql_database" "db" {
  count               = var.enable_db ? 1 : 0
  name                = "${var.names.product_name}-${var.names.environment}db-${var.srvr_id}"
  resource_group_name = var.resource_group_name
  server_name         = azurerm_postgresql_server.primary.name
  charset             = "UTF8"
  collation           = "English_United States.1252"
}

# Sets a PostgreSQL Configuration value on a PostgreSQL Server.
resource "azurerm_postgresql_configuration" "config" {
  for_each            = local.postgresql_config

  name                = each.key
  resource_group_name = var.resource_group_name
  server_name         = azurerm_postgresql_server.primary.name
  value               = each.value
}

# Sets a PostgreSQL Configuration value on a PostgreSQL Server.
resource "azurerm_postgresql_configuration" "config_replica" {
  for_each            = local.postgresql_config

  name                = each.key
  resource_group_name = var.resource_group_name
  server_name         = azurerm_postgresql_server.replica.0.name
  value               = each.value
}

data "azurerm_client_config" "current" {}

# PostgreSQL Azure AD Admin - Default is "false"
resource "azurerm_postgresql_active_directory_administrator" "aduser1" {
  count               = var.enable_postgresql_ad_admin ? 1 : 0
  server_name         = azurerm_postgresql_server.primary.name
  resource_group_name = var.resource_group_name
  login               = var.ad_admin_login_name 
  tenant_id           = data.azurerm_client_config.current.tenant_id
  object_id           = data.azurerm_client_config.current.object_id
}

resource "azurerm_postgresql_active_directory_administrator" "aduser2" {
  count               = var.enable_replica && var.enable_postgresql_ad_admin ? 1 : 0
  server_name         = azurerm_postgresql_server.replica.0.name
  resource_group_name = var.resource_group_name
  login               = var.ad_admin_login_name_replica
  tenant_id           = data.azurerm_client_config.current.tenant_id
  object_id           = data.azurerm_client_config.current.object_id
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)