Skip to content

Commit 24bd57f

Browse files
committed
feat: Added deploy API to cloud run
1 parent 01ef5f4 commit 24bd57f

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

terraform/main.tf

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
resource "google_sql_database_instance" "main" {
1+
# Cloud SQL
2+
3+
resource "google_sql_database_instance" "main" { # create Cloud SQL instance
24
name = "${var.environment}-postgres"
35
database_version = "POSTGRES_15"
46
region = var.region
@@ -28,4 +30,60 @@ resource "google_sql_user" "user" { # db user creation
2830
name = var.db_user
2931
instance = google_sql_database_instance.main.name
3032
password = var.db_password
33+
}
34+
35+
# Cloud Run
36+
37+
data "google_iam_policy" "event-access-noauth" { # Create public access
38+
binding {
39+
role = "roles/run.invoker"
40+
members = [
41+
"allUsers",
42+
]
43+
}
44+
}
45+
46+
# event access API
47+
resource "google_cloud_run_service" "event-access-cloud-run" { # deploy image to Cloud Run
48+
name = "${var.environment}-${var.service_name}"
49+
location = var.region
50+
template {
51+
spec {
52+
containers {
53+
image = var.image_uri
54+
55+
env {
56+
name = "DB_HOST"
57+
value = google_sql_database_instance.main.public_ip_address
58+
}
59+
env {
60+
name = "DB_PORT"
61+
value = "5432" # default Cloud SQL PostgreSQL port
62+
}
63+
env {
64+
name = "DB_USER"
65+
value = google_sql_user.user.name
66+
}
67+
env {
68+
name = "DB_PASSWORD"
69+
value = var.db_password
70+
}
71+
env {
72+
name = "DB_NAME"
73+
value = google_sql_database.database.name
74+
}
75+
}
76+
}
77+
}
78+
traffic {
79+
percent = 100
80+
latest_revision = true
81+
}
82+
}
83+
84+
resource "google_cloud_run_service_iam_policy" "event-access-cloud-run-noauth" { # enable public access on Cloud Run service
85+
location = google_cloud_run_service.event-access-cloud-run.location
86+
project = google_cloud_run_service.event-access-cloud-run.project
87+
service = google_cloud_run_service.event-access-cloud-run.name
88+
policy_data = data.google_iam_policy.event-access-noauth.policy_data
3189
}

terraform/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
output "db_host" { # db public IP
22
value = google_sql_database_instance.main.public_ip_address
3+
}
4+
5+
output "cloud_run_service_url" {
6+
value = google_cloud_run_service.event-access-cloud-run.status[0].url
37
}

terraform/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ variable "db_tier" {
88
default = "db-f1-micro"
99
}
1010

11+
variable "service_name" { # cloud run service name
12+
type = string
13+
default = "register-ticket-api"
14+
}
15+
1116
# db_port=5432 by default in Cloud SQL
1217

1318
# required vars
@@ -35,3 +40,7 @@ variable "db_name" {
3540
}
3641

3742
# cloud run vars
43+
44+
variable "image_uri" {
45+
type = string
46+
}

0 commit comments

Comments
 (0)