|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + azapi = { |
| 4 | + source = "Azure/azapi" |
| 5 | + } |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +locals { |
| 10 | + endpoint_name = "${var.stack_name}-cdn" |
| 11 | + default_origin_group_name = "website-origin-group" |
| 12 | +} |
| 13 | + |
| 14 | +data "azurerm_client_config" "current" {} |
| 15 | + |
| 16 | +# Create the CDN profile for the website |
| 17 | +resource "azurerm_cdn_profile" "website_profile" { |
| 18 | + name = "website-profile" |
| 19 | + resource_group_name = var.resource_group_name |
| 20 | + location = var.location |
| 21 | + sku = "Standard_Microsoft" |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +# Create the CDN endpoint with full configuration using azapi, due to limitations in the azurerm provider |
| 26 | +# This includes all origins, origin groups, and delivery rule, see https://github.com/hashicorp/terraform-provider-azurerm/issues/10771 |
| 27 | +resource "azapi_resource" "cdn_endpoint" { |
| 28 | + type = "Microsoft.Cdn/profiles/endpoints@2024-09-01" |
| 29 | + name = local.endpoint_name |
| 30 | + parent_id = azurerm_cdn_profile.website_profile.id |
| 31 | + location = var.location |
| 32 | + |
| 33 | + body = { |
| 34 | + properties = { |
| 35 | + isHttpAllowed = false |
| 36 | + isHttpsAllowed = true |
| 37 | + isCompressionEnabled = true |
| 38 | + contentTypesToCompress = [ |
| 39 | + "text/html", "text/css", "application/javascript", |
| 40 | + "application/json", "image/svg+xml", "font/woff", "font/woff2" |
| 41 | + ] |
| 42 | + |
| 43 | + # Define all origins in one place |
| 44 | + origins = concat( |
| 45 | + # Storage origin (duplicated from Terraform resource) |
| 46 | + [{ |
| 47 | + name = var.storage_account_name |
| 48 | + properties = { |
| 49 | + hostName = var.storage_account_primary_web_host |
| 50 | + originHostHeader = var.storage_account_primary_web_host |
| 51 | + enabled = true |
| 52 | + httpPort = 80 |
| 53 | + httpsPort = 443 |
| 54 | + } |
| 55 | + }], |
| 56 | + # API origins |
| 57 | + [for key in sort(keys(var.apis)) : { |
| 58 | + name = "api-origin-${key}" |
| 59 | + properties = { |
| 60 | + hostName = replace(replace(var.apis[key].gateway_url, "https://", ""), "/", "") |
| 61 | + originHostHeader = replace(replace(var.apis[key].gateway_url, "https://", ""), "/", "") |
| 62 | + httpPort = 80 |
| 63 | + httpsPort = 443 |
| 64 | + enabled = true |
| 65 | + } |
| 66 | + }] |
| 67 | + ) |
| 68 | + |
| 69 | + # Define all origin groups |
| 70 | + originGroups = concat( |
| 71 | + # Website origin group |
| 72 | + [{ |
| 73 | + name = "website-origin-group" |
| 74 | + properties = { |
| 75 | + origins = [{ |
| 76 | + id = "${azurerm_cdn_profile.website_profile.id}/endpoints/${local.endpoint_name}/origins/${var.storage_account_name}" |
| 77 | + }] |
| 78 | + } |
| 79 | + }], |
| 80 | + # API origin groups |
| 81 | + [for key in sort(keys(var.apis)) : { |
| 82 | + name = "api-origin-group-${key}" |
| 83 | + properties = { |
| 84 | + origins = [{ |
| 85 | + id = "${azurerm_cdn_profile.website_profile.id}/endpoints/${local.endpoint_name}/origins/api-origin-${key}" |
| 86 | + }] |
| 87 | + } |
| 88 | + }] |
| 89 | + ) |
| 90 | + |
| 91 | + # Set default origin group |
| 92 | + defaultOriginGroup = { |
| 93 | + id = "${azurerm_cdn_profile.website_profile.id}/endpoints/${local.endpoint_name}/originGroups/website-origin-group" |
| 94 | + } |
| 95 | + |
| 96 | + # Define delivery rules |
| 97 | + deliveryPolicy = { |
| 98 | + description = "API routing policy" |
| 99 | + rules = [ |
| 100 | + for idx, key in { for i, k in sort(keys(var.apis)) : i => k } : { |
| 101 | + name = "forward_${key}" |
| 102 | + order = idx + 1 |
| 103 | + conditions = [{ |
| 104 | + name = "UrlPath" |
| 105 | + parameters = { |
| 106 | + typeName = "DeliveryRuleUrlPathMatchConditionParameters" |
| 107 | + operator = "BeginsWith" |
| 108 | + matchValues = ["/api/${key}"] |
| 109 | + transforms = ["Lowercase"] |
| 110 | + negateCondition = false |
| 111 | + } |
| 112 | + }] |
| 113 | + actions = [ |
| 114 | + { |
| 115 | + name = "OriginGroupOverride" |
| 116 | + parameters = { |
| 117 | + typeName = "DeliveryRuleOriginGroupOverrideActionParameters" |
| 118 | + originGroup = { |
| 119 | + id = "${azurerm_cdn_profile.website_profile.id}/endpoints/${local.endpoint_name}/originGroups/api-origin-group-${key}" |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + { |
| 124 | + name = "UrlRewrite" |
| 125 | + parameters = { |
| 126 | + typeName = "DeliveryRuleUrlRewriteActionParameters" |
| 127 | + sourcePattern = "/api/${key}/" |
| 128 | + destination = "/" |
| 129 | + } |
| 130 | + } |
| 131 | + ] |
| 132 | + } |
| 133 | + ] |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + response_export_values = ["properties.hostName"] |
| 139 | + |
| 140 | + depends_on = [azurerm_cdn_profile.website_profile] |
| 141 | +} |
| 142 | + |
0 commit comments