This project is an AWS automation script built using Python, boto3, and AWS CLI.
It allows you to:
List all EC2 instances in your AWS account
View their current running state
Start an EC2 instance
Stop an EC2 instance
AWS SDK for Python (boto3)
AWS CLI executed through Python
This reflects how real cloud engineers automate infrastructure instead of relying on the AWS Console.
Python 3
AWS SDK for Python (boto3)
AWS CLI
Bash commands executed via Python subprocess
IAM permissions for EC2 control
Python runs AWS CLI commands to list EC2 instances
Instance IDs and states are displayed
User selects whether to start or stop an instance
boto3 sends the start or stop request to AWS EC2
Check if running in AwsConsole:
- AWS Configuration
REGION = "eu-north-1"
INSTANCE_ID = "ENTER_INSTANCE_ID"
Region must match where your EC2 instance exists
Instance ID must start with:
i-xxxxxxxxxxxxxxxxx
- Listing EC2 Instances
def list_ec2():
subprocess.run(
"bash -c 'aws ec2 describe-instances --query Reservations[].Instances[].InstanceId --output text'",
shell=True
)
subprocess.run(
"bash -c 'aws ec2 describe-instances --query Reservations[].Instances[].State.Name --output text'",
shell=True
)
This uses the AWS CLI to:
Retrieve all EC2 instance IDs
Retrieve their current states
The --output text option removes JSON brackets and makes output readable.
- Starting an Instance
def start_instance():
ec2.start_instances(InstanceIds=[INSTANCE_ID])
This calls the AWS EC2 API:
StartInstances
- Stopping an Instance
def stop_instance():
ec2.stop_instances(InstanceIds=[INSTANCE_ID])
This calls:
StopInstances
Compute billing stops, but EBS storage costs remain.
- Menu-Based CLI Interface
print("1 - Start instance")
print("2 - Stop instance")
User input determines which EC2 API call is executed.
Example Output
list of Ec2 instances
i-0a1b2c3d4e5f67890
i-09182736455443321
running
stopped
EC2 CONTROL
1 - Start instance
2 - Stop instance
Choose option (1 or 2):
- Python installed python --version
- Install dependencies
pip install boto3
- AWS CLI installed
aws --version
- Configure AWS credentials
aws configure
You must configure:
Access Key ID
Secret Access Key
Default region
Output format (json or text)
Credentials are stored securely in:
~/.aws/credentials
The AWS user or role must have the following permissions:
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:StartInstances",
"ec2:StopInstances"
],
"Resource": "*"
}
Without these permissions the script will fail.
EC2 lifecycle states
Difference between start and create
boto3 client usage
AWS CLI integration
IAM permissions
Region-specific resources
Infrastructure automation
Starting an EC2 instance incurs cost
Stopping EC2 does not delete it
EBS volumes continue to incur storage charges
Terminated instances cannot be restarted
Python automation
AWS SDK usage
CLI-to-Python integration
Cloud cost awareness
Infrastructure-as-code mindset
DevOps-style tooling
Possible Improvements
Future enhancements could include:
Automatic instance ID detection
State validation before start/stop
Display instance name tags
Tag-based instance selection
Lambda-based automation
Scheduled auto shutdown
Telegram or email notifications
Siv, Learning cloud engineering through hands-on AWS automation.



