Skip to content

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

  1. In your Terraform project, define the following local variables:

    locals {
      log_retention = {
        production = 90
      }
    
      workspace_log_retention = lookup(local.log_retention, terraform.workspace, null)
    }
    

    In this example, log_retention is a map where each key represents a workspace name, and the corresponding value is the number of days to retain logs. The workspace_log_retention local variable is determined using the lookup() function. If the current workspace is not listed in the log_retention map, workspace_log_retention will be set to null.

    This example sets the log retention period to 90 days in the production workspace. All other workspaces will continue to use the GCP default of 30 days.

  2. Add the google_logging_project_bucket_config resource to your code:

    resource "google_logging_project_bucket_config" "retention_override" {
      count = local.workspace_log_retention != null ? 1 : 0
    
      project        = "projects/${local.project}"
      location       = "global"
      retention_days = local.workspace_log_retention
      bucket_id      = "_Default"
    }
    

    This resource will be created only if the log_retention map includes a value for the current Terraform workspace (i.e. workspace_log_retention is not null).

  3. Apply the changes using logan:

    logan --workspace=production terraform apply
    

Summary

  • The override applies only to workspaces explicitly listed in the log_retention map.
  • Workspaces not listed will retain the default 30-day log retention.
  • The override targets the _Default logging bucket.
  • To remove an override, delete the entry from the log_retention map and re-apply Terraform.
  • You can verify the configured retention period in the GCP Console under Logging > Logs Storage.