Skip to content

Commit c8dda71

Browse files
authored
fix: Secrets when project_id is known after apply (#95)
1 parent 6e3f2d6 commit c8dda71

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ resource "google_storage_bucket_object" "main" {
8989
}
9090

9191
// todo(bharathkkb): remove workaround after https://github.com/hashicorp/terraform-provider-google/issues/11383
92+
// Also: https://github.com/hashicorp/terraform/issues/28925 (when this functions project is created)
9293
data "google_project" "nums" {
93-
for_each = toset(concat(compact([for item in var.secret_environment_variables : lookup(item, "project_id", "")]), [var.project_id]))
94+
for_each = toset(compact([for item in var.secret_environment_variables : lookup(item, "project_id", "")]))
9495
project_id = each.value
9596
}
9697

98+
data "google_project" "default" {
99+
count = length(var.secret_environment_variables) > 0 ? 1 : 0
100+
project_id = var.project_id
101+
}
102+
97103
resource "google_cloudfunctions_function" "main" {
98104
name = var.name
99105
description = var.description
@@ -124,7 +130,7 @@ resource "google_cloudfunctions_function" "main" {
124130

125131
content {
126132
key = secret_environment_variables.value["key"]
127-
project_id = data.google_project.nums[lookup(secret_environment_variables.value, "project_id", var.project_id)].number
133+
project_id = try(data.google_project.nums[secret_environment_variables.value["project_id"]].number, data.google_project.default[0].number)
128134
secret = secret_environment_variables.value["secret_name"]
129135
version = lookup(secret_environment_variables.value, "version", "latest")
130136
}

0 commit comments

Comments
 (0)