小编bha*_*chi的帖子

无法组合“count”和“for_each”

这是我的代码源

\n
resource "aws_s3_bucket_object" "object" {\n  count               = var.s3_create[1] ? 1 : 0 \n  depends_on          = [aws_s3_bucket.bucket_backup]\n  for_each            = local.buckets_and_folders \n    bucket            = each.value.bucket_backup\n    key               = format("%s/", each.value.folder)\n  force_destroy       = true\n} \n
Run Code Online (Sandbox Code Playgroud)\n

换句话说,我正在尝试创建aws_s3_bucket_object依赖于变量的对象s3_create对象...如果为真则创建,否则不创建。

\n

问题:我无法在创建 terraform 资源时使用以下语法的组合,我正在了解:

\n
Error: Invalid combination of "count" and "for_each"\n\xe2\x94\x82\n\xe2\x94\x82   on ..\\s3\\resources.tf line 51, in resource "aws_s3_bucket_object" "object":\n\xe2\x94\x82   51:   for_each            = local.buckets_and_folders\n\xe2\x94\x82\n\xe2\x94\x82 The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number …
Run Code Online (Sandbox Code Playgroud)

terraform terraform-provider-aws

7
推荐指数
1
解决办法
2万
查看次数

Terraform 使用带有标签的 count.index

使用 terraform,我尝试使用 count.index 将计数包含在我的资源标签中,但出现此错误:

\n
\n

错误:资源“aws_subnet”“prod_sn”中 ..\\modules\\sn\\ressources.tf 第 16 行上的属性值类型\n\xe2\x94\x82\n\xe2\x94\x82 不正确:\n \xe2\x94\x82 16: 标签 = var.sn_tags[count.index]\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2 \x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x82 \xe2\x94\x82 count.index 是一个数字,已知仅在 apply\n\xe2\x94\x82 \xe2\x94\x82 var.sn_tags 是字符串列表,仅在 apply\n\xe2\x94\x82\n\xe2\x94\x82 后才知道 属性值不合适“tags”:所需字符串的映射。

\n
\n

变量.tf

\n
variable "sn_tags" {\n  type        = list (string)\n  default     = ["aa", "bb"]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

资源.tf

\n
resource "aws_subnet" "prod_sn" {\n  count                   = length(var.sn_cidr)\n  vpc_id                  = var.vpc_id\n  cidr_block              = var.sn_cidr[count.index]\n  availability_zone       = data.aws_availability_zones.azs.names[count.index]\n  tags                    = var.sn_tags[count.index] \n}\n
Run Code Online (Sandbox Code Playgroud)\n

主.tf

\n
# Create Public Subnet on availability_zone "3a"\nmodule "publicSn-a" {\n …
Run Code Online (Sandbox Code Playgroud)

terraform terraform-provider-aws

5
推荐指数
1
解决办法
5157
查看次数

标签 统计

terraform ×2

terraform-provider-aws ×2