Skip to content

Commit 88380ab

Browse files
rajbosCopilot
andcommitted
fix: add null_resource to register hostname before managed cert creation
Azure API requires the hostname to be bound to a container app before a managed certificate can be created for it (error: RequireCustomHostnameInEnvironment). This creates a circular dependency in Terraform: the managed cert needs the hostname registered, but azurerm_container_app_custom_domain with SniEnabled needs the cert to exist. Break the cycle with a null_resource that registers the hostname (Disabled binding) via az CLI local-exec, giving the cert resource a hostname to validate against. TF then creates the cert and upgrades the binding to SniEnabled via azurerm_container_app_custom_domain. Also adds the hashicorp/null provider (~> 3.0) to providers.tf. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 43b8c0b commit 88380ab

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

sharing-server/infra/main.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ resource "azurerm_container_app" "this" {
186186
# DNS prerequisites (must exist before applying):
187187
# CNAME <subdomain> → local.aca_fqdn
188188
# TXT asuid.<subdomain> → azurerm_container_app_environment.this.custom_domain_verification_id
189+
#
190+
# Azure requires the hostname to be registered on the container app BEFORE a
191+
# managed certificate can be created for it. We use a null_resource to register
192+
# the hostname (binding type Disabled) via az CLI, which breaks the circular
193+
# dependency: cert needs hostname, SniEnabled binding needs cert.
194+
195+
resource "null_resource" "hostname_registration" {
196+
count = var.custom_domain != "" ? 1 : 0
197+
198+
triggers = {
199+
hostname = var.custom_domain
200+
app_id = azurerm_container_app.this.id
201+
}
202+
203+
provisioner "local-exec" {
204+
command = "az containerapp hostname add --name '${azurerm_container_app.this.name}' --resource-group '${var.resource_group_name}' --hostname '${var.custom_domain}' 2>/dev/null || true"
205+
}
206+
207+
depends_on = [azurerm_container_app.this]
208+
}
189209

190210
resource "azurerm_container_app_environment_managed_certificate" "this" {
191211
count = var.custom_domain != "" ? 1 : 0
@@ -194,6 +214,8 @@ resource "azurerm_container_app_environment_managed_certificate" "this" {
194214
subject_name = var.custom_domain
195215
domain_control_validation = "CNAME"
196216

217+
depends_on = [null_resource.hostname_registration]
218+
197219
lifecycle {
198220
create_before_destroy = true
199221
}

sharing-server/infra/providers.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ terraform {
1010
source = "hashicorp/random"
1111
version = "~> 3.6"
1212
}
13+
null = {
14+
source = "hashicorp/null"
15+
version = "~> 3.0"
16+
}
1317
}
1418

1519
# All backend values are supplied via -backend-config flags in CI.

0 commit comments

Comments
 (0)