Terraform aws_cloudfront_cache_policy 将多个 cookie 传递给 cookies_config

Terraform aws_cloudfront_cache_policy pass multiple cookies to cookies_config

在“cookies_config -> cookies”中,我似乎无法在“项目”列表中传递多个 cookies

这是我的变量:

variable "cache_policy_defaults" {
    type = object({
        name = string
        comment = string
        default_ttl = number
        max_ttl = number
        min_ttl = number
        cookie_behavior = string
        cookies_to_forward = optional(list(string))
        header_behavior = string
        headers_to_forward = optional(list(string))
        query_string_behavior = string
        query_strings_to_forward = optional(list(string))
    }
    )
    default = {
        name = ""
        comment = ""
        default_ttl = 60
        max_ttl = 60
        min_ttl = 60
        cookie_behavior = "none"
        cookies_to_forward = []
        header_behavior = "none"
        headers_to_forward = []
        query_string_behavior = "none"
        query_strings_to_forward = []
    }
}

这是我的本地人:

locals {
    origin_id = "origin_${local.origin_config.domain_name}"
    origin_config = merge(var.origin_defaults, var.origin_settings)
    restrictions = merge(var.restrictions_defaults, var.restrictions_settings)
    default_cache_behavior = merge(var.default_cache_behavior_defaults, var.default_cache_behavior_settings)
    cache_policy = merge(var.cache_policy_defaults, var.cache_policy_settings)
    cache_policy_name = "cache_policy_${var.name}"
}

这是我的 tfvars:

"cache_policy_settings": {
        "min_ttl": 30,
        "max_ttl": 30,
        "default_ttl": 30,
        "cookie_behavior": "whitelist",
        "cookies_to_forward": ["123", "456"]
    }

这是我的 main.tf:

resource "aws_cloudfront_cache_policy" "this" {
  name        = lookup(local.cache_policy, local.cache_policy.name, local.cache_policy_name)
  comment     = local.cache_policy.comment
  default_ttl = local.cache_policy.default_ttl
  max_ttl     = local.cache_policy.max_ttl
  min_ttl     = local.cache_policy.min_ttl

  parameters_in_cache_key_and_forwarded_to_origin {
    cookies_config {
        cookie_behavior = local.cache_policy.cookie_behavior
        dynamic "cookies" {
            for_each = local.cache_policy.cookies_to_forward != null ? local.cache_policy.cookies_to_forward : null
            content {
                items = local.cache_policy.cookies_to_forward
            }
        }
      }
    headers_config {
        header_behavior = local.cache_policy.header_behavior
        dynamic "headers" {
            for_each = local.cache_policy.headers_to_forward != null ? local.cache_policy.headers_to_forward : null
            content {
                items = local.cache_policy.headers_to_forward
            }
        }
    }
    query_strings_config {
        query_string_behavior = local.cache_policy.query_string_behavior
        dynamic "query_strings" {
            for_each = local.cache_policy.query_strings_to_forward != null ? local.cache_policy.query_strings_to_forward : null
            content {
                items = local.cache_policy.query_strings_to_forward
            }
        }
    }
  }
}

文档状态

items: (Required) A list of item names (cookies, headers, or query strings). https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_cache_policy#items

但是,列表不接受多项。

Error: Too many list items

  on main.tf line 57, in resource "aws_cloudfront_cache_policy" "this":
  57:     cookies_config {

Attribute supports 1 item maximum, but config has 2 declared.

看来我应该只能传递一个项目列表?如果我将我的输入列表更改为仅包含一个值,那么它将起作用。

技巧如下:

for_each = local.cache_policy.cookies_to_forward != null ? [1] : null

这告诉 Terraform 通过使三元的真值 [1].

来创建一个块

感谢杰森让我走上正轨。