From d28e1576a7c5174d316e54104a2f48ace14bae59 Mon Sep 17 00:00:00 2001 From: Cas Leentfaar Date: Thu, 9 Dec 2021 11:14:57 +0100 Subject: [PATCH 1/5] force deployment every time --- terraform/cloud_function/pubsub_triggered/main.tf | 10 +++------- terraform/cloud_function/scheduled/main.tf | 10 +++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/terraform/cloud_function/pubsub_triggered/main.tf b/terraform/cloud_function/pubsub_triggered/main.tf index a2376ee..ac4eae8 100644 --- a/terraform/cloud_function/pubsub_triggered/main.tf +++ b/terraform/cloud_function/pubsub_triggered/main.tf @@ -8,7 +8,8 @@ resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - name = format("%s%s-%s", var.function_name, var.branch_suffix, random_string.random.result) + labels = { last_deployed_at = timestamp() } + name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id runtime = var.function_runtime service_account_email = var.function_service_account_email @@ -24,13 +25,8 @@ resource "google_cloudfunctions_function" "function" { } } -resource "random_string" "random" { - length = 4 - special = false -} - resource "google_storage_bucket_object" "functioncode" { - name = format("pubsub_function_sources/%s/sourcecode%s.zip", var.function_name, random_string.random.result) + name = format("pubsub_function_sources/%s/sourcecode%s.zip", var.function_name, var.branch_suffix) bucket = var.source_code_bucket_name source = format("%s/%s/%s.zip", var.source_code_root_path, var.function_name, var.function_name) } diff --git a/terraform/cloud_function/scheduled/main.tf b/terraform/cloud_function/scheduled/main.tf index adc0902..b5a99de 100644 --- a/terraform/cloud_function/scheduled/main.tf +++ b/terraform/cloud_function/scheduled/main.tf @@ -8,7 +8,8 @@ resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - name = format("%s%s-%s", var.function_name, var.branch_suffix, random_string.random.result) + labels = { last_deployed_at = timestamp() } + name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id region = var.project_region runtime = var.function_runtime @@ -21,13 +22,8 @@ resource "google_cloudfunctions_function" "function" { vpc_connector_egress_settings = var.function_vpc_connector_egress_settings } -resource "random_string" "random" { - length = 4 - special = false -} - resource "google_storage_bucket_object" "functioncode" { - name = format("http_function_sources/%s/sourcecode%s.zip", var.function_name, random_string.random.result) + name = format("http_function_sources/%s/sourcecode%s.zip", var.function_name, var.branch_suffix) bucket = var.source_code_bucket_name source = "${var.source_code_root_path}/${var.function_name}/${var.function_name}.zip" } From ac1fb438b00be083a369f13188f4a6ffd76889d9 Mon Sep 17 00:00:00 2001 From: Cas Leentfaar Date: Thu, 9 Dec 2021 11:34:42 +0100 Subject: [PATCH 2/5] fixed value --- terraform/cloud_function/pubsub_triggered/main.tf | 2 +- terraform/cloud_function/scheduled/main.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/terraform/cloud_function/pubsub_triggered/main.tf b/terraform/cloud_function/pubsub_triggered/main.tf index ac4eae8..c5726d8 100644 --- a/terraform/cloud_function/pubsub_triggered/main.tf +++ b/terraform/cloud_function/pubsub_triggered/main.tf @@ -8,7 +8,7 @@ resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - labels = { last_deployed_at = timestamp() } + labels = { last_deployed_at = tostring(timestamp()) } name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id runtime = var.function_runtime diff --git a/terraform/cloud_function/scheduled/main.tf b/terraform/cloud_function/scheduled/main.tf index b5a99de..db3c36d 100644 --- a/terraform/cloud_function/scheduled/main.tf +++ b/terraform/cloud_function/scheduled/main.tf @@ -8,7 +8,7 @@ resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - labels = { last_deployed_at = timestamp() } + labels = { last_deployed_at = tostring(timestamp()) } name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id region = var.project_region From 87826a960551dda1d687af446e28b7c28daffec4 Mon Sep 17 00:00:00 2001 From: Cas Leentfaar Date: Thu, 9 Dec 2021 11:42:10 +0100 Subject: [PATCH 3/5] trying alternatives and fixed cs issues --- Makefile | 2 ++ terraform/cloud_function/pubsub_triggered/main.tf | 10 ++++++---- terraform/cloud_function/scheduled/main.tf | 6 +++--- terraform/cloud_function/scheduled/variables.tf | 10 +++++----- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 7b302f0..8e1e35f 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,11 @@ help: ## Show this help message. cs: ## Checks for code style issues docker-compose run --rm python black --check . + terraform -chdir=terraform fmt -check -recursive . cs-fix: ## Fixes code style issues docker-compose run --rm python black . + terraform -chdir=terraform fmt -recursive . build: ## Builds the image including the setup.py file (run this when you've changed dependencies) docker-compose build python diff --git a/terraform/cloud_function/pubsub_triggered/main.tf b/terraform/cloud_function/pubsub_triggered/main.tf index c5726d8..f0b5215 100644 --- a/terraform/cloud_function/pubsub_triggered/main.tf +++ b/terraform/cloud_function/pubsub_triggered/main.tf @@ -5,10 +5,12 @@ Often used in conjunction with bucket notifications to act on objects created in */ resource "google_cloudfunctions_function" "function" { - available_memory_mb = var.function_memory - entry_point = var.function_entry_point - environment_variables = var.function_env_vars - labels = { last_deployed_at = tostring(timestamp()) } + available_memory_mb = var.function_memory + entry_point = var.function_entry_point + environment_variables = var.function_env_vars + labels = { + last_deployed_at = "${timestamp()}" + } name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id runtime = var.function_runtime diff --git a/terraform/cloud_function/scheduled/main.tf b/terraform/cloud_function/scheduled/main.tf index db3c36d..93f833f 100644 --- a/terraform/cloud_function/scheduled/main.tf +++ b/terraform/cloud_function/scheduled/main.tf @@ -29,7 +29,7 @@ resource "google_storage_bucket_object" "functioncode" { } resource "google_cloud_scheduler_job" "scheduler_job" { - for_each = {for scheduler in var.schedulers: scheduler.name => scheduler} + for_each = { for scheduler in var.schedulers : scheduler.name => scheduler } attempt_deadline = each.value.attempt_deadline != null ? each.value.attempt_deadline : "320s" name = each.value.name @@ -45,10 +45,10 @@ resource "google_cloud_scheduler_job" "scheduler_job" { http_method = each.value.request_method != null ? each.value.request_method : "POST" headers = { - "Content-Type": "application/json" + "Content-Type" : "application/json" } - uri = google_cloudfunctions_function.function.https_trigger_url + uri = google_cloudfunctions_function.function.https_trigger_url oidc_token { service_account_email = var.scheduler_service_account_email diff --git a/terraform/cloud_function/scheduled/variables.tf b/terraform/cloud_function/scheduled/variables.tf index 1eaa2ba..b2b5fee 100644 --- a/terraform/cloud_function/scheduled/variables.tf +++ b/terraform/cloud_function/scheduled/variables.tf @@ -32,11 +32,11 @@ variable "schedulers" { default = [] type = list(object({ attempt_deadline = optional(string) - name = string - schedule = string - request_body = optional(string) - request_method = optional(string) - retry_count = optional(number) + name = string + schedule = string + request_body = optional(string) + request_method = optional(string) + retry_count = optional(number) })) } variable "source_code_bucket_name" {} From 425e49dbdf52915f8aef78961416a23deb0599cc Mon Sep 17 00:00:00 2001 From: Cas Leentfaar Date: Thu, 9 Dec 2021 11:58:33 +0100 Subject: [PATCH 4/5] use local to avoid deviating (thus confusing) timestamps within a function --- .../cloud_function/pubsub_triggered/main.tf | 16 ++++++++++------ terraform/cloud_function/scheduled/main.tf | 9 ++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/terraform/cloud_function/pubsub_triggered/main.tf b/terraform/cloud_function/pubsub_triggered/main.tf index f0b5215..65fd28b 100644 --- a/terraform/cloud_function/pubsub_triggered/main.tf +++ b/terraform/cloud_function/pubsub_triggered/main.tf @@ -3,14 +3,17 @@ Cloud Function triggered by a message published on a PubSub topic. Often used in conjunction with bucket notifications to act on objects created in a bucket. */ +locals { + # Due to a bug in terraform, GC functions are not (re)deployed even on code changes + # In our case we simply force it for every apply using a label with current timestamp as its value + renewable_labels = { last_deployed_at = formatdate("YYYYMMDDhhmmss", timestamp()) } +} resource "google_cloudfunctions_function" "function" { - available_memory_mb = var.function_memory - entry_point = var.function_entry_point - environment_variables = var.function_env_vars - labels = { - last_deployed_at = "${timestamp()}" - } + available_memory_mb = var.function_memory + entry_point = var.function_entry_point + environment_variables = var.function_env_vars + labels = local.renewable_labels name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id runtime = var.function_runtime @@ -30,5 +33,6 @@ resource "google_cloudfunctions_function" "function" { resource "google_storage_bucket_object" "functioncode" { name = format("pubsub_function_sources/%s/sourcecode%s.zip", var.function_name, var.branch_suffix) bucket = var.source_code_bucket_name + labels = local.renewable_labels source = format("%s/%s/%s.zip", var.source_code_root_path, var.function_name, var.function_name) } diff --git a/terraform/cloud_function/scheduled/main.tf b/terraform/cloud_function/scheduled/main.tf index 93f833f..52c0262 100644 --- a/terraform/cloud_function/scheduled/main.tf +++ b/terraform/cloud_function/scheduled/main.tf @@ -4,11 +4,17 @@ Cloud Function invoked by a scheduled job through a HTTP request Often used to do things that need to be executed at regular intervals, like pulling data from external sources or doing aggregations */ +locals { + # Due to a bug in terraform, GC functions are not (re)deployed even on code changes + # In our case we simply force it for every apply using a label with current timestamp as its value + renewable_labels = { last_deployed_at = formatdate("YYYYMMDDhhmmss", timestamp()) } +} + resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - labels = { last_deployed_at = tostring(timestamp()) } + labels = local.renewable_labels name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id region = var.project_region @@ -25,6 +31,7 @@ resource "google_cloudfunctions_function" "function" { resource "google_storage_bucket_object" "functioncode" { name = format("http_function_sources/%s/sourcecode%s.zip", var.function_name, var.branch_suffix) bucket = var.source_code_bucket_name + labels = local.renewable_labels source = "${var.source_code_root_path}/${var.function_name}/${var.function_name}.zip" } From 7f9524b36d189abe7fa486a813a549b876d18a7e Mon Sep 17 00:00:00 2001 From: Cas Leentfaar Date: Thu, 9 Dec 2021 12:12:03 +0100 Subject: [PATCH 5/5] back to single use --- terraform/cloud_function/pubsub_triggered/main.tf | 8 +------- terraform/cloud_function/scheduled/main.tf | 9 +-------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/terraform/cloud_function/pubsub_triggered/main.tf b/terraform/cloud_function/pubsub_triggered/main.tf index 65fd28b..1343b86 100644 --- a/terraform/cloud_function/pubsub_triggered/main.tf +++ b/terraform/cloud_function/pubsub_triggered/main.tf @@ -3,17 +3,11 @@ Cloud Function triggered by a message published on a PubSub topic. Often used in conjunction with bucket notifications to act on objects created in a bucket. */ -locals { - # Due to a bug in terraform, GC functions are not (re)deployed even on code changes - # In our case we simply force it for every apply using a label with current timestamp as its value - renewable_labels = { last_deployed_at = formatdate("YYYYMMDDhhmmss", timestamp()) } -} - resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - labels = local.renewable_labels + labels = { last_deployed_at = formatdate("YYYYMMDDhhmmss", timestamp()) } name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id runtime = var.function_runtime diff --git a/terraform/cloud_function/scheduled/main.tf b/terraform/cloud_function/scheduled/main.tf index 52c0262..5aefb5e 100644 --- a/terraform/cloud_function/scheduled/main.tf +++ b/terraform/cloud_function/scheduled/main.tf @@ -4,17 +4,11 @@ Cloud Function invoked by a scheduled job through a HTTP request Often used to do things that need to be executed at regular intervals, like pulling data from external sources or doing aggregations */ -locals { - # Due to a bug in terraform, GC functions are not (re)deployed even on code changes - # In our case we simply force it for every apply using a label with current timestamp as its value - renewable_labels = { last_deployed_at = formatdate("YYYYMMDDhhmmss", timestamp()) } -} - resource "google_cloudfunctions_function" "function" { available_memory_mb = var.function_memory entry_point = var.function_entry_point environment_variables = var.function_env_vars - labels = local.renewable_labels + labels = { last_deployed_at = formatdate("YYYYMMDDhhmmss", timestamp()) } name = format("%s%s", var.function_name, var.branch_suffix) project = var.project_id region = var.project_region @@ -31,7 +25,6 @@ resource "google_cloudfunctions_function" "function" { resource "google_storage_bucket_object" "functioncode" { name = format("http_function_sources/%s/sourcecode%s.zip", var.function_name, var.branch_suffix) bucket = var.source_code_bucket_name - labels = local.renewable_labels source = "${var.source_code_root_path}/${var.function_name}/${var.function_name}.zip" }