尝试在 Azure 云提供商上创建私有 link 时出现问题

Issues when trying to create private link on azure cloud provider

我正在尝试开发在 Azure 上创建虚拟机并使用私有 link 将其连接到 Azure Postgresql 实例的 terraform 脚本。我总是收到此错误:

Error: creating Private Endpoint "n4gx6y-endpoint" (Resource Group "PGSQLResourceGroup"): 
network.PrivateEndpointsClient#CreateOrUpdate: 
Failure sending request: StatusCode=400 -- Original Error: 
Code="IncorrectPrivateLinkServiceConnectionGroupId" 
Message="Call to Microsoft.DBforPostgreSQL/servers failed. 
Error message: Private Link Service Connection Group Id is 
incorrect for Azure Database for PostgreSQL" Details=[]

  on main.tf line 68, in resource "azurerm_private_endpoint" "example":
  68: resource "azurerm_private_endpoint" "example" {

这是脚本:

# -*- mode: hcl; coding: utf-8; -*-
# vim: set syntax=hcl fileencoding=utf-8

terraform {
  required_version = ">= 0.12, < 0.13"
}

provider "azurerm" {
  version                 = ">=2.20"
  subscription_id         = var.subscription_id
  client_id               = var.client_id
  client_certificate_path = var.client_certificate_path
  tenant_id               = var.tenant_id
  features {}
}

provider "random" {
  version = "~> 2.3"
}

resource "random_string" "random" {
  length  = 6
  special = false
  upper   = false
}

resource "azurerm_resource_group" "grp" {
  name     = "PGSQLResourceGroup"
  location = var.location

  tags = {
    environment = "Terraform Demo"
  }
}

resource "azurerm_virtual_network" "example" {
  name                = "${random_string.random.result}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.grp.location
  resource_group_name = azurerm_resource_group.grp.name
}

resource "azurerm_subnet" "example" {
  name                                           = "${random_string.random.result}-subnet"
  resource_group_name                            = azurerm_resource_group.grp.name
  virtual_network_name                           = azurerm_virtual_network.example.name
  address_prefixes                               = ["10.0.1.0/24"]
  enforce_private_link_endpoint_network_policies = true
}

resource "azurerm_postgresql_server" "example" {
  name                = "${random_string.random.result}-pg"
  location            = azurerm_resource_group.grp.location
  resource_group_name = azurerm_resource_group.grp.name

  sku_name = "GP_Gen5_2"

  backup_retention_days        = 7
  geo_redundant_backup_enabled = false
  storage_mb                   = 51200
  auto_grow_enabled            = true
  administrator_login          = "mysqladmin"
  administrator_login_password = "H@Sh1CoR3!"
  version                      = "11"
  ssl_enforcement_enabled      = true
}

resource "azurerm_private_endpoint" "example" {
  name                = "${random_string.random.result}-endpoint"
  location            = azurerm_resource_group.grp.location
  resource_group_name = azurerm_resource_group.grp.name
  subnet_id           = azurerm_subnet.example.id

  private_service_connection {
    name                           = "${random_string.random.result}-privateserviceconnection"
    private_connection_resource_id = azurerm_postgresql_server.example.id
    subresource_names              = ["postgresServer"]
    is_manual_connection           = false
  }
}

这里是 github 存储库,其中包含相同的脚本,一个用于 Postgres,一个用于 Mariadb。该错误仅出现在 Postgres 中。

https://github.com/kiklop74/terraform-private-link-issue

问题实际上与脚本的这一部分有关:

  private_service_connection {
    name                           = "${random_string.random.result}-privateserviceconnection"
    private_connection_resource_id = azurerm_postgresql_server.example.id
    subresource_names              = ["postgresServer"] # <-- HERE!
    is_manual_connection           = false
  }

事实证明,每个受支持的私有 link Azure 托管服务都有硬编码的子资源名称。对于 MariaDB 实例,它是 mariadbServer 对于 Postgres,它是 postgresqlServer。我错误地放在那里 postgresServer 认为它只是任意值。

private_service_connection {
  name                           = "${random_string.random.result}- 
  privateserviceconnection"
  private_connection_resource_id = azurerm_postgresql_server.example.id
  subresource_names              = ["postgresqlServer"] # <-- HERE!
  is_manual_connection           = false

}