首页 > 解决方案 > Terraform aws_cloudfront_cache_policy 将多个 cookie 传递给 cookies_config

问题描述

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

这是我的变量:

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}"
}

这是我的 tfvar:

"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:(必需)项目名称列表(cookie、标题或查询字符串)。 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.

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

标签: amazon-web-servicesterraformterraform-provider-aws

解决方案


这是诀窍:

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

这告诉 Terraform 通过设置 ternary 的真实值来精确创建一个块[1]

感谢杰森让我走上了正轨。


推荐阅读