-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.tf
157 lines (127 loc) · 5.12 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
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
terraform {
# The modules used in this example have been updated with 0.12 syntax, additionally we depend on a bug fixed in
# version 0.12.7.
required_version = ">= 0.12.7"
required_providers {
google = ">= 3.4"
}
}
# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY A GOOGLE CLOUD SOURCE REPOSITORY
# ---------------------------------------------------------------------------------------------------------------------
resource "google_sourcerepo_repository" "repo" {
name = var.repository_name
}
# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY A CLOUD RUN SERVICE
# ---------------------------------------------------------------------------------------------------------------------
resource "google_cloud_run_service" "service" {
name = var.service_name
location = var.location
template {
metadata {
annotations = {
"client.knative.dev/user-image" = local.image_name
# uncomment the following line to connect to the cloud sql database instance
#"run.googleapis.com/cloudsql-instances" = local.instance_connection_name
}
}
spec {
containers {
image = local.image_name
# uncomment the following env vars to provide the cloud run service
# with the cloud sql database details.
#env {
# name = "INSTANCE_CONNECTION_NAME"
# value = local.instance_connection_name
#}
#
#env {
# name = "MYSQL_DATABASE"
# value = var.db_name
#}
#
#env {
# name = "MYSQL_USERNAME"
# value = var.db_username
#}
#
#env {
# name = "MYSQL_PASSWORD"
# value = var.db_password
#}
}
}
}
traffic {
percent = 100
latest_revision = true
}
}
# ---------------------------------------------------------------------------------------------------------------------
# EXPOSE THE SERVICE PUBLICALLY
# We give all users the ability to invoke the service.
# ---------------------------------------------------------------------------------------------------------------------
resource "google_cloud_run_service_iam_member" "allUsers" {
service = google_cloud_run_service.service.name
location = google_cloud_run_service.service.location
role = "roles/run.invoker"
member = "allUsers"
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE A CLOUD BUILD TRIGGER
# ---------------------------------------------------------------------------------------------------------------------
resource "google_cloudbuild_trigger" "cloud_build_trigger" {
description = "Cloud Source Repository Trigger ${var.repository_name} (${var.branch_name})"
trigger_template {
branch_name = var.branch_name
repo_name = var.repository_name
}
# These substitutions have been defined in the sample app's cloudbuild.yaml file.
# See: https://github.com/robmorgan/sample-docker-app/blob/master/cloudbuild.yaml#L43
substitutions = {
_LOCATION = var.location
_GCR_REGION = var.gcr_region
_SERVICE_NAME = var.service_name
}
# The filename argument instructs Cloud Build to look for a file in the root of the repository.
# Either a filename or build template (below) must be provided.
filename = "cloudbuild.yaml"
depends_on = [google_sourcerepo_repository.repo]
}
# ---------------------------------------------------------------------------------------------------------------------
# OPTIONALLY DEPLOY A DATABASE
# ---------------------------------------------------------------------------------------------------------------------
resource "google_sql_database_instance" "master" {
count = var.deploy_db ? 1 : 0
name = var.db_instance_name
region = var.location
database_version = "MYSQL_5_7"
settings {
tier = "db-f1-micro"
}
}
resource "google_sql_database" "default" {
count = var.deploy_db ? 1 : 0
name = var.db_name
project = var.project
instance = google_sql_database_instance.master[0].name
depends_on = [google_sql_database_instance.master]
}
resource "google_sql_user" "default" {
count = var.deploy_db ? 1 : 0
project = var.project
name = var.db_username
instance = google_sql_database_instance.master[0].name
host = var.db_user_host
password = var.db_password
depends_on = [google_sql_database.default]
}
# ---------------------------------------------------------------------------------------------------------------------
# PREPARE LOCALS
# ---------------------------------------------------------------------------------------------------------------------
locals {
image_name = var.image_name == "" ? "${var.gcr_region}.gcr.io/${var.project}/${var.service_name}" : var.image_name
# uncomment the following line to connect to the cloud sql database instance
#instance_connection_name = "${var.project}:${var.location}:${google_sql_database_instance.master[0].name}"
}