从ARM模板获取API Management的公共IP地址

时间:2018-05-04 09:10:05

标签: azure azure-api-management 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 Management资源的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地址?我正在阅读有关输出和链接模板的内容。也许这是解决方案?

2 个答案:

答案 0 :(得分:2)

我认为您应该将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 :(得分:1)

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

  1. apiVersion to the reference
  2. 我需要引用staticIp属性
  3. 问题在于,免费层API管理资源实际上没有分配静态IP,而是动态IP。静态IP地址仍然分配给引用对象的staticIp属性:

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

    这将正确引用IP。