Terraform和Cloudfront中的ordered_cache_behavior作为变量。

huangapple go评论51阅读模式
英文:

ordered_cache_behavior as variable with Terraform & Cloudfront

问题

在Terraform中,如果要向CloudFront分发资源添加ordered_cache_behaviors,或者默认情况下不添加任何ordered_cache_behavior,是否可能实现?

我想要包含一个有序行为的数组,也可以为空。是否可能?

resource "aws_cloudfront_distribution" "proxy_cdn" {
  enabled = true
  price_class = "PriceClass_100"

  origin {
    origin_id   = var.cdn_proxy_origin_id
    domain_name = var.cdn_domain_name

    custom_origin_config {
      origin_protocol_policy = "https-only"
      http_port              = "80"
      https_port             = "443"
      origin_ssl_protocols   = ["TLSv1", "TLSv1.1", "TLSv1.2"]
    }
  }

  # 在此处实现有序行为的数组,可为空
  ordered_cache_behavior {
    # 有序行为1
  }

  ordered_cache_behavior {
    # 有序行为2
  }

  # ...

  default_cache_behavior {
    viewer_protocol_policy = "redirect-to-https"
    allowed_methods        = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
    cached_methods         = ["GET", "HEAD"]
    target_origin_id       = var.cdn_proxy_origin_id

    forwarded_values {
      query_string = true
      cookies {
        forward = "all"
      }
    }
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }

  viewer_certificate {
    acm_certificate_arn = aws_acm_certificate.proxy_certificate.arn
    ssl_support_method  = "sni-only"
  }

  aliases = ["${var.proxy_subdomain}.myurl.com"]

  depends_on = [
    aws_acm_certificate_validation.proxy_certificate_validation,
  ]
}

希望这样描述的有序行为数组可以为空。

英文:

In Terraform, I would like to add ordered_cache_behaviors to a CloudFront distribution resource if I want to, or, by default, not add any ordered_cache_behavior. Is it possible to do it?

My current code:

resource "aws_cloudfront_distribution" "proxy_cdn" {
  enabled = true

  price_class = "PriceClass_100"

  origin {
    origin_id   = var.cdn_proxy_origin_id
    domain_name = var.cdn_domain_name

    custom_origin_config {
      origin_protocol_policy = "https-only"
      http_port              = "80"
      https_port             = "443"
      origin_ssl_protocols   = ["TLSv1", "TLSv1.1", "TLSv1.2"]
    }
  }

  # current code:
  ordered_cache_behavior {
    # ordered behavior 1
  }

  ordered_cache_behavior {
    # ordered behavior 2
  }

  # ...

  default_cache_behavior {
    viewer_protocol_policy = "redirect-to-https"
    allowed_methods        = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
    cached_methods         = ["GET", "HEAD"]
    target_origin_id       = var.cdn_proxy_origin_id

    forwarded_values {
      query_string = true
      cookies {
        forward = "all"
      }
    }
  }


  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }

  viewer_certificate {
    acm_certificate_arn = aws_acm_certificate.proxy_certificate.arn
    ssl_support_method  = "sni-only"
  }

  aliases = ["${var.proxy_subdomain}.myurl.com"]

  depends_on = [
    aws_acm_certificate_validation.proxy_certificate_validation,
  ]

}

I would like to include an array of ordered behaviors that can also be empty. Is it possible?

答案1

得分: 4

可以通过使用dynamic [1] 和 for_each [2] 的组合来实现。我建议创建一个变量,不是一个列表,而是一个映射,例如:

variable "ordered_cache_behavior" {
  type = map(object({
    path_pattern           = string
    allowed_methods        = list(string)
    cached_methods         = list(string)
    target_origin_id       = string
    viewer_protocol_policy = string
  }))
  description = "有序缓存行为的映射。"

  default = {
    "ordered_cache_behavior_1" = {
      allowed_methods        = ["GET", "HEAD", "OPTIONS"]
      cached_methods         = ["GET", "HEAD"]
      path_pattern           = "/content/*"
      target_origin_id       = "myS3Origin"
      viewer_protocol_policy = "redirect-to-https"
    }
  }
}

请注意,此变量仅包含必需参数。它可以扩展以使用其他参数(例如,min_ttlmax_ttl等)。然后,在资源本身中(为了可读性而缩短):

resource "aws_cloudfront_distribution" "proxy_cdn" {
  .
  .
  .

  dynamic "ordered_cache_behavior" {
    for_each = var.ordered_cache_behavior
    content {
      path_pattern           = ordered_cache_behavior.value.path_pattern
      allowed_methods        = ordered_cache_behavior.value.allowed_methods
      cached_methods         = ordered_cache_behavior.value.cached_methods
      target_origin_id       = ordered_cache_behavior.value.target_origin_id
      viewer_protocol_policy = ordered_cache_behavior.value.viewer_protocol_policy
    }
  }
  .
  .
  .
}

ordered_cache_behavior的默认值设置为{}将意味着不会创建任何ordered_cache_behavior块。


[1] https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks

[2] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

英文:

It is possible by using a combination of dynamic [1] and for_each [2]. I would suggest creating a variable which is not a list, rather a map, e.g:

variable "ordered_cache_behavior" {
  type = map(object({
    path_pattern           = string
    allowed_methods        = list(string)
    cached_methods         = list(string)
    target_origin_id       = string
    viewer_protocol_policy = string
  }))
  description = "Map of ordered cache behaviors."

  default = {
    "ordered_cache_behavior_1" = {
      allowed_methods        = ["GET", "HEAD", "OPTIONS"]
      cached_methods         = ["GET", "HEAD"]
      path_pattern           = "/content/*"
      target_origin_id       = "myS3Origin"
      viewer_protocol_policy = "redirect-to-https"
    }
  }
}

Note that this variable contains only required arguments. It can be expanded to use other arguments as well (e.g., min_ttl, max_ttl etc.). Then, in the resource itself (shortened for readability):

resource "aws_cloudfront_distribution" "proxy_cdn" {
  .
  .
  .

  dynamic "ordered_cache_behavior" {
    for_each = var.ordered_cache_behavior
    content {
      path_pattern           = ordered_cache_behavior.value.path_pattern
      allowed_methods        = ordered_cache_behavior.value.allowed_methods
      cached_methods         = ordered_cache_behavior.value.cached_methods
      target_origin_id       = ordered_cache_behavior.value.target_origin_id
      viewer_protocol_policy = ordered_cache_behavior.value.viewer_protocol_policy
    }
  }
  .
  .
  .
}

Setting the default value for the ordered_cache_behavior to be equal to {} will mean that no ordered_cache_behavior blocks will be created.


[1] https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks

[2] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

huangapple
  • 本文由 发表于 2023年2月14日 19:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定