温馨提示:本文翻译自stackoverflow.com,查看原文请点击:amazon web services - how to create multiple folders inside an existing AWS bucket
amazon-s3 amazon-web-services terraform

amazon web services - 如何在现有的AWS存储桶中创建多个文件夹

发布于 2020-04-04 00:02:50

如何使用terraform在现有存储桶中创建多个文件夹。例如:bucket / folder1 / folder2

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

查看更多

提问者
user_01_02
被浏览
84
ThomasVdBerge 2018-04-29 04:33

尽管Nate的答案是正确的,但这将导致大量代码重复。我认为一个更好的解决方案是使用列表并对其进行循环。

创建一个包含可能的文件夹列表的变量(variable.tf文件):

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

然后更改您已有的代码:

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"
}

最新帖子

最新文章