Terraform - 创建多个存储桶 [英] Terraform - creating multiple buckets

查看:25
本文介绍了Terraform - 创建多个存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建存储桶非常简单.

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

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

Initially I thought I could create a list for the s3_bucket_name variable but I get an error:

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

-

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

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

How can I create multiple buckets without duplicating code?

推荐答案

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

You can use a combination of count & element like so:

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] 模式而不是元素.

as suggested by @ydaetskcoR you can use the list[index] pattern rather than 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        = "${var.s3_bucket_name[count.index]}"
  acl           = "private"
  force_destroy = "true"
}

这篇关于Terraform - 创建多个存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