尝试使用以下提供的代码使用Terraform创建多个AWS s3存储桶。提供商版本:4.5.0
尝试不使用count
功能,同时使用for_each
功能和
resource "aws_s3_bucket" "public_bucket" {
count = "${length(var.public_bucket_names)}"
bucket = "${var.public_bucket_names[count.index]}"
# acceleration_status = var.public_bucket_acceleration
tags = {
ProjectName = "${var.project_name}"
Environment = "${var.env_suffix}"
}
}
resource "aws_s3_bucket_versioning" "public_bucket_versioning" {
bucket = aws_s3_bucket.public_bucket[count.index].id
versioning_configuration {
status = "Enabled"
}
}
面对错误
Error: Reference to "count" in non-counted context
│
│ on modules/S3-Public/s3-public.tf line 24, in resource "aws_s3_bucket_versioning" "public_bucket_versioning":
│ 24: bucket = aws_s3_bucket.public_bucket[count.index].id
│
│ The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set.
您当前的代码创建了多个S3存储桶,但只尝试创建一个存储桶版本控制配置。您正在bucket版本控制资源中引用一个count
变量,但尚未为该资源声明count
属性。
您需要在bucket版本控制资源上声明count
,就像您对s3 bucket资源所做的那样。
resource "aws_s3_bucket_versioning" "public_bucket_versioning" {
count = "${length(var.public_bucket_names)}"
bucket = aws_s3_bucket.public_bucket[count.index].id
versioning_configuration {
status = "Enabled"
}
}