How to override log retention period for product environments in GCP¶
This short guide explains how to override the default Google Cloud Logging retention period (30 days) for specific environments (Terraform workspaces).
Info
Previously, this was configured using the log_retention variable in
gcp-product-factory.
It is now handled individually by each project, as needed.
Configuration steps¶
-
In your Terraform project, define the following local variable:
log_retention = lookup( { production = 90 }, terraform.workspace, null )In this example, the value of the
log_retentionvariable is determined using thelookup()function, which searches a map where each key is a workspace name and each value is the number of days to retain logs. If the current workspace is not found in the map,log_retentionwill be set tonull.This example sets the log retention period to 90 days in the
productionworkspace. All other workspaces will continue to use the GCP default of 30 days. -
Add the
google_logging_project_bucket_configresource to your code:resource "google_logging_project_bucket_config" "default" { count = local.log_retention != null ? 1 : 0 project = "projects/${local.project}" location = "global" retention_days = local.log_retention bucket_id = "_Default" }This resource is created only when the
log_retentionvariable is notnull, meaning the lookup returns a value for a current Terraform workspace. -
Apply the changes using logan:
logan --workspace=production terraform apply
Summary¶
- The override applies only to workspaces explicitly listed in the
log_retentionmap. - Workspaces not listed will retain the default 30-day log retention.
- The override targets the
_Defaultlogging bucket. - To remove an override, delete the entry from the
log_retentionmap and re-apply Terraform. - You can verify the configured retention period in the GCP Console under Logging > Logs Storage.