使用 privatendpoint 创建 SQL 数据库的 ARM 模板

ARM Template to create SQL Database with a privatendpoint

我在尝试使用 SQL 数据库及其专用端点部署 ARM 部署时遇到错误。 这是下面的代码

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "sqlAdministratorLogin": {
      "type": "string",
      "metadata": {
        "description": "The administrator username of the SQL logical server"
      }
    },
    "sqlAdministratorLoginPassword": {
      "type": "securestring",
      "metadata": {
        "description": "The administrator password of the SQL logical server."
      }
    },
   
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "vnetName": "powerStateManagement-vnet",
    "subnet1Name": "default",
    "sqlServerName": "[concat('sqlserver', uniqueString(resourceGroup().id))]",
    "databaseName": "[concat(variables('sqlServerName'),'/sample-db')]",
    "privateEndpointName": "myPrivateEndpoint",
    "privateDnsZoneName": "[concat('privatelink', environment().suffixes.sqlServerHostname)]",
    "pvtendpointdnsgroupname": "[concat(variables('privateEndpointName'),'/mydnsgroupname')]",
    "vnetResourceGroup":"powerStateManagement"
  },
  "resources": [
    {
      "type": "Microsoft.Sql/servers",
      "apiVersion": "2020-02-02-preview",
      "name": "[variables('sqlServerName')]",
      "location": "[parameters('location')]",
      "kind": "v12.0",
      "tags": {
        "displayName": "[variables('sqlServerName')]"
      },
      "properties": {
        "administratorLogin": "[parameters('sqlAdministratorLogin')]",
        "administratorLoginPassword": "[parameters('sqlAdministratorLoginPassword')]",
        "version": "12.0",
        "publicNetworkAccess": "Disabled"
      },
      "resources": [
      ]
    },
    {
      "type": "Microsoft.Sql/servers/databases",
      "apiVersion": "2020-02-02-preview",
      "name": "[variables('databaseName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Basic",
        "tier": "Basic",
        "capacity": 5
      },
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', variables('sqlServerName'))]"
      ],
      "tags": {
        "displayName": "[variables('databaseName')]"
      },
      "properties": {
        "collation": "SQL_Latin1_General_CP1_CI_AS",
        "edition": "Basic",
        "maxSizeBytes": 104857600,
        "requestedServiceObjectiveName": "Basic",
        "sampleName": "AdventureWorksLT"
      }
    },
    {
      "type": "Microsoft.Network/privateEndpoints",
      "apiVersion": "2020-06-01",
      "name": "[variables('privateEndpointName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[variables('vnetName')]",
        "[variables('sqlServerName')]"
      ],
      "properties": {
        "subnet": {
          "id": "[resourceId(variables('vnetResourceGroup'),'/','Microsoft.Network/virtualNetworks','/',variables('vnetName'),'/',variables('subnet1Name'))]"
        },
        "privateLinkServiceConnections": [
          {
            "name": "[variables('privateEndpointName')]",
            "properties": {
              "privateLinkServiceId": "[resourceId('Microsoft.Sql/servers',variables('sqlServerName'))]",
              "groupIds": [
                "sqlServer"
              ]
            }
          }
        ]
      }
    },

    {
      "type": "Microsoft.Network/privateDnsZones/virtualNetworkLinks",
      "apiVersion": "2020-01-01",
      "name": "[concat(variables('privateDnsZoneName'), '/', variables('privateDnsZoneName'), '-link')]",
      "location": "global",
      "dependsOn": [
        "[resourceId('Microsoft.Network/privateDnsZones', variables('privateDnsZoneName'))]",
          "[resourceId(variables('vnetResourceGroup'),'Microsoft.Network/virtualNetworks',variables('vnetName'))]"
      ],
      "properties": {
        "registrationEnabled": false,
        "virtualNetwork": {
          "id": "/subscriptions/*****/resourceGroups/powerStateManagement/providers/Microsoft.Network/virtualNetworks/powerStateManagement-vnet"
        }
      }
    },
    {
      "type": "Microsoft.Network/privateEndpoints/privateDnsZoneGroups",
      "apiVersion": "2020-06-01",
      "name": "[variables('pvtendpointdnsgroupname')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/privateDnsZones', variables('privateDnsZoneName'))]",
        "[variables('privateEndpointName')]"
      ],
      "properties": {
        "privateDnsZoneConfigs": [
          {
            "name": "config1",
            "properties": {
              "privateDnsZoneId": "[resourceId('Microsoft.Network/privateDnsZones', variables('privateDnsZoneName'))]"
            }
          }
        ]
      }
    }
  ]
}

这里的挑战是,当我尝试 运行 这段代码时,我总是得到这个错误

Deployment template validation failed: 'The template reference 'powerStateManagement-vnet' is not valid: could not find template resource or resource copy with this name.

''powerStateManagement-vnet' 是下面引用的现有虚拟网络

