This Python project automates the provisioning and monitoring of web servers on AWS, simplifying the process of EC2 instance launch, S3 bucket setup, and website deployment with an easy-to-use script.
- Utilises Python 3 and the Boto3 library to interact with AWS services.
- Automates the launch of an Amazon Linux EC2 nano instance, automatically determining the latest AMI using AWS Systems Manager Parameter Store.
- Configures security groups and tags for easy management and security.
- Installs and starts a web server to display instance metadata (with potential customization).
- Configures an S3 bucket for static website hosting, enabling content uploads and public access.
- Opens a browser to show the newly deployed EC2 instance and S3 static website URLs.
- Facilitates enhanced EC2 instance monitoring with an included
monitoring.shscript and SSH-based automation. - Integrates with AWS CloudWatch to list metrics, retrieve granular metric data over various time periods, and manage alarms.
- Utilises a configuration file (config.ini) for customisation.
- Includes error handling to improve robustness.
- Python 3 installed on your local machine (https://www.python.org/).
- Boto3 library installed (
pip3 install boto3). - Python Fire installed (
pip3 install fire). - An AWS account with properly configured credentials (typically in
~/.aws/credentials). - An existing SSH key pair for EC2 access, placed in the same directory as the script.
-
Clone the Repository: Download the project to your local machine.
-
Configure AWS Credentials: Verify that your AWS credentials are correctly set up.
-
Customise the Configuration: Modify the
config.inifile to customise the script's behaviour. Before executing most commands, ensure you've updatedconfig.iniwith your AWS PEM key name and other desired settings. -
Getting Help: To view available commands and get help with a specific command, use the
--helpor-hflag. For example:python3 devops_1.py --help -
Deployment: To launch an EC2 instance, configure an S3 bucket, and deploy the web server, execute
python3 devops_1.pywith no arguments. -
CLI: To interact with AWS resources using the command-line interface, try some of the commands below.
AWS Resource Management
-
AWS Management Commands:
python3 devops_1.py instancespython3 devops_1.py terminate <instance_id>python3 devops_1.py terminate_allpython3 devops_1.py bucketspython3 devops_1.py delete_buckets
-
CloudWatch Commands:
python3 devops_1.py cloudwatch --helppython3 devops_1.py cloudwatch list_metrics --instance_id <instance_id>python3 devops_1.py cloudwatch get_metric_data --instance_id <instance_id> --metric_name <metric_name> [--period <1h|8h|24h>]python3 devops_1.py cloudwatch create_alarm --alarm_name <name> --metric_name <metric_name> --instance_id <instance_id> --threshold <threshold> [--comparison_operator <operator>] [--evaluation_periods <number>] [--period <seconds>] [--statistic <statistic>]python3 devops_1.py cloudwatch list_alarmspython3 devops_1.py cloudwatch delete_alarm --alarm_name <name>python3 devops_1.py cloudwatch metrics --instance_id <instance_id>
Examples
-
Retrieve an overview core metrics for an instance:
python3 devops_1.py cloudwatch metrics --instance_id i-0c86e59079b90dd57
-
Retrieve 24 hours of network traffic data:
python3 devops_1.py cloudwatch get_metric_data --instance_id i-0c86e59079b90dd57 --metric_name NetworkIn --period 24h
-
Check disk read operations over the past hour:
python3 devops_1.py cloudwatch get_metric_data --instance_id i-0c86e59079b90dd57 --metric_name DiskReadOps --period 1h
-
Create an alarm that triggers if CPU usage exceeds 90%:
python3 devops_1.py cloudwatch create_alarm --alarm_name HighCPUAlert --metric_name CPUUtilization --instance_id i-0c86e59079b90dd57 --threshold 90
To leverage the included monitoring.sh script for system monitoring on the EC2 instance:
- The
devops_1.pyscript will automatically transfermonitoring.shto the instance upon execution. - SSH into the EC2 instance using the
.pemkey file. - Change the script's permissions to make it executable:
chmod +x monitoring.sh. - Execute the script:
./monitoring.sh.