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

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

问题描述

创建存储桶非常简单.

Creating a bucket is pretty simple.

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?

推荐答案

您可以使用 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天全站免登陆