Automate Amazon S3 lifecycle policies using Python and AWS CLI β from bucket creation to cleanup.
S3 Lifecycle Buddy is a fully automated Python tool that manages the entire lifecycle of an S3 bucket using code β no Console clicks required.
This project handles:
- β Region-aware S3 bucket creation
- β Lifecycle policy application via inline JSON
- β Upload of a test object
- β Optional cleanup that deletes both the object and the bucket
Itβs a real-world cloud automation use case for cost optimization, policy enforcement, and DevOps-style AWS control.
AWS storage can quietly rack up costs without proper management. This script demonstrates how to:
- Enforce data retention using lifecycle rules
- Reduce cloud waste from forgotten dev/test buckets
- Automate cleanup workflows to avoid manual intervention
- Python 3
- AWS CLI β for command-line resource management
- boto3 β AWS SDK for Python
- subprocess β to bridge Python with CLI commands
s3-lifecycle-buddy/
βββ s3_lifecycle.py # Main Python script
βββ README.md # Project documentation
βββ test_upload.txt # Auto-generated test file
βββ .gitignore # Git cleanup rules
βββ venv/
-
Bucket Creation
Creates an S3 bucket using AWS CLI logic that adjusts automatically forus-east-1
region behavior. -
Lifecycle Policy
Applies a lifecycle policy via inline JSON to automatically delete objects after 1 day. -
Test Upload
Generates and uploads a sample file to validate the lifecycle configuration. -
Cleanup (Optional)
Deletes all objects and the bucket using:aws s3 rb s3://bucket-name --force
- Python 3 installed
- AWS CLI installed and configured (run: aws configure)
- Virtual environment (optional but recommended)
- Boto3 installed in your environment
# 1. Create a virtual environment (optional but recommended)
python -m venv venv
# 2. Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# 3. Install boto3 if not already installed
pip install boto3
# 4. Run the script
python s3_lifecycle.py
π License MIT License β Free to use, modify, and distribute.