【问题标题】:ARM Template to create SQL Database with a privatendpoint使用 privatendpoint 创建 SQL 数据库的 ARM 模板
【发布时间】:2021-04-05 14:31:03
【问题描述】:

我在尝试使用 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"
        }
      }
    }

请帮忙

【问题讨论】:

    标签: azure azure-resource-manager


    【解决方案1】:

    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 对我进行了测试,它对我有用。

    结果

    【讨论】:

    • 感谢您的回复并确保您的代码有效。但是,就我而言,powerstatemanagement-vnet 是一个现有的 vnet,我该如何处理这种获取现有 vNet 的情况。
    • 您可以在需要的地方识别您的 vnet 资源 ID。如果我的帖子是有帮助的,请接受它作为答案:)
    • @user989865,怎么样?您的问题解决了吗?
    猜你喜欢
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2018-09-22
    相关资源
    最近更新 更多