使用Terraform在多个S3存储桶中创建多个文件夹



我希望创建一个S3地形模块,它可以在所有这些存储桶中创建存储桶名称和文件夹名称的列表。例如,在我的S3模块main.tf中。我有

resource "aws_s3_bucket_object" "folders" {
count = var.create_folders ? length(var.s3_folder_names) : 0
bucket  = element(aws_s3_bucket.s3bucket.*.id, count.index)
acl    = "private"
key    = format("%s/",var.s3_folder_names[count.index])
source = "/dev/null"
}

我调用这个模块,如下所示:

variable "s3_bucket_name" {
type = list
description = "List of S3 bucket names"
default = ["bucket1","bucket-2"]
}
variable "s3_folder_names" {
type        = list
description = "The list of S3 folders to be created inside S3 bucket"
default=["folder1/dev","folder2/qa"]
}
module "s3" {
source = "../../../gce-nextgen-cloud-terraform-modules/modules/s3"
create_folders = true
s3_folder_names = var.s3_folder_names
environment = var.environment
s3_bucket_name = var.s3_bucket_name
force_destroy = true
bucket_replication_enabled = true
tags = local.common_tags
providers = {
aws.main_region = aws.main_region
aws.secondary_region = aws.secondary_region
}
}

我面临的问题是,计数变量只能在资源块中设置一次。以下是引发问题的场景:

  1. 如果CCD_ 1。然后我将无法访问S3 bucket列表的所有元素,如下所示
resource "aws_s3_bucket_object" "folders" {
count = var.create_folders ? length(var.s3_folder_names) : 0
**bucket  = element(aws_s3_bucket.s3bucket.*.id, count.index)**
acl    = "private"
key    = format("%s/",var.s3_folder_names[count.index])
source = "/dev/null"
}

因此,正因为如此,我将无法在所有的bucket中创建这些文件夹。唯一的目标是在所有这些bucket中创建相同的文件夹结构集。如有任何帮助,我们将不胜感激。提前感谢!

您可以创建组合数据结构,例如:

locals {
buckets_and_folders = merge([
for bucket in var.s3_bucket_name:
{
for folder in var.s3_folder_names:
"${bucket}-${folder}" => {
bucket = bucket
folder = folder
}
}
]...)
}

然后使用for_each:迭代该结构

resource "aws_s3_bucket_object" "folders" {
for_each = var.create_folders ? local.buckets_and_folders : {}
bucket  = each.value.bucket
acl    = "private"
key    = format("%s/", each.value.folder)
source = "/dev/null"
}

相关内容

  • 没有找到相关文章