-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
206 lines (181 loc) · 5.17 KB
/
main.tf
File metadata and controls
206 lines (181 loc) · 5.17 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 7.0"
}
}
}
data "google_service_account" "service_account" {
account_id = var.gsa
project = var.project
}
resource "google_cloud_run_v2_service" "cloudrun" {
for_each = toset(var.regions)
name = var.name
location = each.value
launch_stage = "GA"
project = var.project
deletion_protection = false
scaling {
min_instance_count = var.min_instances
max_instance_count = var.max_instances
}
template {
execution_environment = "EXECUTION_ENVIRONMENT_GEN2"
service_account = data.google_service_account.service_account.email
gpu_zonal_redundancy_disabled = anytrue([for c in var.containers : c.gpus != ""])
dynamic "containers" {
for_each = var.containers
content {
image = containers.value.image
name = containers.value.name
command = containers.value.command
args = containers.value.args
dynamic "ports" {
for_each = containers.value.port != 0 ? [containers.value.port] : []
content {
container_port = ports.value
}
}
dynamic "liveness_probe" {
for_each = containers.value.liveness_probe != "" ? [containers.value.liveness_probe] : []
content {
http_get {
path = liveness_probe.value
}
}
}
dynamic "env" {
for_each = concat(
[
for secret in var.secrets : {
name = secret.name
value_source = {
secret_key_ref = {
secret = secret.secret_name
version = "latest"
}
}
}
],
[
for env_var in var.addl_env_vars : {
name = env_var.name
value = env_var.value
}
]
)
content {
name = env.value.name
value = try(env.value.value, null)
dynamic "value_source" {
for_each = try([env.value.value_source], [])
content {
secret_key_ref {
secret = value_source.value.secret
version = value_source.value.version
}
}
}
}
}
dynamic "volume_mounts" {
for_each = containers.value.volume_mounts
content {
name = volume_mounts.value.name
mount_path = volume_mounts.value.mount_path
}
}
resources {
cpu_idle = containers.value.gpus == ""
limits = merge(
{
memory = containers.value.memory,
cpu = containers.value.cpu
},
containers.value.gpus != "" ? {
"nvidia.com/gpu" = containers.value.gpus
} : {}
)
}
}
}
dynamic "vpc_access" {
for_each = var.vpc_direct_egress == "OFF" ? [] : [true]
content {
egress = var.vpc_direct_egress
network_interfaces {
network = var.vpc_direct_egress_network
subnetwork = var.vpc_direct_egress_subnetwork
tags = var.vpc_direct_egress_tags
}
}
}
dynamic "volumes" {
for_each = var.empty_dir_volumes
content {
name = volumes.value.name
empty_dir {
medium = "MEMORY"
size_limit = volumes.value.size_limit
}
}
}
dynamic "volumes" {
for_each = var.gcs_volumes
content {
name = volumes.value.name
gcs {
bucket = volumes.value.bucket
read_only = volumes.value.read_only
}
}
}
}
}
resource "google_cloud_run_v2_service_iam_member" "invoker" {
for_each = {
for pair in setproduct(var.regions, var.invokers) :
"${pair[0]}-${pair[1]}" => {
region = pair[0]
member = pair[1]
}
}
location = each.value.region
name = google_cloud_run_v2_service.cloudrun[each.value.region].name
project = var.project
role = "roles/run.invoker"
member = each.value.member
}
# create a serverless NEG for this set of regional services
resource "google_compute_region_network_endpoint_group" "neg" {
for_each = var.skipNeg ? toset([]) : toset(var.regions)
name = "libops-neg-${var.name}-${each.value}"
network_endpoint_type = "SERVERLESS"
region = each.value
project = var.project
cloud_run {
service = google_cloud_run_v2_service.cloudrun[each.value].name
}
lifecycle {
create_before_destroy = true
}
}
resource "google_compute_backend_service" "backend" {
count = var.skipNeg ? 0 : 1
project = var.project
name = "libops-backend-${var.name}"
load_balancing_scheme = "EXTERNAL_MANAGED"
protocol = "HTTPS"
dynamic "backend" {
for_each = google_compute_region_network_endpoint_group.neg
content {
group = backend.value.id
}
}
log_config {
enable = true
sample_rate = 1.0
}
}