ARM Return App Service Environment ID to use in Terraform Script

妖精的绣舞 提交于 2020-02-05 04:38:19

问题


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?

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"]
}

Thanks in advance for any help!


回答1:


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'))]"
  }
}

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"]

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



来源:https://stackoverflow.com/questions/56278126/arm-return-app-service-environment-id-to-use-in-terraform-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!