Azure CDN Multi-Origin endpoint with terraform azapi provider

307 Views Asked by At

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"
    }
  })
}
1

There are 1 best solutions below

0
LCM On

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.

resource "azapi_resource" "endpoint" { 
  type = "Microsoft.Cdn/profiles/endpoints@2022-11-01-preview"
  name = "EDP-${local.cdnprofile_name}"
  location = azurerm_resource_group.rg.location
  parent_id = azurerm_cdn_profile.cdn.id
  body = jsonencode({
    properties = {
      isCompressionEnabled = true,
      contentTypesToCompress = var.cdn_content_compress,
      optimizationType = "GeneralWebDelivery",
      isHttpAllowed = true,
      isHttpsAllowed = true,
      defaultOriginGroup = {
        id = "/subscriptions/${var.subscription_id}/resourcegroups/${azurerm_resource_group.rg.name}/providers/Microsoft.Cdn/profiles/${azurerm_cdn_profile.cdn.name}/endpoints/EDP-${local.cdnprofile_name}/origingroups/${var.cdn_stg_org_group_name}"
      },
      origins = [{
        name = var.cdn_stg_org_name,
        properties = {
          enabled = true,
          hostName = "contoso.com",
          httpPort = 80,
          httpsPort = 443
        }
      },
      {
        name = var.cdn_app_org_name,
        properties = {
          enabled = true,
          hostName = "contoso.com",
          httpPort = 80,
          httpsPort = 443
        }
      }],
      originGroups = [{
        name = var.cdn_stg_org_group_name,
        properties = {
          origins = [{
            id = "/subscriptions/${var.subscription_id}/resourcegroups/${azurerm_resource_group.rg.name}/providers/Microsoft.Cdn/profiles/${azurerm_cdn_profile.cdn.name}/endpoints/EDP-${local.cdnprofile_name}/origins/${var.cdn_stg_org_name}"
          }] 
        }
      },
      {
        name = var.cdn_app_org_group_name,
        properties = {
          origins = [{
            id = "/subscriptions/${var.subscription_id}/resourcegroups/${azurerm_resource_group.rg.name}/providers/Microsoft.Cdn/profiles/${azurerm_cdn_profile.cdn.name}/endpoints/EDP-${local.cdnprofile_name}/origins/${var.cdn_app_org_name}"
          }] 
        }
      }]
    }
  })
}