-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.tf
120 lines (96 loc) · 2.98 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = ">= 4.34.0"
}
}
}
provider "google" {
project = var.project
region = var.region
}
variable "services" {
type = list(string)
default = [
"run.googleapis.com",
"cloudfunctions.googleapis.com",
"iam.googleapis.com",
"cloudbuild.googleapis.com",
]
}
resource "google_project_service" "services" {
for_each = toset(var.services)
project = var.project
service = each.value
}
resource "random_id" "default" {
byte_length = 8
}
resource "google_storage_bucket" "default" {
name = "${random_id.default.hex}-gcf-source" # Every bucket name must be globally unique
location = var.region
uniform_bucket_level_access = true
}
data "archive_file" "default" {
type = "zip"
output_path = "/tmp/function-source.zip"
source_dir = "src/"
}
locals {
source_code_hash = filemd5(data.archive_file.default.output_path)
config = yamldecode(file("${path.module}/prod.env.yaml"))
}
resource "google_service_account" "service_account" {
account_id = "${var.name}-sa"
display_name = "Service Account"
}
resource "google_storage_bucket_object" "object" {
name = "function-source-${local.source_code_hash}.zip"
bucket = google_storage_bucket.default.name
source = data.archive_file.default.output_path # Add path to the zipped function source code
}
resource "google_cloudfunctions2_function" "bot" {
depends_on = [
google_storage_bucket_object.object,
google_service_account.service_account,
google_project_service.services,
]
name = var.name
description = var.description
location = var.region
build_config {
runtime = "python312"
entry_point = "handle" # Set the entry point
source {
storage_source {
bucket = google_storage_bucket.default.name
object = google_storage_bucket_object.object.name
}
}
}
service_config {
max_instance_count = 1
available_memory = "256M"
timeout_seconds = 60
ingress_settings = "ALLOW_ALL"
environment_variables = local.config
service_account_email = google_service_account.service_account.email
}
}
resource "google_cloudfunctions2_function_iam_member" "v2invoker" {
depends_on = [google_cloudfunctions2_function.bot]
project = google_cloudfunctions2_function.bot.project
cloud_function = google_cloudfunctions2_function.bot.name
location = google_cloudfunctions2_function.bot.location
role = "roles/cloudfunctions.invoker"
member = "allUsers"
}
resource "google_cloudfunctions2_function_iam_binding" "binding" {
depends_on = [google_cloudfunctions2_function.bot]
project = google_cloudfunctions2_function.bot.project
location = google_cloudfunctions2_function.bot.location
cloud_function = google_cloudfunctions2_function.bot.name
role = "roles/cloudfunctions.invoker"
members = ["allUsers"]
}