Create custom domain for app services via terraform
Asked Answered
B

4

7

I am creating azure app services via terraform and following there documentation located at this site : https://www.terraform.io/docs/providers/azurerm/r/app_service.html

Here is the snippet for terraform script:

resource "azurerm_app_service" "app" {
  name                = "app-name"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  app_service_plan_id = "ommitted"

  site_config {
    java_version           = "1.8"
    java_container         = "TOMCAT"
    java_container_version = "8.5"
  }
  }

I need sub domain as well for my app services for which I am not able to find any help in terraform :

as of now url for app services is: https://abc.azure-custom-domain.cloud

and I want my url to be : https://*.abc.azure-custom-domain.cloud

I know this can be done via portal but is their any way by which we can do it via terraform?

Britneybritni answered 6/2, 2018 at 11:50 Comment(3)
Based on my knowledge, this is not possible. You need do it on Portal.Zachariahzacharias
I add it as an answer. Hope it will help more people.Zachariahzacharias
I think using the combination of ARM templates and Terraform it should workBritneybritni
Z
-1

This is not possible. You could the link you provided. If parameter is not in, the parameter is not supported by terraform.

You need do it on Azure Portal.

Zachariahzacharias answered 12/2, 2018 at 9:56 Comment(1)
This is out of date as it is possiblePopery
P
11

This is now possible using app_service_custom_hostname_binding (since PR#1087 on 6th April 2018)

resource "azurerm_app_service_custom_hostname_binding" "test" {
  hostname            = "www.mywebsite.com"
  app_service_name    = "${azurerm_app_service.test.name}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}
Pedagogy answered 15/4, 2019 at 13:38 Comment(4)
Instead of app service, is it possible to link it to an app service slot? There isn't a module for app service slots custom hostname bindingsImpermanent
Hi and_apo, there is an issue open to track this feature request: github.com/terraform-providers/terraform-provider-azurerm/… We used the workaround suggested in the issue of having an arm template resource to manage the slot hostname binding and it seemed to work well.Pedagogy
it says you need to configure the CNAME but doesn't specify where. Does anyone know where I do this? registry.terraform.io/providers/hashicorp/azurerm/latest/docs/…Venessavenetia
Hi @Venessavenetia it means you need to add a CNAME record to your custom domain that you want to use with App Service - so it depends on where your DNS is being hosted.Pedagogy
M
7

I have found it to be a tiny bit more complicated...

  1. DNS Zone (then set name servers at the registrar)
  2. App Service
  3. Domain verification TXT record
  4. CNAME record
  5. Hostname binding
  6. Certificate binding (SSL)
resource "azurerm_dns_zone" "dns-zone" {
  name                = var.azure_dns_zone
  resource_group_name = var.azure_resource_group_name
}

resource "azurerm_linux_web_app" "app-service" {
  name                = "some-service"
  resource_group_name = var.azure_resource_group_name
  location            = var.azure_region
  service_plan_id = "some-plan"
  site_config {}
}

resource "azurerm_dns_txt_record" "domain-verification" {
  name                = "asuid.api.domain.com"
  zone_name           = var.azure_dns_zone
  resource_group_name = var.azure_resource_group_name
  ttl                 = 300

  record {
    value = azurerm_linux_web_app.app-service.custom_domain_verification_id
  }
}

resource "azurerm_dns_cname_record" "cname-record" {
  name                = "domain.com"
  zone_name           = azurerm_dns_zone.dns-zone.name
  resource_group_name = var.azure_resource_group_name
  ttl                 = 300
  record              = azurerm_linux_web_app.app-service.default_hostname

  depends_on = [azurerm_dns_txt_record.domain-verification]
}

resource "azurerm_app_service_custom_hostname_binding" "hostname-binding" {
  hostname            = "api.domain.com"
  app_service_name    = azurerm_linux_web_app.app-service.name
  resource_group_name = var.azure_resource_group_name

  depends_on = [azurerm_dns_cname_record.cname-record]
}

resource "azurerm_app_service_managed_certificate" "example" {
  custom_hostname_binding_id = azurerm_app_service_custom_hostname_binding.hostname-binding.id
}

resource "azurerm_app_service_certificate_binding" "example" {
  hostname_binding_id = azurerm_app_service_custom_hostname_binding.hostname-binding.id
  certificate_id      = azurerm_app_service_managed_certificate.example.id
  ssl_state           = "SniEnabled"
}
Md answered 19/7, 2022 at 16:11 Comment(0)
Z
-1

This is not possible. You could the link you provided. If parameter is not in, the parameter is not supported by terraform.

You need do it on Azure Portal.

Zachariahzacharias answered 12/2, 2018 at 9:56 Comment(1)
This is out of date as it is possiblePopery
R
-3

I had the same issue & had to use PowerSHell to overcome it in the short-term. Maybe you could get Terraform to trigger the PSHell script... I haven't tried that yet!!!

PSHell as follows: -

$fqdn="www.yourwebsite.com"
$webappname="yourwebsite.azurewebsites.net"
Set-AzureRmWebApp -Name <YourAppServiceName> -ResourceGroupName <TheResourceGroupOfYourAppService> -HostNames @($fqdn,$webappname) 

IMPORTANT: Make sure you configure DNS FIRST i.e. CNAME or TXT record for the custom domain you're trying to set, else PSHell & even the Azure Portal manual method will fail.

Remuneration answered 11/3, 2018 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.