user_01_02
user_01_02

Reputation: 763

how to create multiple folders inside an existing AWS bucket

How to create a multiple folders inside an existing bucket using terraform. example: bucket/folder1/folder2

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

Upvotes: 3

Views: 6912

Answers (3)

Michael Olafisoye
Michael Olafisoye

Reputation: 71

There a no tips for windows users but this should work for you.

Slightly easier than using an empty file as "source"

resource "aws_s3_bucket_object" "output_subdir" {
  bucket = "${aws_s3_bucket.file_bucket.id}"
  key    = "output/"
  content_type = "application/x-directory"
}

resource "aws_s3_bucket_object" "input_subdir" {
  bucket = "${aws_s3_bucket.file_bucket.id}"
  key    = "input/"
  content_type = "application/x-directory"
}

Upvotes: 0

ThomasVdBerge
ThomasVdBerge

Reputation: 8140

While the answer of Nate is correct, this would lead to a lot of code duplication. A better solution in my opinion would be to work with a list and loop over it.

Create a variable (variable.tf file) that contains a list of possible folders:

variable "s3_folders" {
  type        = "list"
  description = "The list of S3 folders to create"
  default     = ["folder1", "folder2", "folder3"]
}

Then alter the piece of code you already have:

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

Upvotes: 12

Nathan Smith
Nathan Smith

Reputation: 8347

Apply the same logic as you did to create the first directory.

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

Upvotes: 1

Related Questions