Terraform aws_cloudfront_cache_policy传递多个cookie到cookies_conf



我似乎无法在"item "中传递多个cookie。在"cookies_config ->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:(必选)项名称列表(cookie、报头或查询字符串)。https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_cache_policy项目

但是,列表不接受多个项目。

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]的真值恰好创建一个块。

谢谢Jason告诉我正确的方向。

相关内容

最新更新