diff --git a/modules/cloud-run-v2/job.tf b/modules/cloud-run-v2/job.tf index cc97a4315e..74b170408b 100644 --- a/modules/cloud-run-v2/job.tf +++ b/modules/cloud-run-v2/job.tf @@ -87,7 +87,15 @@ resource "google_cloud_run_v2_job" "job" { } } dynamic "volume_mounts" { - for_each = coalesce(containers.value.volume_mounts, tomap({})) + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k != "cloudsql" } + content { + name = volume_mounts.key + mount_path = volume_mounts.value + } + } + # CloudSQL is the last mount in the list returned by API + dynamic "volume_mounts" { + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k == "cloudsql" } content { name = volume_mounts.key mount_path = volume_mounts.value @@ -96,7 +104,7 @@ resource "google_cloud_run_v2_job" "job" { } } dynamic "volumes" { - for_each = var.volumes + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances == null } content { name = volumes.key dynamic "secret" { @@ -114,12 +122,7 @@ resource "google_cloud_run_v2_job" "job" { } } } - dynamic "cloud_sql_instance" { - for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] - content { - instances = volumes.value.cloud_sql_instances - } - } + dynamic "empty_dir" { for_each = volumes.value.empty_dir_size == null ? [] : [""] content { @@ -144,6 +147,19 @@ resource "google_cloud_run_v2_job" "job" { } } } + # CloudSQL is the last volume in the list returned by API + dynamic "volumes" { + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances != null } + content { + name = volumes.key + dynamic "cloud_sql_instance" { + for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] + content { + instances = volumes.value.cloud_sql_instances + } + } + } + } } } @@ -162,4 +178,3 @@ resource "google_cloud_run_v2_job_iam_binding" "binding" { role = each.key members = each.value } - diff --git a/modules/cloud-run-v2/service.tf b/modules/cloud-run-v2/service.tf index 0906f24a55..0e35be1707 100644 --- a/modules/cloud-run-v2/service.tf +++ b/modules/cloud-run-v2/service.tf @@ -101,7 +101,15 @@ resource "google_cloud_run_v2_service" "service" { } } dynamic "volume_mounts" { - for_each = coalesce(containers.value.volume_mounts, tomap({})) + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k != "cloudsql" } + content { + name = volume_mounts.key + mount_path = volume_mounts.value + } + } + # CloudSQL is the last mount in the list returned by API + dynamic "volume_mounts" { + for_each = { for k, v in coalesce(containers.value.volume_mounts, tomap({})) : k => v if k == "cloudsql" } content { name = volume_mounts.key mount_path = volume_mounts.value @@ -174,7 +182,7 @@ resource "google_cloud_run_v2_service" "service" { } } dynamic "volumes" { - for_each = var.volumes + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances == null } content { name = volumes.key dynamic "secret" { @@ -192,12 +200,7 @@ resource "google_cloud_run_v2_service" "service" { } } } - dynamic "cloud_sql_instance" { - for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] - content { - instances = volumes.value.cloud_sql_instances - } - } + dynamic "empty_dir" { for_each = volumes.value.empty_dir_size == null ? [] : [""] content { @@ -222,6 +225,19 @@ resource "google_cloud_run_v2_service" "service" { } } } + # CloudSQL is the last volume in the list returned by API + dynamic "volumes" { + for_each = { for k, v in var.volumes : k => v if v.cloud_sql_instances != null } + content { + name = volumes.key + dynamic "cloud_sql_instance" { + for_each = length(coalesce(volumes.value.cloud_sql_instances, [])) == 0 ? [] : [""] + content { + instances = volumes.value.cloud_sql_instances + } + } + } + } } deletion_protection = var.deletion_protection