Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a folder in an amazon S3 bucket using terraform

I was able to create a bucket in an amazon S3 using this link.

I used the following code to create a bucket :

resource "aws_s3_bucket" "b" {
    bucket = "my_tf_test_bucket"
    acl    = "private"
}

Now I wanted to create folders inside the bucket, say Folder1.

I found the link for creating an S3 object. But this has a mandatory parameter source. I am not sure what this value have to , since my intent is to create a folder inside the S3 bucket.

like image 887
cmm user Avatar asked May 27 '16 20:05

cmm user


People also ask

Can we create S3 bucket using Terraform?

Once you have all the tools and utilities installed, the next step is to create a Terraform configuration to provision an S3 bucket on AWS. Navigate into the directory and create a Terraform configuration. Open the file and add the following configuration to create an S3 bucket using your favorite text editor.

Can you have folders in S3?

You can have folders within folders, but not buckets within buckets. You can upload and copy objects directly into a folder.


3 Answers

For running terraform on Mac or Linux, the following will do what you want

resource "aws_s3_bucket_object" "folder1" {
    bucket = "${aws_s3_bucket.b.id}"
    acl    = "private"
    key    = "Folder1/"
    source = "/dev/null"
}

If you're on windows you can use an empty file.

While folks will be pedantic about s3 not having folders, there are a number of operations where having an object placeholder for a key prefix (otherwise called a folder) make life easier. Like s3 sync for example.

like image 125
PaulR Avatar answered Oct 24 '22 12:10

PaulR


Actually, there is a canonical way to create it, without being OS dependent, by inspecting the Network on a UI put you see the content headers, as stated by : https://stackoverflow.com/users/1554386/alastair-mccormack ,

And S3 does support folders these days as visible from the UI.

So this is how you can achieve it:

resource "aws_s3_bucket_object" "base_folder" {
    bucket  = "${aws_s3_bucket.default.id}"
    acl     = "private"
    key     =  "${var.named_folder}/"
    content_type = "application/x-directory"
    kms_key_id = "key_arn_if_used"
}

Please notice the trailing slash otherwise it creates an empty file

Above has been used with a Windows OS to successfully create a folder using terraform s3_bucket_object.

like image 42
Ilhicas Avatar answered Oct 24 '22 11:10

Ilhicas


S3 doesn't support folders. Objects can have prefix names with slashes that look like folders, but that's just part of the object name. So there's no way to create a folder in terraform or anything else, because there's no such thing as a folder in S3.

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html http://docs.aws.amazon.com/AWSImportExport/latest/DG/ManipulatingS3KeyNames.html

If you want to pretend, you could create a zero-byte object in the bucket named "Folder1/" but that's not required. You can just create objects with key names like "Folder1/File1" and it will work.

like image 32
Karen B Avatar answered Oct 24 '22 11:10

Karen B