-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3407 from yuvipanda/usage-logs
Allow enabling usage logs on GCS storage buckets & enable for LEAP
- Loading branch information
Showing
4 changed files
with
119 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,14 @@ resource "google_storage_bucket" "user_buckets" { | |
// Set these values explicitly so they don't "change outside terraform" | ||
labels = {} | ||
|
||
dynamic "logging" { | ||
for_each = each.value.usage_logs ? [1] : [] | ||
|
||
content { | ||
log_bucket = google_storage_bucket.usage_logs_bucket.name | ||
} | ||
} | ||
|
||
dynamic "lifecycle_rule" { | ||
for_each = each.value.delete_after != null ? [1] : [] | ||
|
||
|
@@ -26,6 +34,40 @@ resource "google_storage_bucket" "user_buckets" { | |
} | ||
} | ||
|
||
# Create GCS bucket that can store *usage* logs (access logs). | ||
# Helpful to see what data is *actually* being used. | ||
# https://cloud.google.com/storage/docs/access-logs | ||
# | ||
# We create this bucket unconditionally, because it costs nothing. | ||
# It only costs if we actually enable this logging, which is done in | ||
# per-bucket config. | ||
# | ||
# We only keep them for 30 days so they don't end up costing a | ||
# ton of money | ||
resource "google_storage_bucket" "usage_logs_bucket" { | ||
name = "${var.prefix}-gcs-usages-logs" | ||
location = var.region | ||
project = var.project_id | ||
|
||
labels = {} | ||
|
||
lifecycle_rule { | ||
condition { | ||
age = "30d" | ||
} | ||
action { | ||
type = "Delete" | ||
} | ||
} | ||
} | ||
|
||
# Provide access to GCS infrastructure to write usage logs to this bucket | ||
resource "google_storage_bucket_iam_member" "usage_logs_bucket_access" { | ||
bucket = google_storage_bucket.logging_bucket.name | ||
member = "group:[email protected]" | ||
role = "roles/storage.objectCreator" | ||
} | ||
|
||
locals { | ||
# Nested for loop, thanks to https://www.daveperrett.com/articles/2021/08/19/nested-for-each-with-terraform/ | ||
bucket_admin_permissions = distinct(flatten([ | ||
|
@@ -95,3 +137,14 @@ output "buckets" { | |
the full name of all GCS buckets created conveniently. | ||
EOT | ||
} | ||
|
||
output "usage_log_bucket" { | ||
value = google_storage_bucket.usage_logs_bucket.name | ||
description = <<-EOT | ||
Name of GCS bucket containing GCS usage logs (when enabled). | ||
https://cloud.google.com/storage/docs/access-logs has more information | ||
on GCS usage logs. It has to be enabled on a per-bucket basis - see | ||
the documentation for the `user_buckets` variable for more information. | ||
EOT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,29 +30,35 @@ filestore_capacity_gb = 2048 | |
user_buckets = { | ||
"scratch-staging" : { | ||
"delete_after" : 7, | ||
"extra_admin_members" : [] | ||
"extra_admin_members" : [], | ||
"usage_logs" : true, | ||
}, | ||
"scratch" : { | ||
"delete_after" : 7, | ||
"extra_admin_members" : [] | ||
"extra_admin_members" : [], | ||
"usage_logs" : true, | ||
} | ||
# For https://github.com/2i2c-org/infrastructure/issues/1230#issuecomment-1278183441 | ||
"persistent" : { | ||
"delete_after" : null, | ||
"extra_admin_members" : ["group:[email protected]"] | ||
"extra_admin_members" : ["group:[email protected]"], | ||
"usage_logs" : true, | ||
}, | ||
"persistent-staging" : { | ||
"delete_after" : null, | ||
"extra_admin_members" : ["group:[email protected]"] | ||
"extra_admin_members" : ["group:[email protected]"], | ||
"usage_logs" : true, | ||
} | ||
# For https://github.com/2i2c-org/infrastructure/issues/1230#issuecomment-1278183441 | ||
"persistent-ro" : { | ||
"delete_after" : null, | ||
"extra_admin_members" : ["group:[email protected]"], | ||
"usage_logs" : true, | ||
}, | ||
"persistent-ro-staging" : { | ||
"delete_after" : null, | ||
"extra_admin_members" : ["group:[email protected]"], | ||
"usage_logs" : true, | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters