Good day,
Frequently I encounter conditions where I need to create a single resource or block in a resource when a list is not empty.
For dynamic blocks I find it cleaner to use the below:
dynamic "statement" {
for_each = try([var.s3_write_keys[0]], [])
content {
sid = "KMSWrite"
effect = "Allow"
...
For resources I use the below due to the key in the for_each becoming an index for the resource. I assume the try
above could cause inconsistency in the key based on the ordering of the passed list.
data "aws_iam_policy_document" "s3_write" {
for_each = length(var.s3_write_buckets) > 0 ? toset(["s3_write"]) : []
statement {
sid = "S3Write"
effect = "Allow"
...
Is there a better way (code golf ) of doing this for resources?
Thank you.