首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Terraform -在多个位置重用块定义

Terraform -在多个位置重用块定义
EN

Stack Overflow用户
提问于 2021-03-22 18:51:02
回答 1查看 216关注 0票数 0

我有以下azure端点定义,我需要定义几个类似的端点。

如何重用delivery_rule定义和global_delivery_rule,以便只定义一次。

实现这一目标的最佳实践是什么?

提前谢谢你!

代码语言:javascript
运行
复制
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 {}
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-22 21:34:31

在您的情况下,最适合的是Terraform动态块。

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

这里有一个简单的例子(可以改进)

代码语言:javascript
运行
复制
  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
      }
    }
  }

下面是variables.tf的一个例子(它可以改进)

代码语言:javascript
运行
复制
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 = []
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66744605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档