-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
317 lines (232 loc) · 8.25 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Provider
# Terraform configuration to set up providers by version.
terraform {
required_providers {
google-beta = {
source = "hashicorp/google-beta"
version = "~> 4.0"
}
}
}
# Configures the provider to use the resource block's specified project for quota checks.
provider "google-beta" {
user_project_override = true
}
# Configures the provider to not use the resource block's specified project for quota checks.
# This provider should only be used during project creation and initializing services.
provider "google-beta" {
alias = "no_user_project_override"
user_project_override = false
}
provider "google" {
project = var.project_name
region = var.project_name
}
# Basic project setup
# Creates a new Google Cloud project.
resource "google_project" "default" {
provider = google-beta.no_user_project_override
name = var.project_name
project_id = var.project_id
# Required for any service that requires the Blaze pricing plan
# (like Firebase Authentication with GCIP)
billing_account = var.billing_account
# Required for the project to display in any list of Firebase projects.
labels = {
"firebase" = "enabled"
}
}
# Enables required APIs.
resource "google_project_service" "default" {
provider = google-beta.no_user_project_override
project = google_project.default.project_id
for_each = toset([
"cloudbilling.googleapis.com",
"cloudresourcemanager.googleapis.com",
"firebase.googleapis.com",
# Enabling the ServiceUsage API allows the new project to be quota checked from now on.
"serviceusage.googleapis.com",
"aiplatform.googleapis.com",
"servicenetworking.googleapis.com",
"cloudfunctions.googleapis.com",
"cloudbuild.googleapis.com",
"run.googleapis.com",
"eventarc.googleapis.com",
"pubsub.googleapis.com",
"vision.googleapis.com"
])
service = each.key
# Don't disable the service if the resource block is removed by accident.
disable_on_destroy = false
}
# Enables Firebase services for the new project created above.
resource "google_firebase_project" "default" {
provider = google-beta
project = google_project.default.project_id
# Waits for the required APIs to be enabled.
depends_on = [
google_project_service.default
]
}
# WEB APP
resource "google_firebase_web_app" "default" {
provider = google-beta
project = google_firebase_project.default.project
display_name = var.project_name
deletion_policy = "DELETE"
}
# AUTH
# Enable the Identity Toolkit API.
resource "google_project_service" "auth" {
provider = google-beta
project = google_firebase_project.default.project
service = "identitytoolkit.googleapis.com"
# Don't disable the service if the resource block is removed by accident.
disable_on_destroy = false
}
# Create an Identity Platform config.
# Also, enable Firebase Authentication using Identity Platform (if Authentication isn't yet enabled).
resource "google_identity_platform_config" "auth" {
provider = google-beta
project = google_firebase_project.default.project
# For example, you can configure to auto-delete anonymous users.
autodelete_anonymous_users = true
# Wait for identitytoolkit.googleapis.com to be enabled before initializing Authentication.
depends_on = [
google_project_service.auth,
]
}
# STORAGE FOR IMAGES, CLOUD STORAGE BUCKET and FIRESTORE
# Enable required APIs for Cloud Firestore.
resource "google_project_service" "firestore" {
provider = google-beta
project = google_firebase_project.default.project
for_each = toset([
"firestore.googleapis.com",
"firebaserules.googleapis.com",
])
service = each.key
# Don't disable the service if the resource block is removed by accident.
disable_on_destroy = false
}
# Provision the Firestore database instance.
resource "google_firestore_database" "default" {
provider = google-beta
project = google_firebase_project.default.project
name = "(default)"
# See available locations:
# https://firebase.google.com/docs/firestore/locations
location_id = var.region
# "FIRESTORE_NATIVE" is required to use Firestore with Firebase SDKs,
# authentication, and Firebase Security Rules.
type = "FIRESTORE_NATIVE"
concurrency_mode = "OPTIMISTIC"
depends_on = [
google_project_service.firestore
]
}
resource "google_firestore_index" "userDataIndex" {
project = google_firebase_project.default.project
database = google_firestore_database.default.name
collection = "userData"
fields {
field_path = "userId"
order = "ASCENDING"
}
fields {
field_path = "date"
order = "ASCENDING"
}
}
# Enable required APIs for Cloud Storage for Firebase.
resource "google_project_service" "storage" {
provider = google-beta
project = google_firebase_project.default.project
for_each = toset([
"firebasestorage.googleapis.com",
"storage.googleapis.com",
])
service = each.key
# Don't disable the service if the resource block is removed by accident.
disable_on_destroy = false
}
# Provision the default Cloud Storage bucket for the project via Google App Engine.
resource "google_app_engine_application" "default" {
provider = google-beta
project = google_firebase_project.default.project
# See available locations: https://firebase.google.com/docs/projects/locations#default-cloud-location
# This will set the location for the default Storage bucket and the App Engine App.
location_id = var.region # Must be in the same location as Firestore (above)
# Wait until Firestore is provisioned first.
depends_on = [
google_firestore_database.default
]
}
# Make the default Storage bucket accessible for Firebase SDKs, authentication, and Firebase Security Rules.
resource "google_firebase_storage_bucket" "default-bucket" {
provider = google-beta
project = google_firebase_project.default.project
bucket_id = google_app_engine_application.default.default_bucket
}
# Create a ruleset of Firestore Security Rules from a local file.
resource "google_firebaserules_ruleset" "firestore" {
provider = google-beta
project = google_firebase_project.default.project
source {
files {
name = "firestore.rules"
# Write security rules in a local file named "firestore.rules".
# Learn more: https://firebase.google.com/docs/firestore/security/get-started
content = file("firestore.rules")
}
}
# Wait for Firestore to be provisioned before creating this ruleset.
depends_on = [
google_firestore_database.default,
]
}
# Release the ruleset for the Firestore instance.
resource "google_firebaserules_release" "firestore" {
provider = google-beta
name = "cloud.firestore" # must be cloud.firestore
ruleset_name = google_firebaserules_ruleset.firestore.name
project = google_firebase_project.default.project
# Wait for Firestore to be provisioned before releasing the ruleset.
depends_on = [
google_firestore_database.default,
]
lifecycle {
replace_triggered_by = [
google_firebaserules_ruleset.firestore
]
}
}
# Create a ruleset of Cloud Storage Security Rules from a local file.
resource "google_firebaserules_ruleset" "storage" {
provider = google-beta
project = google_firebase_project.default.project
source {
files {
# Write security rules in a local file named "storage.rules".
# Learn more: https://firebase.google.com/docs/storage/security/get-started
name = "storage.rules"
content = file("storage.rules")
}
}
# Wait for the default Storage bucket to be provisioned before creating this ruleset.
depends_on = [
google_firebase_storage_bucket.default-bucket,
]
}
# Release the ruleset to the default Storage bucket.
resource "google_firebaserules_release" "default-bucket" {
provider = google-beta
name = "firebase.storage/${google_app_engine_application.default.default_bucket}"
ruleset_name = "projects/${google_firebase_project.default.project}/rulesets/${google_firebaserules_ruleset.storage.name}"
project = google_firebase_project.default.project
lifecycle {
replace_triggered_by = [
google_firebaserules_ruleset.storage
]
}
}