Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - re-use block definition in multiple places

I am having the following azure endpoint definition and I need to define several similar endpoints.

How can I reuse the delivery_rule definitions and global_delivery_rule so that I define them only once.

What is the best practice to achieve this?

Thank you in advance!

resource "azurerm_cdn_endpoint" "cdne1" {
  name                = "cdne1"
  origin_host_header  = "cdne1.z6.web.core.windows.net"
  origin_path         = "/applatest/"
  profile_name        = azurerm_cdn_profile.cdnp_clientapp_dev.name
  resource_group_name = azurerm_resource_group.rg_dev.name
  content_types_to_compress = local.cdne_content_types_to_compress
  is_http_allowed               = true
  is_https_allowed              = true
  location                      = "global"
  optimization_type             = "GeneralWebDelivery"
  querystring_caching_behaviour = "IgnoreQueryString"
  tags = {
    environment = "development"
  }

  origin {
    host_name  = "stclientappdevshoca1.z6.web.core.windows.net"
    name       = "stclientappdevshoca1-z6-web-core-windows-net"
  }

  delivery_rule {
    name  = "HTTPtoHTTPS"
    order = 1

    request_scheme_condition {
      match_values = [
        "HTTP",
      ]
      negate_condition = false
      operator         = "Equal"
    }

    url_redirect_action {
      protocol      = "Https"
      redirect_type = "Moved"
    }
  }
  delivery_rule {
    name  = "redirectToIndex"
    order = 2

    url_file_extension_condition {
      match_values = [
        "0",
      ]
      negate_condition = true
      operator         = "GreaterThan"
      transforms       = []
    }

    url_rewrite_action {
      destination             = "/index.html"
      preserve_unmatched_path = false
      source_pattern          = "/"
    }
  }
  delivery_rule {
    name  = "securityHeaders"
    order = 3

    modify_response_header_action {
      action = "Append"
      name   = "Strict-Transport-Security"
      value  = "max-age=31536000; includeSubDomains"
    }
    modify_response_header_action {
      action = "Append"
      name   = "Content-Security-Policy"
      value  = "script-src 'self'"
    }
    modify_response_header_action {
      action = "Append"
      name   = "X-Frame-Options"
      value  = "SAMEORIGIN"
    }
    modify_response_header_action {
      action = "Append"
      name   = "X-Content-Type-Options"
      value  = "nosniff"
    }
    modify_response_header_action {
      action = "Append"
      name   = "Referrer-Policy"
      value  = "no-referrer"
    }

    request_uri_condition {
      operator = "Any"
    }
  }

  global_delivery_rule {
    cache_expiration_action {
      behavior = "BypassCache"
    }
  }

  timeouts {}
}

enter image description here

like image 814
klodoma Avatar asked Sep 14 '25 01:09

klodoma


1 Answers

In your case best fit is Terraform dynamic blocks.

https://www.terraform.io/docs/language/expressions/dynamic-blocks.html

Here is a quick example(It can be improved)

  dynamic "delivery_rule" {
    for_each = var.delivery_rule_list
    content {
    name  = name = delivery_rule.value.name
    order = delivery_rule.value.order

    request_scheme_condition {
      match_values = delivery_rule.value.match_values
      negate_condition = delivery_rule.value.negate_condition 
      operator         = delivery_rule.value.operator         
      }

    url_redirect_action {
      protocol      = delivery_rule.value.protocol
      redirect_type = delivery_rule.value.redirect_type
      }
    }
  }

Here is an example of variables.tf(It can be improved)

variable "delivery_rule_list" {
      type = list(object({
        name = string
        order = number
        match_values = any
        negate_condition = bool
        operator = string         
        protocol = string
        redirect_type = string
      }))
      default = []
    }
like image 152
Andriy Bilous Avatar answered Sep 17 '25 18:09

Andriy Bilous