This is my code source
resource "aws_s3_bucket_object" "object" {
count = var.s3_create[1] ? 1 : 0
depends_on = [aws_s3_bucket.bucket_backup]
for_each = local.buckets_and_folders
bucket = each.value.bucket_backup
key = format("%s/", each.value.folder)
force_destroy = true
}
In other words, I'm traying to create object aws_s3_bucket_object
depends on variable s3_create
... create if true else not create.
Issue: I am not able to use the combination of both the below syntax in creating the terraform resource and I'm geeting :
Error: Invalid combination of "count" and "for_each"
│
│ on ..\s3\resources.tf line 51, in resource "aws_s3_bucket_object" "object":
│ 51: for_each = local.buckets_and_folders
│
│ The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created.
Both count and for_each apply to the whole block. Indenting lines underneath a for_each doesn't impact anything but human readability.
Try using the ternary operator with a for_each instead of a count. If the value is false, return an empty set.
resource "aws_s3_bucket_object" "object" {
for_each = var.s3_create[1] ? tomap({local.buckets_and_folders}) : {}
bucket = each.value.bucket_backup
key = format("%s/", each.value.folder)
depends_on = [aws_s3_bucket.bucket_backup]
force_destroy = true
}