-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
52 lines (44 loc) · 1.65 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
locals {
timestamp = formatdate("YYMMDDhhmmss", timestamp())
root_dir = abspath("../")
}
# Create bucket that will host the source code
resource "google_storage_bucket" "function_bucket" {
name = "${var.project}-function"
location = var.bucket_location
}
# Compress source code
data "archive_file" "source" {
type = "zip"
source_dir = var.filename
output_path = "/tmp/function-${local.timestamp}.zip"
}
# Add source code zip to bucket
resource "google_storage_bucket_object" "zip" {
# Append file MD5 to force bucket to be recreated
name = "source.zip#${data.archive_file.source.output_md5}"
bucket = google_storage_bucket.function_bucket.name
source = data.archive_file.source.output_path
}
resource "google_service_account" "service_account" {
account_id = "cloud-function-invoker"
display_name = "Cloud Function Tutorial Invoker Service Account"
}
# Create IAM entry so all users can invoke the function
resource "google_cloudfunctions_function_iam_member" "invoker" {
project = google_cloudfunctions_function.function.project
region = google_cloudfunctions_function.function.region
cloud_function = google_cloudfunctions_function.function.name
role = "roles/cloudfunctions.invoker"
member = "allUsers"
}
# Create Cloud Function
resource "google_cloudfunctions_function" "function" {
name = var.function_name
runtime = var.runtime
available_memory_mb = var.available_memory_mb
source_archive_bucket = google_storage_bucket.function_bucket.name
source_archive_object = google_storage_bucket_object.zip.name
trigger_http = true
entry_point = var.function_entry_point
}