Hi all. I’m new here and I am faced with the problem of substitution values in maps.
Let’s presume I have such a variable:
variable "c1" {
default = [
{
"path_pattern" : "/static1/*",
"target_origin_id" : "s3_bucket_one",
"viewer_protocol_policy" : "redirect-to-https",
"allowed_methods" : [
"GET",
"HEAD",
"OPTIONS"
],
"cached_methods" : [
"GET",
"HEAD"
],
"compress" : true,
"query_string" : true,
"cache_policy_id" : "7-days"
},
{
"path_pattern" : "/static2/*",
"target_origin_id" : "s3_bucket_one",
"viewer_protocol_policy" : "redirect-to-https",
"allowed_methods" : [
"GET",
"HEAD",
"OPTIONS"
],
"cached_methods" : [
"GET",
"HEAD"
],
"compress" : true,
"query_string" : true,
"cache_policy_id" : "3-days"
}
]
}
Next, I need to take the value of each cache_policy_id
and get it through the data aws_cloudfront_cache_policy
I’m doing it with the code:
data "aws_cloudfront_cache_policy" "id" {
name = local.c3[0]
}
locals {
c3 = [for i, v in var.c1 : v["cache_policy_id"]]
}
But I presume it wouldn’t work with more than 1 policy in the variable above. (So probably you can assist with this too)
And the main problem is to have a variable with the next values:
variable "c1" {
default = [
{
"path_pattern" : "/static/*",
"target_origin_id" : "s3_bucket_one",
"viewer_protocol_policy" : "redirect-to-https",
"allowed_methods" : [
"GET",
"HEAD",
"OPTIONS"
],
"cached_methods" : [
"GET",
"HEAD"
],
"compress" : true,
"query_string" : true,
"cache_policy_id" : "8765-4322-1234-5678"
},
{
"path_pattern" : "/static2/*",
"target_origin_id" : "s3_bucket_one",
"viewer_protocol_policy" : "redirect-to-https",
"allowed_methods" : [
"GET",
"HEAD",
"OPTIONS"
],
"cached_methods" : [
"GET",
"HEAD"
],
"compress" : true,
"query_string" : true,
"cache_policy_id" : "1234-5678-9012-3456"
}
]
}
So the cache_policy_id
is represented with its id, not the name itself. And all other information should be as it was provided to the variable.
Any ideas on how can it be done?