I would like to see if it's possible to manipulate each resource in a for_each without creating more resources by the reference of the resource.
For example, I'm creating 3 AWS S3 buckets, in my S3 module I have this resource:
resource "aws_s3_bucket" "s3-bucket" {
for_each = var.bucket_details
bucket = each.value.bucket
acl = each.value.acl
policy = each.value.policy
}
And this is my .tfvars:
bucket_details = {
bucket_1 = {
bucket = "stg-kikeman-bkt-1"
acl = "private"
policy = <<EOF
{ ... <= THIS IS A JSON S3 POLICY
}
EOF
},
bucket_2 = { ... }
bucket_3 = { ... }
After an apply, it suppose to create 3 buckets with the values of the bucket name, acl and its policy.
But I'm stuck with the error:
on modules/s3/s3.tf line 15, in resource "aws_s3_bucket" "s3-bucket":
│ 15: policy = each.value.policy
│ ├────────────────
│ │ each.value is map of string with 3 elements
│
│ This map does not have an element with the key "policy"
It looks like the value for the key policy which is a json policy is not being recognized as a "value", I've tried using the jsonencode({the_json_policy}) but I got the error of
Functions may not be called here
I'm looking on how can I use the json policy as a "value" form a map, if it's possible of course.