forked from chainguard-dev/registry-redirect
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathredirect.tf
100 lines (85 loc) · 2.13 KB
/
redirect.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
terraform {
required_providers {
ko = {
source = "chainguard-dev/ko"
version = "0.0.2"
}
google = {
source = "hashicorp/google"
version = "~> 4.36.0"
}
}
}
provider "ko" {
docker_repo = "gcr.io/${var.project}"
}
variable "project" {
type = string
}
provider "google" {
project = var.project
}
// Enable Cloud Run API.
resource "google_project_service" "run" {
service = "run.googleapis.com"
}
// Enable Compute Engine API.
resource "google_project_service" "compute" {
service = "compute.googleapis.com"
}
// The service runs as a minimal service account with no permissions in the project.
resource "google_service_account" "sa" {
account_id = "redirect-sa"
display_name = "Minimal Service Account"
}
resource "ko_image" "redirect" {
importpath = "github.com/chainguard-dev/registry-redirect"
}
resource "google_cloud_run_service" "regions" {
for_each = var.regions
name = each.key
location = each.key
template {
spec {
containers {
image = ko_image.redirect.image_ref
env {
name = "REGION"
value = each.key
}
args = [
"--prefix",
"chainguard",
"--repo",
"chainguard-images",
]
}
service_account_name = google_service_account.sa.email
container_concurrency = 1000
}
}
traffic {
percent = 100
latest_revision = true
}
// This is supposed to prevent permanent "Still modifying..." states.
// See https://github.com/hashicorp/terraform-provider-google/issues/9438
autogenerate_revision_name = true
depends_on = [google_project_service.run]
}
// Output each service URL.
output "urls" {
value = {
for reg in google_cloud_run_service.regions :
reg.name => reg.status[0].url
}
}
// Make each service invokable by all users.
resource "google_cloud_run_service_iam_member" "allUsers" {
for_each = google_cloud_run_service.regions
service = google_cloud_run_service.regions[each.key].name
location = each.key
role = "roles/run.invoker"
member = "allUsers"
depends_on = [google_cloud_run_service.regions]
}