Given this structure in a variable in Terraform:
https_listener_rules = [
{
https_listener_index = 0
priority = 1
actions = [{
type = "forward"
target_group_index = 1
}]
conditions = [{
host_headers = ["foo.example.com"]
}]
},
{
https_listener_index = 0
priority = 2
actions = [{
type = "forward"
target_group_index = 0
}]
conditions = [{
host_headers = ["bar.example.com"]
}]
}
]
I would like to update each of the listener rules' conditions
list with an additional map.
This is where my current effort sits. I'm not sure how to get it to return the whole value of the original var.https_listener_rules
to local.https_listener_rules
with the new condition inserted.
https_listener_rules = var.create_secret_cloudfront_header ? flatten([for r in var.https_listener_rules : [concat(r.conditions, [{
http_headers = [
{
http_header_name = "X-Origin-Secret"
values = [random_string.origin_secret[0].result]
}
]
}])]]) : var.https_listener_rules
Right now it just returns a list with 4 maps (the conditions only) rather than the list of two listeners each with two conditions.
18: https_listener_rules = var.create_secret_cloudfront_header ? flatten([for r in var.https_listener_rules : [concat(r.conditions, [{
│ 19: http_headers = [
│ 20: {
│ 21: http_header_name = "X-Origin-Secret"
│ 22: values = [random_string.origin_secret[0].result]
│ 23: }
│ 24: ]
│ 25: }])]]) : var.https_listener_rules
│ ├────────────────
│ │ random_string.origin_secret[0].result is "[snip]"
│ │ var.create_secret_cloudfront_header is true
│ │ var.https_listener_rules is tuple with 2 elements
│
│ The true and false result expressions must have consistent types. The 'true' tuple has length 4, but the 'false' tuple has length 2.
EDIT:
Expected result:
https_listener_rules = [
{
https_listener_index = 0
priority = 1
actions = [{
type = "forward"
target_group_index = 1
}]
conditions = [{
host_headers = ["foo.example.com"]
},{
http_headers = [
{
http_header_name = "X-Origin-Secret"
values = ["snip"]
}
]
}]
},
{
https_listener_index = 0
priority = 2
actions = [{
type = "forward"
target_group_index = 0
}]
conditions = [{
host_headers = ["bar.example.com"]
},{
http_headers = [
{
http_header_name = "X-Origin-Secret"
values = ["snip"]
}
]
}]
}
]
Actual result:
[
{
"host_headers" = [
"foo.example.com",
]
},
{
"http_headers" = [
{
"http_header_name" = "X-Origin-Secret"
"values" = [
"snip",
]
},
]
},
{
"host_headers" = [
"bar.example.com",
]
},
{
"http_headers" = [
{
"http_header_name" = "X-Origin-Secret"
"values" = [
"snip",
]
},
]
},
]