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 variable:

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

    In this example, the value of the log_retention variable is determined using the lookup() 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_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" "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_retention variable is not null, meaning the lookup returns a value for a current Terraform workspace.

  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.