我在 GCP 中有一个用于填充 kubernetes 机密的服务帐户模块
这是我的模块
resource "google_service_account" "service_account" {
count = var.enabled ? 1 : 0
account_id = var.account_id
display_name = var.display_name
}
resource "google_project_iam_member" "service_account_roles" {
count = var.enabled ? length(var.roles) : 0
role = "roles/${element(var.roles, count.index)}"
member = "serviceAccount:${google_service_account.service_account[0].email}"
}
resource "google_service_account_key" "service_account_key" {
count = var.enabled ? 1 : 0
service_account_id = google_service_account.service_account[0].name
}
'output.tf' 包含以下内容
output "private_decoded_key" {
value = base64decode(
element(
concat(
google_service_account_key.service_account_key.*.private_key,
[""],
),
0,
),
)
description = "The base 64 decoded version of the credentials"
}
由于有一个条件是这些资源都是在没有enabled
标志的情况下创建的,所以我不得不在 TF 0.11.14 中这样处理它,而 tf0.12 自动升级工具在这里没有做太多的更改。
如何在 Terraform 0.12.24 中简化这一点,我尝试将输出修改为简单
value = base64decode(google_service_account_key.service_account_key[0].private_key)
但问题是,如果在删除过程中对应的kubernetes集群被删除,中途因为terraform出现错误,我将无法使用`terraform destroy'清理其余资源的terraform状态
尝试将其转换count
为for_each
如下所示给了我以下错误
resource "google_service_account" "service_account" {
# count = var.enabled ? 1 : 0
for_each = var.enabled ? 1 : 0
account_id = var.account_id
display_name = var.display_name
}
resource "google_project_iam_member" "service_account_roles" {
# count = var.enabled ? length(var.roles) : 0
for_each = var.enabled ? toset(var.roles) : 0
# role = "roles/${element(var.roles, count.index)}"
role = "roles/${each.value}"
member = "serviceAccount:${google_service_account.service_account[0].email}"
}
for_each = var.enabled ? toset(var.roles) : 0
The true and false result expressions must have consistent types. The given
expressions are set of dynamic and number, respectively.
我在上面做错了什么?