This commit is contained in:
67
skills/google-cloud-iac-patterns/references/cloud-storage.md
Normal file
67
skills/google-cloud-iac-patterns/references/cloud-storage.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# Cloud Storage
|
||||
|
||||
A private bucket with a retention period and a lifecycle.
|
||||
|
||||
- Bucket names are global. Build the name from the project id.
|
||||
- `uniform_bucket_level_access = true` and `public_access_prevention = "enforced"` keep the bucket private.
|
||||
- `retention_policy.retention_period` is in seconds and stops objects being deleted before then. A lifecycle `Delete` rule with `age` in days removes them after. Retaining for 30 days and then deleting needs both.
|
||||
- Versioning and a retention policy cannot be used on the same bucket.
|
||||
- Customer-managed encryption is `encryption.default_kms_key_name`; the Cloud Storage service agent needs `roles/cloudkms.cryptoKeyEncrypterDecrypter` on the key first.
|
||||
- `force_destroy = false` stops Terraform deleting a bucket that holds objects.
|
||||
|
||||
```hcl
|
||||
variable "project_id" {
|
||||
type = string
|
||||
description = "Project to deploy into."
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
type = string
|
||||
description = "Region for regional resources."
|
||||
}
|
||||
|
||||
resource "google_service_account" "writer" {
|
||||
project = var.project_id
|
||||
account_id = "raw-events-writer"
|
||||
display_name = "Writes raw events"
|
||||
}
|
||||
|
||||
resource "google_storage_bucket" "raw_events" {
|
||||
project = var.project_id
|
||||
name = "${var.project_id}-raw-events"
|
||||
location = var.region
|
||||
storage_class = "STANDARD"
|
||||
uniform_bucket_level_access = true
|
||||
public_access_prevention = "enforced"
|
||||
force_destroy = false
|
||||
|
||||
retention_policy {
|
||||
retention_period = 2592000
|
||||
}
|
||||
|
||||
lifecycle_rule {
|
||||
condition {
|
||||
age = 31
|
||||
}
|
||||
action {
|
||||
type = "Delete"
|
||||
}
|
||||
}
|
||||
|
||||
lifecycle_rule {
|
||||
condition {
|
||||
age = 7
|
||||
}
|
||||
action {
|
||||
type = "SetStorageClass"
|
||||
storage_class = "NEARLINE"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_storage_bucket_iam_member" "writer_creates_objects" {
|
||||
bucket = google_storage_bucket.raw_events.name
|
||||
role = "roles/storage.objectCreator"
|
||||
member = google_service_account.writer.member
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user