-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2_controller.py
More file actions
65 lines (51 loc) · 1.86 KB
/
Copy pathec2_controller.py
File metadata and controls
65 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import boto3
import json
from datetime import datetime
# ================================
# EC2 Controller
# Author: Sadhvi - Cloud Engineer
# ================================
REGION = 'ca-central-1'
def get_all_instances():
ec2 = boto3.client('ec2', region_name=REGION)
response = ec2.describe_instances()
instances = []
for reservation in response['Reservations']:
for instance in reservation['Instances']:
instances.append({
'id': instance['InstanceId'],
'state': instance['State']['Name'],
'type': instance['InstanceType']
})
return instances
def start_instance(instance_id):
ec2 = boto3.client('ec2', region_name=REGION)
print(f"Starting instance: {instance_id}")
ec2.start_instances(InstanceIds=[instance_id])
print(f"Instance {instance_id} is starting...")
def stop_instance(instance_id):
ec2 = boto3.client('ec2', region_name=REGION)
print(f"Stopping instance: {instance_id}")
ec2.stop_instances(InstanceIds=[instance_id])
print(f"Instance {instance_id} is stopping...")
def check_and_control():
print("AWS EC2 Controller Starting...")
print(f"Time: {datetime.now()}")
instances = get_all_instances()
if not instances:
print("No EC2 instances found in ca-central-1")
return
print(f"\nFound {len(instances)} instances:")
for instance in instances:
print(f" ID: {instance['id']} | State: {instance['state']} | Type: {instance['type']}")
# Save report
report = {
'generated_at': str(datetime.now()),
'total_instances': len(instances),
'instances': instances
}
with open('ec2_report.json', 'w') as f:
json.dump(report, f, indent=4)
print("\nReport saved to ec2_report.json")
if __name__ == "__main__":
check_and_control()