Home > Net >  Why does terraform create an empty directory in the s3 bucket?
Why does terraform create an empty directory in the s3 bucket?

Time:12-14

I have a Terraform set up that creates an s3 bucket and sub directories in the bucket. For some reason the bucket is created with an empty / and in each directory.

s3_bucket:
-/
-folder1:-/
         -some file
-folder2:-/

And so on for every directory, there is an empty /. This causes issues in my next few steps, why is it created this way? how can I create the s3 and folders without the empty / ?

resource "aws_s3_bucket_object" "object" {
  bucket = "${aws_s3_bucket.s3_ob.id}"
  key    = "folder1/"
  source = "dummy.txt"
}

resource "aws_s3_bucket_object" "object2" {
  bucket = "${aws_s3_bucket.s3_ob.id}"
  key    = "/folder2/"
  source = "dummy.txt"
}

CodePudding user response:

This happens because your keys end with a /. What your code does is to write contents of dummy.txt to the keys at folder1/ and folder2/. S3 is a key value objects storage and not a file system. Concept of directories is just something we bring on logically by splitting the keys with slash characters / and calling every step a directory, so in this case yes, a folder1 and folder2 has been created and since there's nothing after the slash the file name is practically empty.

I assume what you want to do is to just give your files some names in the traditional file system sense, as in don't end your key names with a /.

resource "aws_s3_bucket_object" "object" {
  bucket = "${aws_s3_bucket.s3_ob.id}"
  key    = "folder1/dummy.txt"
  source = "dummy.txt"
}

resource "aws_s3_bucket_object" "object2" {
  bucket = "${aws_s3_bucket.s3_ob.id}"
  key    = "/folder2/dummy.txt"
  source = "dummy.txt"
}
  • Related