从 ARM 模板获取 API 管理的 public IP 地址

Get public IP address of API Management from ARM template

我得到了这个 API 管理 ARM 模板

{
      "apiVersion": "2017-03-01",
      "name": "[variables('am-apimanagement-service-name')]",
      "type": "Microsoft.ApiManagement/service",
      "location": "North Europe",
      "sku": {
        "name": "[parameters('am-sku')]",
        "capacity": "[parameters('am-skuCount')]"
      },
      "properties": {
        "publisherEmail": "[parameters('am-publisher-email-p')]",
        "publisherName": "[parameters('am-publisher-name-p')]"
      },
      "resources": [
        {
          "type": "apis",
          "apiVersion": "2017-03-01",
          "name": "test",
          "dependsOn": [
            "[concat('Microsoft.ApiManagement/service/',variables('am-apimanagement-service-name'))]"
          ],
          "properties": {
            "displayName": "test",
            "description": "",
            "serviceUrl": "[concat('https://test-',parameters('environment'),'.azurewebsites.net')]",
            "path": "test",
            "protocols": [
              "https"
            ],
            "isCurrent": true
          },
          "resources": [
            {
              "apiVersion": "2017-03-01",
              "type": "operations",
              "name": "GetTEst",
              "dependsOn": [
                "[concat('Microsoft.ApiManagement/service/', variables('am-apimanagement-service-name'), '/apis/test')]"
              ],
              "properties": {
                "displayName": "GET",
                "method": "GET",
                "urlTemplate": "/api/sites",
                "description": "Get"
              }
            }
          ]

        }
      ]
    }

还有这个 Web API ARM 模板

   {
      "apiVersion": "2016-03-01",
      "name": "[variables('swa-name')]",
      "type": "Microsoft.Web/sites",
      "properties": {
        "name": "[variables('swa-name')]",
        "serverFarmId": "[variables('swa-hosting-plan-name')]",
        "hostingEnvironment": "[parameters('swa-hosting-environment')]",
        "siteConfig": {
          "appSettings": [
            {
              "name": "Test:ConnectionString",
              "value": "[concat('Server=tcp:', reference(resourceId('Microsoft.Sql/servers/', variables('db-serverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', variables('db-databaseName'), ';Persist Security Info=False;User ID=', parameters('db-administratorLogin'), ';Password=', parameters('db-administratorLoginPassword'), ';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;')]"
            }
          ],
          "ipSecurityRestrictions": [
            {
              "ipAddress": "[variables('test-ip-address')]",
              "subnetMask": "255.255.255.255"
            }
          ]
        }
      },
      "location": "[parameters('swa-location')]",
      "tags": {

      },
      "kind": "api",
      "dependsOn": [
        "[concat('Microsoft.Web/serverfarms/', variables('swa-hosting-plan-name'))]"
      ]
    }

如何使用引用功能从 Web API 获取 API 管理资源的 IP 地址?我想将 API 管理 IP 地址放在 ipSecurityRestrictions 中。我想不通,这让我很沮丧。

我已经从 ARM 模板的 Web api 资源资源部分试过了:

"ipSecurityRestrictions": [
            {
              "ipAddress": "[variables('test-ip-address')]",
              "subnetMask": "255.255.255.255"
            },
            {
              "ipAddress": "[reference(variables('am-apimanagement-service-name')).publicIPAddresses[0]]",
              "subnetMask": "255.255.255.255"
            }
          ]

但是没用。也许我无法在此过程的这一步中获取 IP 地址?我正在阅读有关输出和链接模板的信息。也许这就是解决方案?

我认为您应该将 api 版本添加到您的参考中,因为 API 管理未部署在您所参考的同一模板中。

https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#reference

像这样

{
          "ipAddress": "[reference(variables('am-apimanagement-service-name'), '2017-03-01').publicIPAddresses[0]]",
          "subnetMask": "255.255.255.255"
}

这里最疯狂的是我需要两件事:

  1. 参考的api版本
  2. 我需要引用 staticIp 属性

问题在于,免费层 API 管理资源实际上并未分配静态 IP,而是动态 IP。仍然将静态ip地址分配给引用对象的staticIp 属性:

"ipAddress": "[reference(variables('am-apimanagement-service-name'),'2017-03-01').staticIps[0]]"

这将正确引用 IP。