1
votes

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

1 Answers

1
votes

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)