{
      "type": "Microsoft.Network/privateDnsZones/virtualNetworkLinks",
      "apiVersion": "2020-01-01",
      "name": "[concat(variables('privateDnsZoneName'), '/', variables('privateDnsZoneName'), '-link')]",
      "location": "global",
      "dependsOn": [
        "[resourceId('Microsoft.Network/privateDnsZones', variables('privateDnsZoneName'))]",
          "[resourceId(variables('vnetResourceGroup'),'Microsoft.Network/virtualNetworks',variables('vnetName'))]"
      ],
      "properties": {
        "registrationEnabled": false,
        "virtualNetwork": {
          "id": "/subscriptions/*****/resourceGroups/powerStateManagement/providers/Microsoft.Network/virtualNetworks/powerStateManagement-vnet"
        }
      }
    }

请帮忙

Microsoft.Network/privateEndpointsdependsOn 参数有问题。看来你的模板还有其他问题,我根据你的模板做了一些修改,试试下面:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sqlAdministratorLogin": {
            "type": "string",
            "metadata": {
                "description": "The administrator username of the SQL logical server"
            }
        },
        "sqlAdministratorLoginPassword": {
            "type": "securestring",
            "metadata": {
                "description": "The administrator password of the SQL logical server."
            }
        },

        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        }
    },
    "variables": {
        "vnetName": "powerStateManagement-vnet",
        "subnet1Name": "default",
        "sqlServerName": "[concat('sqlserver', uniqueString(resourceGroup().id))]",
        "databaseName": "[concat(variables('sqlServerName'),'/sample-db')]",
        "privateEndpointName": "myPrivateEndpoint",
        "privateDnsZoneName": "testdns.com",
        "pvtendpointdnsgroupname": "[concat(variables('privateEndpointName'),'/mydnsgroupname')]",
        "vnetResourceGroup": "powerStateManagement"
    },
    "resources": [{
            "type": "Microsoft.Sql/servers",
            "apiVersion": "2020-02-02-preview",
            "name": "[variables('sqlServerName')]",
            "location": "[parameters('location')]",
            "kind": "v12.0",
            "tags": {
                "displayName": "[variables('sqlServerName')]"
            },
            "properties": {
                "administratorLogin": "[parameters('sqlAdministratorLogin')]",
                "administratorLoginPassword": "[parameters('sqlAdministratorLoginPassword')]",
                "version": "12.0",
                "publicNetworkAccess": "Disabled"
            },
            "resources": [
            ]
        }, {
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2020-02-02-preview",
            "name": "[variables('databaseName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "Basic",
                "tier": "Basic",
                "capacity": 5
            },
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', variables('sqlServerName'))]"
            ],
            "tags": {
                "displayName": "[variables('databaseName')]"
            },
            "properties": {
                "collation": "SQL_Latin1_General_CP1_CI_AS",
                "edition": "Basic",
                "maxSizeBytes": 104857600,
                "requestedServiceObjectiveName": "Basic",
                "sampleName": "AdventureWorksLT"
            }
        }, {
            "type": "Microsoft.Network/privateEndpoints",
            "apiVersion": "2020-06-01",
            "name": "[variables('privateEndpointName')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]",
                "[resourceId('Microsoft.Sql/servers', variables('sqlServerName'))]"
            ],
            "properties": {
                "subnet": {
                    "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', variables('vnetName')),'/subnets/default')]"
                },
                "privateLinkServiceConnections": [{
                        "name": "[variables('privateEndpointName')]",
                        "properties": {
                            "privateLinkServiceId": "[resourceId('Microsoft.Sql/servers',variables('sqlServerName'))]",
                            "groupIds": [
                                "sqlServer"
                            ]
                        }
                    }
                ]
            }
        }, {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-05-01",
            "name": "[variables('vnetName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "172.22.0.0/16"
                    ]
                }
            },
            "resources": [{
                    "type": "subnets",
                    "apiVersion": "2020-05-01",
                    "location": "[resourceGroup().location]",
                    "name": "default",
                    "dependsOn": [
                        "[variables('vnetName')]"
                    ],
                    "properties": {
                        "addressPrefix": "172.22.0.0/24",
                        "privateEndpointNetworkPolicies": "Disabled"
                    }
                }
            ]
        }, {
            "type": "Microsoft.Network/privateDnsZones/virtualNetworkLinks",
            "apiVersion": "2020-01-01",
            "name": "[concat(variables('privateDnsZoneName'), '/', variables('privateDnsZoneName'), '-link')]",
            "location": "global",
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
            ],
            "properties": {
                "registrationEnabled": false,
                "virtualNetwork": {
                    "id":"[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
                }
            }
        }, {
            "type": "Microsoft.Network/privateEndpoints/privateDnsZoneGroups",
            "apiVersion": "2020-06-01",
            "name": "[variables('pvtendpointdnsgroupname')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/privateEndpoints', variables('privateEndpointName'))]"
            ],
            "properties": {
                "privateDnsZoneConfigs": [{
                        "name": "config1",
                        "properties": {
                            "privateDnsZoneId": "[resourceId('Microsoft.Network/privateDnsZones', variables('privateDnsZoneName'))]"
                        }
                    }
                ]
            }
        }
    ]
}

此模板与默认子网一起创建了一个新的虚拟网络,我使用我自己的私有 DNS 区域命名为:testdns.com。我已经通过 powershell 在我这边进行了测试,它对我有用。

结果