-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.tf
40 lines (32 loc) · 1.4 KB
/
function.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# The Cloud Function
resource "google_cloudfunctions_function" "function" {
name = "${local.function_name}-${random_string.bucket_suffix.result}"
description = "image processing function"
runtime = "python310"
region = var.region
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.source.name
source_archive_object = google_storage_bucket_object.archive.name
trigger_http = true
entry_point = "detect_image"
service_account_email = google_service_account.images_worker.email
depends_on = [google_project_service.cloudfunctions]
}
# A dedicated Cloud Storage bucket to store the zip source
resource "google_storage_bucket" "source" {
name = "${var.project}-source-${random_string.bucket_suffix.result}"
}
# Create a fresh archive of the current function folder
data "archive_file" "function" {
type = "zip"
output_path = "temp/function_code_${timestamp()}.zip"
source_dir = local.function_folder
}
# The archive in Cloud Stoage uses the md5 of the zip file
# This ensures the Function is redeployed only when the source is changed.
resource "google_storage_bucket_object" "archive" {
name = "${local.function_folder}_${data.archive_file.function.output_md5}.zip" # will delete old items
bucket = google_storage_bucket.source.name
source = data.archive_file.function.output_path
depends_on = [data.archive_file.function]
}