gpt4 book ai didi

amazon-web-services - Terraform - 创建多个桶

转载 作者:行者123 更新时间:2023-12-04 02:54:02 24 4
gpt4 key购买 nike

创建存储桶非常简单。

resource "aws_s3_bucket" "henrys_bucket" {
bucket = "${var.s3_bucket_name}"
acl = "private"
force_destroy = "true"
}

最初我以为我可以为 s3_bucket_name 变量创建一个列表,但我收到一个错误:

Error: bucket must be a single value, not a list

-

variable "s3_bucket_name" {
type = "list"
default = ["prod_bucket", "stage-bucket", "qa_bucket"]
}

如何在不重复代码的情况下创建多个存储桶?

最佳答案

您可以使用 count 的组合& element像这样:

variable "s3_bucket_name" {
type = "list"
default = ["prod_bucket", "stage-bucket", "qa_bucket"]
}

resource "aws_s3_bucket" "henrys_bucket" {
count = "${length(var.s3_bucket_name)}"
bucket = "${element(var.s3_bucket_name, count.index)}"
acl = "private"
force_destroy = "true"
}

编辑:按照@ydaetskcoR 的建议,您可以使用list[index] 模式而不是元素。

variable "s3_bucket_name" {
type = "list"
default = ["prod_bucket", "stage-bucket", "qa_bucket"]
}

resource "aws_s3_bucket" "henrys_bucket" {
count = "${length(var.s3_bucket_name)}"
bucket = "${var.s3_bucket_name[count.index]}"
acl = "private"
force_destroy = "true"
}

关于amazon-web-services - Terraform - 创建多个桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53764505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com