> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Storage lifecycle configurations remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Buckets Should Have Lifecycle Rules Configured" for GCP using GCP console, you can follow the below steps:

        1. Log in to the GCP console using your credentials.

        2. Navigate to the Cloud Storage section by clicking on the hamburger menu on the top left corner and selecting "Storage" under the "Storage" section.

        3. Select the bucket that needs to be remediated from the bucket list.

        4. Click on the "Lifecycle" tab on the left-hand side menu.

        5. Click on the "Add rule" button.

        6. Choose the object condition for applying the lifecycle rule. You can choose options like "Age", "Created before", "Matches prefix" and "Matches regex" to apply the rule.

        7. Set the action to be taken on the object when the rule is met. You can choose options like "Delete", "Archive" and "Set storage class".

        8. Set the duration for which the rule should be applied. You can choose options like "Days", "Weeks", "Months" and "Years".

        9. Click on the "Create" button to create the lifecycle rule.

        10. Verify the rule by checking if it is listed under the "Lifecycle" tab for the bucket.

        By following these steps, the misconfiguration "Buckets Should Have Lifecycle Rules Configured" for GCP can be remediated using the GCP console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Buckets Should Have Lifecycle Rules Configured" in GCP using GCP CLI, you can follow these steps:

        1. First, you need to identify the bucket that needs to have lifecycle rules configured. You can use the following command to list all the buckets in your project:

        ```
        gsutil ls
        ```

        2. Once you have identified the bucket, you can use the following command to set a lifecycle rule on the bucket:

        ```
        gsutil lifecycle set <lifecycle.json> gs://<bucket-name>
        ```

        Here, `<lifecycle.json>` is a JSON file that contains the lifecycle configuration for the bucket. You can create this file using any text editor and include the following example configuration:

        ```
        {
          "rule":
          [
            {
              "action": {"type": "Delete"},
              "condition": {"age": 30}
            }
          ]
        }
        ```

        This configuration will delete any object that is older than 30 days.

        3. Once you have created the `<lifecycle.json>` file, you can run the above command by replacing `<bucket-name>` with the name of the bucket that needs to have the lifecycle rule configured.

        After running the above command, the bucket will have a lifecycle rule configured, and any objects that meet the criteria specified in the rule will be automatically deleted.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the issue of buckets not having lifecycle rules configured in GCP using Python, you can follow the below steps:

        1. First, you need to authenticate to GCP using a service account key. You can create a service account key by following the instructions given in the [official GCP documentation](https://cloud.google.com/iam/docs/creating-managing-service-account-keys).

        2. Once you have the service account key, you can use the `google-cloud-storage` Python library to access the GCP storage buckets. If you don't have the library installed, you can install it using the following command:

           ```
           pip install google-cloud-storage
           ```

        3. After installing the library, you can use the following Python code to remediate the issue by setting lifecycle rules for all the buckets in the GCP project:

           ```python theme={null}
           from google.cloud import storage

           # Authenticate to GCP using the service account key
           client = storage.Client.from_service_account_json('path/to/service_account_key.json')

           # Get a list of all the buckets in the project
           buckets = client.list_buckets()

           # Set lifecycle rules for each bucket
           for bucket in buckets:
               bucket.lifecycle_rules = [{'action': {'type': 'Delete'}, 'condition': {'age': 30}}]
               bucket.patch()
           ```

           In the above code, we are setting a lifecycle rule to delete objects in the bucket that are older than 30 days. You can adjust the age as per your requirements.

        4. Save the Python file and run it using the following command:

           ```
           python filename.py
           ```

           This will set the lifecycle rules for all the buckets in the GCP project, and remediate the misconfiguration.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "google_storage_bucket" "BUCKET_NAME" {
          name     = "BUCKET_NAME"        # replace with your bucket name
          location = "BUCKET_LOCATION"    # e.g. "US", "EU", "asia-south1"

          # Example: delete noncurrent (archived) object versions older than 30 days
          lifecycle_rule {
            action {
              type = "Delete"
            }

            condition {
              is_live = false
              age     = 30                  # number of days since object became noncurrent
            }
          }

          # Example: delete any object older than 365 days
          lifecycle_rule {
            action {
              type = "Delete"
            }

            condition {
              age = 365                     # days since object creation
            }
          }

          # Example: transition objects with a specific prefix to Nearline after 90 days
          lifecycle_rule {
            action {
              type          = "SetStorageClass"
              storage_class = "NEARLINE"
            }

            condition {
              age    = 90
              matches_prefix = ["LOGS_PREFIX/"]  # replace with your prefix, e.g. "logs/"
            }
          }
        }
        ```

        Changing or adding `lifecycle_rule` blocks on an existing `google_storage_bucket` does not normally force bucket replacement, so it is a safe change to apply.

        For verification, `terraform plan` should show `lifecycle_rule` being added to (or updated on) the `google_storage_bucket.BUCKET_NAME` resource with no `-/+` (replace) indicator on the bucket itself.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
