• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How we built a scalable Odoo solution for a Travel Tech service provider!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    How we built a scalable Odoo solution for TravelTech service provider!

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy
Script to Gather Retention Details of S3 Buckets

Script to Gather Retention Details of S3 Buckets

Ashley Merrin Shaji

  • 6 min read
Script to Gather Retention Details of S3 Buckets

Generating audio, please wait...

Introduction 

This script is designed to retrieve and display retention details for all S3 buckets in your AWS account.  The script queries each bucket for life-cycle configurations and generates a comprehensive report in JSON format. Additionally, you can format the results into a tabular structure for easy viewing. 

Purpose 

  • Fetch, Identify and display retention policies for each bucket. 
  • Retrieve folder-specific lifecycle policies where applicable. 
  • Captures post-retention actions (e.g., expiration, transition). 
  • Save results in a JSON file for further processing. 

Prerequisites 

AWS CLI must be installed and configured with appropriate permissions to list S3 buckets and view life-cycle configurations. 

Script 

#!/bin/bash 
  
# Created by: Ashley Merrin Shaji
  
# Get the list of all S3 buckets in your account 
buckets=$(aws s3api list-buckets --query "Buckets[].Name" --output text) 
  
# Create a file to store the results 
output_file="s3_bucket_lifecycle_details.json" 
echo "[" > $output_file # Start the JSON array 
  
# Loop through each bucket 
for bucket in $buckets; do 
  echo "Fetching details for bucket: $bucket" 
    
  # Get the bucket's lifecycle configuration 
  lifecycle=$(aws s3api get-bucket-lifecycle-configuration --bucket "$bucket" 2>/dev/null) 
    
  # If lifecycle configuration exists, process it 
  if [ $? -eq 0 ]; then 
    echo "{\"Bucket\": \"$bucket\", \"LifecycleConfiguration\": $lifecycle," >> $output_file 
      
    # Fetch prefix-specific lifecycle rules with actions 
    prefixes=$(echo "$lifecycle" | jq -r '.Rules[]? | { 
      Prefix: .Filter.Prefix, 
      Expiration: .Expiration, 
      Transition: .Transitions[]? // null 
    }') 
      
    if [ -n "$prefixes" ]; then 
      echo "\"PrefixDetails\": [$prefixes]}" >> $output_file 
    else 
      echo "\"PrefixDetails\": []}" >> $output_file 
    fi 
  else 
    echo "{\"Bucket\": \"$bucket\", \"LifecycleConfiguration\": null, \"PrefixDetails\": []}," >> $output_file 
  fi 
done 
  
# Close the JSON array properly 
sed -i '$ s/,$//' $output_file # Remove the trailing comma from the last entry 
echo "]" >> $output_file 
  
echo "All bucket details with lifecycle configurations, prefix-specific rules, and post-retention actions saved to $output_file" 

Command to Format and Display Output 

Use the following command to parse and display the retention information in a tabular format: 

cat s3_bucket_lifecycle_details.json | jq -r ' 
[ 
.[] | 
{ 
  "Bucket": .Bucket, 
  "PostRetentionActions": ( 
    if .LifecycleConfiguration != null and .LifecycleConfiguration.Rules != null then 
      .LifecycleConfiguration.Rules[] | 
      ("Prefix: " + (.Filter.Prefix // "No Prefix") + ", Expiration Days: " + (.Expiration.Days | tostring // "No Expiration") + ", Transition Action: " + 
      ( 
        if .Transitions != null then 
          (.Transitions[] | ("After " + (.Days | tostring) + " Days -> " + .StorageClass)) 
        else 
          "No Transition" 
        end 
      )) 
    else 
      "No Retention Policy" 
    end 
  ) 
} 
] | 
(["Bucket Name", "Post-Retention Actions"] | @tsv), 
(.[] | [.Bucket, .PostRetentionActions] | @tsv) 
' | column -t -s$'\t' 
' 

Output 

The script generates a table with the following columns: 
Bucket Name: The name of the S3 bucket. 
Retention Policy: 
The jq command extracts and formats the following details for each bucket: 

  • "Prefix": Displays the prefix (folder) for which the lifecycle policy is applied. 
  • "Expiration Days": Displays the number of days after which objects expire. If no expiration policy is set, it shows "No Expiration". 
  • If no lifecycle configuration is present for the bucket, it displays "No Retention Policy". 

JSON Output: 

[ 
  { 
    "Bucket": "example-bucket-1", 
    "LifecycleConfiguration": { 
      "Rules": [ 
        { 
          "Filter": { "Prefix": "folder1/" }, 
          "Expiration": { "Days": 30 }, 
          "Transitions": [ 
            { "Days": 60, "StorageClass": "GLACIER" } 
          ] 
        } 
      ] 
    }, 
    "PrefixDetails": [ 
      { 
        "Prefix": "folder1/", 
        "Expiration": { "Days": 30 }, 
        "Transition": { "Days": 60, "StorageClass": "GLACIER" } 
      } 
    ] 
  }, 
  { 
    "Bucket": "example-bucket-2", 
    "LifecycleConfiguration": null, 
    "PrefixDetails": [] 
  } 
] 

Tabular Output: 

Bucket Name             Post-Retention Actions 
example-bucket-1 Prefix: folder1/, Expiration Days: 30, Transition Action: After 60 Days -> GLACIER
example-bucket-2No Retention Policy

Conclusion 

This script provides a comprehensive way to retrieve and display life-cycle policies for all S3 buckets in an AWS account, including folder-specific life-cycle rules and post-retention actions like expiration and transitions to different storage classes. It generates a detailed JSON report, helping users understand how long objects are retained or when they are transitioned or deleted. 

Additionally, the provided command formats the output into a clear, tabular view, making it easy for administrators to quickly identify which buckets or folders have specific retention policies and whether objects are set to expire or transition to another storage class. 

This solution ensures efficient data life-cycle management across all S3 buckets, aiding in compliance, cost optimization, and data governance. 

  • AWS
  • DevOps
Promotional banner
Promotional banner

Automating SES Event Monitoring with AWS Lambda and CloudWatch Logs

Automating SES Event Monitoring with AWS Lambda and CloudWatch Logs
  • AWS
logo

Automating SES Event Monitoring with AWS Lambda and CloudWatch Logs

Automating SES Event Monitoring with AWS Lambda and CloudWatch Logs
  • AWS
  • DevOps
logo

Comparison of GCR (Google), ECR (Amazon), and ACR (Azure)

Comparison of GCR (Google), ECR (Amazon), and ACR (Azure)
  • DevOps
  • Azure
  • GCP
  • AWS
logo

Posts by Ashley Merrin Shaji