本文介绍了ARM 返回要在 Terraform 脚本中使用的应用服务环境 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Terraform 不允许部署应用服务环境,因此我使用 azurerm_template_deployment 作为解决方法.但是,我想在稍后创建的应用服务计划资源中引用应用服务环境 ID.如何使用此方法获取和保存应用服务环境的 ID?

Terraform does not allow for the deployment of App Service Environments so I am using the azurerm_template_deployment as a work around. However, I want to reference the App Service Environment ID in an App Service Plan resource that I am creating later. How would I get and save the ID of the App Service Environment using this method?

我在应用服务计划资源中使用depends_on标签来确保它在应用服务环境之后创建,但我不知道如何从创建中获取id并保存到变量中.我认为这会涉及到ARM模板的变量和输出标签的使用.

I am using the depends_on tag in the app service plan resource to ensure its creation after the app service environment, but I can not figure out how to get the id out of the creation and save to a variable. I think that it would involve the use of the variable and output tags of the ARM template.

resource "azurerm_template_deployment" "ase" {
  name                = "ILBASE_ARM_template"
  resource_group_name = "${azurerm_resource_group.ase.name}"

  template_body = <<DEPLOY

  {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "ilbase_name": {
        "type": "string"
      },
      "ilbase_domain_name": {
        "type": "string"
      },
      "ilbase_subnet_name": {
        "type": "string"
      },
      "ilbase_rglocation": {
        "defaultValue": "East US",
        "type": "string"
      },
      "vnet_id": {
        "type": "string"
      }
    },
    "variables": {
    },
    "resources": [
      {
        "apiVersion": "2016-09-01",
        "type": "Microsoft.Web/hostingEnvironments",
        "name": "[parameters('ilbase_name')]",
        "kind": "ASEV2",
        "location": "[parameters('ilbase_rglocation')]",
        "properties": {
          "name": "[parameters('ilbase_name')]",
          "location": "[parameters('ilbase_rglocation')]",
          "virtualNetwork": {
            "Id": "[parameters('vnet_id')]",
            "Subnet": "[parameters('ilbase_subnet_name')]"
          },
          "internalLoadBalancingMode": "Web, Publishing",
          "multiSize": "Standard_D1_V2",
          "multiRoleCount": 2,
          "workerPools": null,
          "ipsslAddressCount": 0,
          "dnsSuffix": "[parameters('ilbase_domain_name')]",
          "networkAccessControlList": [],
          "frontEndScaleFactor": 15,
          "apiManagementAccountId": null,
          "suspended": false,
          "dynamicCacheEnabled": null,
          "clusterSettings": null
        }
      }
    ],
    "outputs": {
    }
  }

  DEPLOY

  parameters {
    "vnet_id"            = "${azurerm_virtual_network.main_vnet.id}"
    "ilbase_subnet_name" = "${azurerm_subnet.ase.name}"
    "ilbase_name"        = "${var.env}-ASE-001"
    "ilbase_domain_name" = "${var.dnsName}"
    "ilbase_rglocation" = "${var.location}"
  }

  deployment_mode = "Incremental"
}

resource "azurerm_app_service_plan" "test" {
  name                = "api-appserviceplan-pro"
  location            = "${var.location}"
  resource_group_name = "${azurerm_resource_group.ase.name}"
  app_service_environment_id = ????????????????????

  sku {
    tier = "Isolated"
    size = "S1"
  }

  depends_on = ["azurerm_template_deployment.ase"]
}

提前感谢您的帮助!

推荐答案

在 ARM 模板中,使用 outputs 将输出设置为应用服务环境 ID.

In the ARM template, use outputs to set an output to the app service environment ID.

(像这样,没有机会测试,任何关于更改的反馈将不胜感激!)

(something like this, didn't have a chance to test, any feedback on changes would be greatly appreciated!)

"outputs": {
  "app_service_evironment_id": {
    "type": "string",
    "value": "[resourceId('Microsoft.Web/hostingEnvironments', parameters('ilbase_name'))]"
  }
}

azurerm_template_deployment 支持 输出地图.使用此地图,您可以设置

The azurerm_template_deployment supports an outputs map. Using this map, you can then set

azurerm_app_service_plan.test.app_service_environment_id = azurerm_template_deployment.ase.outputs["app_service_evironment_id"]

depends_on 不是必需的,应该是隐式的(因为 azurerm_app_service_plan 使用 azurerm_template_deployment 的输出)

The depends_on shouldn't be necessary and should be implicit (since the azurerm_app_service_plan uses an output of azurerm_template_deployment)

这篇关于ARM 返回要在 Terraform 脚本中使用的应用服务环境 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:24