[英]Reference resource from variable in terraform
I want to create an object through which I will iterate using for_each
to create some:我想创建一个 object 通过它我将使用for_each
进行迭代以创建一些:
google_service_account
google_project_iam_member
resources资源
So my object is more or less like this所以我的 object 或多或少是这样的
service_accounts = {
"gha_storage_admin_sa" = {
create = true
project = var.project_id
account_id = "id1"
display_name = "GHA service account 1"
role = "roles/storage.admin"
},
"gha_cluster_scaling_sa" = {
create = false
project = var.project_id
account_id = "id2"
display_name = "GHA service account 2"
role = google_organization_iam_custom_role.my_custom_role.id
},
}
resource "google_service_account" "service_account" {
for_each = {
for k, v in local.service_accounts: k => v
if v.create
}
project = each.value.project
account_id = each.value.account_id
display_name = each.value.display_name
}
resource "google_project_iam_member" "member" {
for_each = local.service_accounts
project = var.project_id
role = each.value.role
member = "serviceAccount:${google_service_account.service_account[each.key].email}"
}
This works fine if the above is a local
variable.如果上面是local
变量,这很好用。
I want however to expose it as a regular variable
.但是,我想将其公开为常规variable
。
My question is whether the referenced resource ( google_organization_iam_custom_role.my_custom_role.id
) in the second element can be somehow exposed as a variable.我的问题是第二个元素中的引用资源( google_organization_iam_custom_role.my_custom_role.id
)是否可以以某种方式公开为变量。
My question is whether the referenced resource (google_organization_iam_custom_role.my_custom_role.id) in the second element can be somehow exposed as a variable.我的问题是第二个元素中的引用资源(google_organization_iam_custom_role.my_custom_role.id)是否可以以某种方式公开为变量。
Sadly its not possible .可悲的是它不可能。 TF does not support dynamic variables. TF 不支持动态变量。 All variables' values must be know at plan time.在计划时必须知道所有变量的值。 You have to pass the actual value of google_organization_iam_custom_role.my_custom_role.id
as input variable to your code.您必须将google_organization_iam_custom_role.my_custom_role.id
的实际值作为输入变量传递给您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.