Terraform YAML 配置 - 对象没有属性名称
Terraform YAML config - object does not have an attribute names
我正在努力调试我有一个每个条目包含两个属性的 yaml 文件的问题:
topics:
- name: topic_one
partitions: 24
- name: topic_two
partitions: 1
- name: topic_three
partitions: 12
- name: topic_four
partitions: 6
我正在将其放入 TF 中:
locals {
confluent = yamldecode(file("./confluent.yaml"))["topics"]
confluent_topics = flatten([for topic in local.confluent :
{
"name" = topic.name
"partitions" = topic.partitions
}])
}
通过以下方式传入模块:
module "confluent" {
source = "./modules/confluent-cloud"
count = var.confluent_cluster ? 1 : 0
topics = local.confluent_topics
region = var.region
}
然后在模块中:
resource "confluent_kafka_topic" "kafka" {
for_each = {
for topic in var.topics : topic.name => name
}
kafka_cluster {
id = confluent_kafka_cluster.kafka.id
}
topic_name = each.value.name
partitions_count = each.value.partitions
...
我几乎尝试了每一种组合,并且我已经将模块 variables.tf
中的变量类型设置为几乎所有可以想象到的东西..但错误是它认为对象没有一个名为 partitions
的属性 - 我尝试过简化局部而不展平列表,我尝试了很多不同的变体..但总是这样;
│ Error: Unsupported attribute
│
│ on main.tf line 40, in locals:
│ 40: "partitions" = topic.partitions
│
│ This object does not have an attribute named "partitions".
控制台给我:
> local.confluent
[
{
"name" = "topic_one"
"partitions" = 24
},
{
"name" = "topic_two"
"partitions" = 1
},
{
"name" = "topic_three"
"partitions" = 12
},
{
"name" = "topic_four"
"partitions" = 6
}
扁平化本地控制台 returns:
> local.confluent_topics
(known after apply)
...这有点令人费解,因为我认为它应该在不应用的情况下知道这一点,但是..我相当有信心这不是我的问题。
令人恼火的是我以前做过这个没有问题..所以我想知道这是否是一个 TF 版本的情况,必须以不同的方式处理某些事情?
❯ terraform version
Terraform v1.1.7
on darwin_arm64
+ provider registry.terraform.io/confluentinc/confluent v0.7.0
+ provider registry.terraform.io/hashicorp/aws v4.12.1
+ provider registry.terraform.io/hashicorp/kubernetes v2.11.0
+ provider registry.terraform.io/hashicorp/null v3.1.1
+ provider registry.terraform.io/hashicorp/random v3.1.3
+ provider registry.terraform.io/hashicorp/tls v3.3.0
Your version of Terraform is out of date! The latest version
is 1.1.9. You can update by downloading from https://www.terraform.io/downloads.html
你的逻辑都很好,除了带有属性 name
的循环。您不能拥有 name
属性并从中获取 partitions
字段。它只能从 topic
对象中获取。
试试这个..
resource "confluent_kafka_topic" "kafka" {
for_each = {
for topic in var.topics : topic.name => topic
}
kafka_cluster {
id = confluent_kafka_cluster.kafka.id
}
topic_name = each.value.name
partitions_count = each.value.partitions
...
您拥有 topic.name
形式的唯一键,并且 topic
是具有 partitions
字段的对象。
我正在努力调试我有一个每个条目包含两个属性的 yaml 文件的问题:
topics:
- name: topic_one
partitions: 24
- name: topic_two
partitions: 1
- name: topic_three
partitions: 12
- name: topic_four
partitions: 6
我正在将其放入 TF 中:
locals {
confluent = yamldecode(file("./confluent.yaml"))["topics"]
confluent_topics = flatten([for topic in local.confluent :
{
"name" = topic.name
"partitions" = topic.partitions
}])
}
通过以下方式传入模块:
module "confluent" {
source = "./modules/confluent-cloud"
count = var.confluent_cluster ? 1 : 0
topics = local.confluent_topics
region = var.region
}
然后在模块中:
resource "confluent_kafka_topic" "kafka" {
for_each = {
for topic in var.topics : topic.name => name
}
kafka_cluster {
id = confluent_kafka_cluster.kafka.id
}
topic_name = each.value.name
partitions_count = each.value.partitions
...
我几乎尝试了每一种组合,并且我已经将模块 variables.tf
中的变量类型设置为几乎所有可以想象到的东西..但错误是它认为对象没有一个名为 partitions
的属性 - 我尝试过简化局部而不展平列表,我尝试了很多不同的变体..但总是这样;
│ Error: Unsupported attribute
│
│ on main.tf line 40, in locals:
│ 40: "partitions" = topic.partitions
│
│ This object does not have an attribute named "partitions".
控制台给我:
> local.confluent
[
{
"name" = "topic_one"
"partitions" = 24
},
{
"name" = "topic_two"
"partitions" = 1
},
{
"name" = "topic_three"
"partitions" = 12
},
{
"name" = "topic_four"
"partitions" = 6
}
扁平化本地控制台 returns:
> local.confluent_topics
(known after apply)
...这有点令人费解,因为我认为它应该在不应用的情况下知道这一点,但是..我相当有信心这不是我的问题。
令人恼火的是我以前做过这个没有问题..所以我想知道这是否是一个 TF 版本的情况,必须以不同的方式处理某些事情?
❯ terraform version
Terraform v1.1.7
on darwin_arm64
+ provider registry.terraform.io/confluentinc/confluent v0.7.0
+ provider registry.terraform.io/hashicorp/aws v4.12.1
+ provider registry.terraform.io/hashicorp/kubernetes v2.11.0
+ provider registry.terraform.io/hashicorp/null v3.1.1
+ provider registry.terraform.io/hashicorp/random v3.1.3
+ provider registry.terraform.io/hashicorp/tls v3.3.0
Your version of Terraform is out of date! The latest version
is 1.1.9. You can update by downloading from https://www.terraform.io/downloads.html
你的逻辑都很好,除了带有属性 name
的循环。您不能拥有 name
属性并从中获取 partitions
字段。它只能从 topic
对象中获取。
试试这个..
resource "confluent_kafka_topic" "kafka" {
for_each = {
for topic in var.topics : topic.name => topic
}
kafka_cluster {
id = confluent_kafka_cluster.kafka.id
}
topic_name = each.value.name
partitions_count = each.value.partitions
...
您拥有 topic.name
形式的唯一键,并且 topic
是具有 partitions
字段的对象。