-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
62 lines (47 loc) · 1.71 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
variable "google_project_id" {
type = string
default = ""
description = "The GCP project id to deploy to"
}
# ------- #
provider "google" {
project = var.google_project_id
region = "europe-west1"
zone = "europe-west1-b"
}
provider "random" {}
# ------- #
resource "random_id" "random_suffix" {
byte_length = 4
}
module "postgrest_database" {
source = "./cloud_sql"
project_id = var.google_project_id
db_name_suffix = random_id.random_suffix.hex
}
module "postgres_api" {
source = "./cloud_run"
project_id = var.google_project_id
service_suffix = random_id.random_suffix.hex
sql_connection_name = module.postgrest_database.db_connection_name
sql_db_username = module.postgrest_database.db_user_name
sql_db_password_secret_name = module.postgrest_database.db_password_secret_name
sql_db_password = module.postgrest_database.db_password
sql_db_table = module.postgrest_database.db_database_name
depends_on = [module.postgrest_database]
}
output "postgres_api_url" {
value = module.postgres_api.service_endpoint
}
output "sql_connection_name" {
value = module.postgrest_database.db_connection_name
}
output "example" {
description = "Example commands to run to add data to Cloud SQL"
value = join("", [
"cloud_sql_proxy -instances=${module.postgrest_database.db_connection_name}=tcp:127.0.0.1:5432 2>/dev/null &\n",
"export _DB_PASSWORD=$(gcloud secrets versions access latest --secret ${module.postgrest_database.db_password_secret_name} | cut -d'@' -f1)\n",
"sleep 1 && psql $_DB_PASSWORD@localhost:5432/${module.postgrest_database.db_database_name} < docs/petstore.sql\n",
"# killall cloud_sql_proxy"
])
}