-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.tf
65 lines (48 loc) · 2.19 KB
/
storage.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
resource "google_storage_bucket" "website" {
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket
# https://cloud.google.com/storage/docs/hosting-static-website#create-bucket
provider = google
project = var.project
name = local.website_bucket_name
location = upper(var.bucket_region)
storage_class = "STANDARD"
uniform_bucket_level_access = true
force_destroy = var.website_bucket_force_destroy
website {
main_page_suffix = var.website_index_page_file_name
# Why: https://stackoverflow.com/a/40786636/1557013
# How so: https://cloud.google.com/storage/docs/gsutil/commands/web
not_found_page = var.website_index_page_file_name
}
logging {
log_bucket = google_storage_bucket.website_usage_logs.name
log_object_prefix = replace(var.website_domain, ".", "-")
}
}
resource "google_storage_bucket_iam_member" "website_public" {
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_iam
provider = google
bucket = google_storage_bucket.website.name
# https://cloud.google.com/storage/docs/hosting-static-website#sharing
role = "roles/storage.legacyObjectReader"
member = "allUsers"
}
resource "google_service_account" "website" {
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_service_account
provider = google
project = var.project
# Forcing the name to match the pre-defined regex "^[a-z](?:[-a-z0-9]{4,28}[a-z0-9])$":
account_id = trim(substr(google_storage_bucket.website.name, 0, 28), "-")
}
resource "google_storage_bucket_iam_member" "website_internal" {
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_iam
provider = google
bucket = google_storage_bucket.website.name
role = "roles/storage.admin"
member = "serviceAccount:${google_service_account.website.email}"
}
resource "google_service_account_key" "website" {
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_service_account_key
provider = google
service_account_id = google_service_account.website.name
}