Provision a GCS bucket the Terraform way
Hand-writing google_storage_bucket HCL means remembering the right nested blocks for versioning, lifecycle, and access control. This builder assembles a correct, opinionated resource — secure defaults on, plus an optional IAM binding — that you can drop straight into your Terraform configuration.
How it works
The generated google_storage_bucket resource declares the bucket name, project, location, and storage_class. It sets uniform_bucket_level_access = true so permissions are governed entirely by IAM rather than legacy per-object ACLs, and enforces public_access_prevention to block accidental public exposure.
A versioning block keeps prior object generations, and an optional lifecycle_rule applies an action once objects reach a given age. Choosing Delete removes old objects, while choosing a storage class (Nearline, Coldline, Archive) emits a SetStorageClass action that tiers data down to cheaper storage. If you enable the IAM binding, a separate google_storage_bucket_iam_member grants a role to a member without overwriting other bindings.
Tips and notes
force_destroyis set tofalseso Terraform will refuse to delete a non-empty bucket — flip it totrueonly for disposable buckets.- Use
google_storage_bucket_iam_member(additive) rather than..._iam_policy(authoritative) unless you intend to manage the entire policy in Terraform. - Combine versioning with a lifecycle rule on noncurrent versions to control cost.
- Bucket names are global; prefix them with your org or project to avoid
409 conflicterrors on apply.