Hello,
I tried to create for_each loop for AWS s3 bucket notification.
I got these errors:
on triggers.tf line 8, in resource "aws_s3_bucket_notification" "aws-lambda-trigger":
8: events = ["${each.value.events}"]
|----------------
| each.value is tuple with 1 element
This value does not have any attributes.
Error: Unsupported attribute
on triggers.tf line 9, in resource "aws_s3_bucket_notification" "aws-lambda-trigger":
9: filter_prefix = "${each.value.filter_prefix}"
|----------------
| each.value is tuple with 1 element
This value does not have any attributes.
Error: Unsupported attribute
on triggers.tf line 10, in resource "aws_s3_bucket_notification" "aws-lambda-trigger":
10: filter_suffix = "${each.value.filter_suffix}"
|----------------
| each.value is tuple with 1 element
This value does not have any attributes.
I did also notice that the bucket line is likely to be wrong - you are setting it to a string, so that could just be a typo in the post (remove the quotes) but even then it might still be wrong. each.key would be the filter_prefix value. I’m guessing you actually want each.value.bucket_name
@stuart-c If I remove this I get this this error, but it looks like loop is working.
Error: Duplicate object key
on triggers.tf line 2, in resource "aws_s3_bucket_notification" "aws-lambda-trigger":
2: for_each = {for item in var.create_s3: "${item.events} ${item.bucket_name}" => item if item.create_s3_trigger}
|----------------
| item.bucket_name is "dev-ras-initialization-dev"
| item.events is "s3:ObjectCreated:*"
Two different items produced the key "s3:ObjectCreated:*
dev-ras-initialization-dev" in this 'for' expression. If duplicates are
expected, use the ellipsis (...) after the value expression to enable grouping
by key.
You need to ensure the key in the map you are producing is unique.
So you need to figure out what combination of the values need combining to achieve that. What that is saying is that using the bucket name and events isn’t sufficient. You would need to choose a different combination.
Alternatively a simpler option would be to change the create_s3 variable from being a list of maps to a map of maps. That way you could set a unique key yourself instead of having to try to programatically produce one.
I wanted to deploy it as soon as possible but now it wants to deploy 32 new stuff, so I have one bucket and it wants to create multiple buckets, policies…