I'm trying to create an Azure CDN endpoint with one (or more) origin group(s) where each origin group has one or more origins.
I have the following code which - indeed - does not work because I'm referencing the endpoint from the origin group through the parent_id but I also must reference the origin group from the endpoint when I want to set the defaultOriginGroup.id which - of course leads to a cycle.
How can I set the defaultOriginGroup in a scenario like this?
If it is possible to solve this with the azurerm provider that's also fine.
resource "azapi_resource" "profile" {
type = "Microsoft.Cdn/profiles@2022-11-01-preview"
name = "MyPforile2"
location = azurerm_resource_group.main.location
parent_id = azurerm_resource_group.main.id
body = jsonencode({
sku = {
name = "Standard_Microsoft"
}
})
}
resource "azapi_resource" "endpoint" {
type = "Microsoft.Cdn/profiles/endpoints@2022-11-01-preview"
name = "MyEndpoint2"
location = azurerm_resource_group.main.location
parent_id = azapi_resource.profile.id
body = jsonencode({
properties = {
defaultOriginGroup = {
id = azapi_resource.originGroup.id
},
origins = [{
name = "DefaultOrigin",
properties = {
enabled = true,
hostName = "contoso.com",
httpPort = 80,
httpsPort = 443
}
}]
}
})
}
resource "azapi_resource" "originGroup" {
type = "Microsoft.Cdn/profiles/endpoints/originGroups@2022-11-01-preview"
name = "MyOriginGroup2"
parent_id = azapi_resource.endpoint.id
body = jsonencode({
origins = [{
id = azapi_resource.origin.id
}]
})
}
resource "azapi_resource" "origin" {
type = "Microsoft.Cdn/profiles/endpoints/origins@2022-11-01-preview"
name = "MyOrigin2"
parent_id = azapi_resource.endpoint.id
body = jsonencode({
properties = {
enabled = true
hostName = "contoso.com"
httpPort = 80
httpsPort = 443
originHostHeader = "www.contoso.com"
}
})
}
Found your question while I was searching for official support for multi-origin CDN using AzureRM. There's an open issue about that and the short answer is that they are yet to support it.
So I tried your solution and came out with the following code to address my needs and I think I solved the issue you had to set the defaultOriginGroup. I just provision everything together using the Endpoint API and reference the IDs as you would do when using ARM Templates. I also use variables to name everything so building the resource IDs is less of a pain.
Its not as elegant a solution as I would like, but since I could not find a better way to handle the resource IDs, and using separate resources would cause cross-reference issues, I had no other option.