A comprehensive collection of Python scripts for automating AWS infrastructure management, monitoring, optimization, and security compliance. These scripts leverage the AWS SDK for Python (boto3) to streamline DevOps operations, enhance security, and reduce operational costs.
- Overview
- Features
- Repository Structure
- Getting Started
- Script Categories
- Usage Examples
- Best Practices
- Security Considerations
- Contributing
- License
- Acknowledgments
This repository contains over 40 Python scripts designed to automate common AWS administrative tasks, security checks, and cost optimization processes. These scripts are based on real-world DevOps use cases and best practices for AWS cloud management.
- Automation: Reduce manual effort for routine AWS management tasks
- Cost Optimization: Identify and eliminate unnecessary AWS resource costs
- Security Compliance: Automate security audits and enforce best practices
- Infrastructure Management: Simplify resource provisioning and configuration
- Monitoring: Track performance metrics and health status of AWS resources
- Disaster Recovery: Automate backup and recovery procedures
The repository is organized by AWS service and automation purpose:
python-automation-scripts/
βββ README.md
βββ requirements.txt
βββ EC2 & Compute/
β βββ manage_ec2_instance.py
β βββ ec2_instance_scheduler.py
β βββ ec2_custom_ami_builder.py
β βββ ...
βββ Storage/
β βββ delete_old_s3_objects.py
β βββ sync_s3_buckets.py
β βββ ...
βββ Security/
β βββ security_compliance_checker.py
β βββ rotate_iam_keys.py
β βββ ...
βββ ...
- Python 3.6 or higher
- AWS account with appropriate permissions
- AWS CLI installed and configured
- Boto3 library
-
Clone this repository:
git clone https://github.com/Olisaemeka111/python-automation-scripts.git cd python-automation-scripts
-
Install the required dependencies:
pip install -r requirements.txt
-
Configure AWS credentials if you haven't already:
aws configure
Provide your AWS Access Key ID, Secret Access Key, default region (e.g., us-east-1), and output format (e.g., json).
Most scripts use the default AWS credentials and region configuration from your environment. Some scripts may require additional configuration parameters, which are typically specified as command-line arguments or in configuration files.
Script | Description | Usage |
---|---|---|
manage_ec2_instance.py |
Start, stop, restart, or terminate EC2 instances | python manage_ec2_instance.py <instance_id> <action> |
ec2_instance_scheduler.py |
Schedule EC2 instances to start/stop based on time patterns | python ec2_instance_scheduler.py |
ec2_custom_ami_builder.py |
Create and manage custom AMIs with specific configurations | python ec2_custom_ami_builder.py <instance_id> <ami_name> |
auto_scale_ec2.py |
Configure auto-scaling groups with CloudWatch alarms | python auto_scale_ec2.py |
create_ami.py |
Create Amazon Machine Images from EC2 instances | python create_ami.py <instance_id> <ami_name> |
restart_unhealthy_instances.py |
Detect and restart unhealthy EC2 instances | python restart_unhealthy_instances.py |
stop_idle_instances.py |
Identify and stop underutilized EC2 instances | python stop_idle_instances.py <cpu_threshold> <hours> |
delete_unused_ebs_volumes.py |
Find and delete unattached EBS volumes | python delete_unused_ebs_volumes.py |
tag_ec2_instance.py |
Add or update tags on EC2 resources | python tag_ec2_instance.py <instance_id> <key> <value> |
Script | Description | Usage |
---|---|---|
delete_old_s3_objects.py |
Implement S3 data retention policies | python delete_old_s3_objects.py <bucket> <days> |
sync_s3_buckets.py |
Copy objects between S3 buckets | python sync_s3_buckets.py <source> <destination> |
enable_s3_bucket_versioning.py |
Enable versioning on S3 buckets | python enable_s3_bucket_versioning.py |
backup_rds_to_s3.py |
Export RDS snapshots to S3 for backup | python backup_rds_to_s3.py <db_instance> <bucket> |
create_rds_snapshot.py |
Create RDS database snapshots | python create_rds_snapshot.py <db_instance> |
export_dynamodb_to_s3.py |
Export DynamoDB tables to S3 | python export_dynamodb_to_s3.py <table> <bucket> |
dynamodb_autoscaling_manager.py |
Configure DynamoDB auto-scaling | python dynamodb_autoscaling_manager.py <table> |
cleanup_snapshots.py |
Delete outdated EC2 snapshots | python cleanup_snapshots.py <days> |
Script | Description | Usage |
---|---|---|
security_compliance_checker.py |
Comprehensive AWS security audit | python security_compliance_checker.py |
rotate_iam_keys.py |
Rotate IAM access keys | python rotate_iam_keys.py <username> <days> |
check_open_security_groups.py |
Identify security groups with open access | python check_open_security_groups.py |
list_iam_users_and_policies.py |
List IAM users and their policies | python list_iam_users_and_policies.py |
aws_account_scanner.py |
Scan AWS account for security issues | python aws_account_scanner.py |
aws_resource_filter.py |
Filter AWS resources by various criteria | python aws_resource_filter.py <resource_type> |
Script | Description | Usage |
---|---|---|
vpc_analyzer.py |
Analyze VPC configurations and security | python vpc_analyzer.py <vpc_id> |
update_dns_record.py |
Update Route53 DNS records | python update_dns_record.py <domain> <ip> <zone_id> |
cloudfront_cache_invalidator.py |
Manage CloudFront cache invalidations | python cloudfront_cache_invalidator.py <dist_id> <paths> |
route53_dns_backup.py |
Backup Route53 DNS configurations | python route53_dns_backup.py |
vpc_peering_manager.py |
Manage VPC peering connections | python vpc_peering_manager.py |
Script | Description | Usage |
---|---|---|
deploy_lambda_function.py |
Deploy Lambda functions | python deploy_lambda_function.py <function> <zip_file> |
cleanup_old_lambda_versions.py |
Remove old Lambda versions | python cleanup_old_lambda_versions.py <function> |
monitor_lambda_metrics.py |
Track Lambda performance metrics | python monitor_lambda_metrics.py <function> |
lambda_layer_manager.py |
Manage Lambda layers | python lambda_layer_manager.py |
cleanup_ecr_repositories.py |
Clean up old ECR images | python cleanup_ecr_repositories.py <repo> <count> |
ecs_service_deployer.py |
Deploy services to ECS clusters | python ecs_service_deployer.py <cluster> <service> |
eks_management.py |
Manage EKS Kubernetes clusters | python eks_management.py <cluster> |
Script | Description | Usage |
---|---|---|
find_cost_savings.py |
Identify cost optimization opportunities | python find_cost_savings.py |
get_aws_billing.py |
Monitor AWS billing costs | python get_aws_billing.py |
aws_resource_analyzer.py |
Analyze resource usage patterns | python aws_resource_analyzer.py |
aws_infrastructure_cleanup.py |
Clean up unused AWS resources | python aws_infrastructure_cleanup.py |
aws_targeted_cleanup.py |
Target specific resources for cleanup | python aws_targeted_cleanup.py <resource_type> |
Script | Description | Usage |
---|---|---|
automated_health_check.py |
Monitor AWS resource health | python automated_health_check.py |
create_cloudwatch_alarm.py |
Set up CloudWatch alarms | python create_cloudwatch_alarm.py <resource> <metric> |
aws_resource_lister.py |
List all resources in an AWS account | python aws_resource_lister.py |
aws_resource_summarizer.py |
Generate summary reports of AWS resources | python aws_resource_summarizer.py |
aws_detailed_resource_list.py |
Create detailed resource inventories | python aws_detailed_resource_list.py |
test_boto3_setup.py |
Verify Boto3 configuration | python test_boto3_setup.py |
json_to_txt_converter.py |
Convert JSON outputs to readable text | python json_to_txt_converter.py <input> <output> |
python aws_account_scanner.py
This script performs a comprehensive security scan of your AWS account, checking for:
- Public S3 buckets
- Unencrypted EBS volumes
- Security groups with open ports
- IAM users without MFA
- Expired IAM access keys
- CloudTrail logging status
- And more...
The results are saved to a JSON file with a timestamp.
python ec2_instance_scheduler.py --config scheduler_config.json
This script allows you to define start/stop schedules for EC2 instances based on tags, time patterns, and utilization metrics. It can help reduce costs by automatically shutting down non-production instances during off-hours.
python find_cost_savings.py --detailed-report
This script analyzes your AWS environment to identify cost-saving opportunities, including:
- Underutilized EC2 instances
- Unused Elastic IPs
- Old EBS snapshots
- Idle load balancers
- Oversized RDS instances
- Unattached EBS volumes
These scripts interact with your AWS account and can make changes to your resources. Always:
- Review the code before running it
- Ensure you have appropriate permissions
- Test in a non-production environment first
- Be careful with scripts that delete or modify resources
- Never hardcode AWS credentials in scripts
- Use IAM roles and policies to limit script permissions
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Based on real-world DevOps use cases and AWS best practices
- Inspired by the article "20 Must-Have Python Boto3 Scripts for AWS Automation" by Antoine Fongang
- Thanks to the AWS and Python communities for their valuable resources and documentation