skip to Main Content

I have a tf code to create folders via aws_s3_object resource and am trying to modify the current code:

variable "s3_bucket_folders" {
type = set(string)
default = []
}


resource "aws_s3_object" "folders" {
for_each = var.s3_bucket_folders
bucket = aws_s3_bucket.bucket.id
acl = "private"
key = each.key
}

I also want the bucket to be using a for_each so that in every bucket I specify the folders mentioned should be created. I tried using a map with dynamic but am running into errors, can anyone specify the best solution for this

Trying to find the best possible solution to create all these folders withing buckets from one resource block

2

Answers


  1. resource "aws_s3_bucket_object" "folders" {
        count   = "${length(var.s3_bucket_folders)}"
        bucket = "${aws_s3_bucket.b.id}"
        acl    = "private"
        key    = "${var.s3_bucket_folders[count.index]}/"
        source = "/dev/null"
    }

    use count instead of for_each

    Login or Signup to reply.
  2. Transform your list to a set for the foreach to iterate over

    resource "aws_s3_object" "folders" {
        for_each = toset(var.s3_bucket_folders)
        bucket = aws_s3_bucket.bucket.id
        acl = "private"
        key = each.key
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search