Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7de81c

Browse files
Merge pull request avinashkranjan#889 from shantamsultania/master
AWS management Script
2 parents 157980f + 2e0412a commit b7de81c

File tree

8 files changed

+420
-0
lines changed

8 files changed

+420
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Aws Script for AWS Management
2+
3+
This python script can be used to manage a AWS endorsement.
4+
5+
This provides features like :
6+
7+
### EC2 :
8+
9+
1. Create your own Key based SnapShots
10+
2. Delete SnapShots
11+
3. Delete/ Terminate any EC2 instance which does not have a user/ any specific tag
12+
4. stop any useless Running Ec2 instance
13+
14+
### RDS :
15+
16+
1. delete RDS Instance
17+
2. Delete RDS Cluster
18+
3. Stop any useless Running RDS Cluster/Instance
19+
4. Delete useless Snapshots
20+
5. Delete any RDS Instance or Cluster which does not have a specific tag with it
21+
6. Delete any RDS Snapshot that is older then 2 days
22+
23+
these Scripts can directly be used with AWS lambda function for Automation purposes as well
24+
25+
## Installation
26+
27+
First of all install [python]("https://www.python.org/downloads/") on your system.
28+
```
29+
pip install boto3
30+
```
31+
32+
### Made with ❤️ by Shantam Sultania
33+
34+
You can find me at:-
35+
[Linkedin](https://www.linkedin.com/in/shantam-sultania-737084175/) or [Github](https://github.com/shantamsultania) .
36+
37+
Happy coding ❤️ .
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def lambda_handler(event, context):
2+
print("event " + str(event))
3+
print("context " + str(context))
4+
ec2_reg = boto3.client('ec2')
5+
regions = ec2_reg.describe_regions()
6+
for region in regions['Regions']:
7+
region_name = region['RegionName']
8+
instances = Ec2Instances(region_name)
9+
deleted_counts = instances.delete_snapshots(1)
10+
instances.delete_available_volumes()
11+
print("deleted_counts for region " + str(region_name) + " is " + str(deleted_counts))
12+
instances.shutdown()
13+
print("For RDS")
14+
rds = Rds(region_name)
15+
rds.cleanup_snapshot()
16+
rds.cleanup_instances()
17+
return 'Hello from Lambda'
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from datetime import datetime, timedelta, timezone
2+
3+
import boto3
4+
5+
6+
def get_delete_data(older_days):
7+
delete_time = datetime.now(tz=timezone.utc) - timedelta(days=older_days)
8+
return delete_time;
9+
10+
11+
def is_ignore_shutdown(tags):
12+
for tag in tags:
13+
print("K " + str(tag['Key']) + " is " + str(tag['Value']))
14+
if str(tag['Key']) == 'excludepower' and str(tag['Value']) == 'true':
15+
print("Not stopping K " + str(tag['Key']) + " is " + str(tag['Value']))
16+
return True
17+
return False
18+
19+
20+
def is_unassigned(tags):
21+
if 'user' not in [t['Key'] for t in tags]:
22+
return True
23+
return False
24+
25+
26+
class Ec2Instances(object):
27+
28+
def __init__(self, region):
29+
print("region " + region)
30+
31+
# if you are not using AWS Tool Kit tool you will be needing to pass your access key and secret key here
32+
33+
# client = boto3.client('rds', region_name=region_name, aws_access_key_id=aws_access_key_id,
34+
# aws_secret_access_key=aws_secret_access_key)
35+
self.ec2 = boto3.client('ec2', region_name=region)
36+
37+
def delete_snapshots(self, older_days=2):
38+
delete_snapshots_num = 0
39+
snapshots = self.get_nimesa_created_snapshots()
40+
for snapshot in snapshots['Snapshots']:
41+
fmt_start_time = snapshot['StartTime']
42+
if fmt_start_time < get_delete_data(older_days):
43+
try:
44+
self.delete_snapshot(snapshot['SnapshotId'])
45+
delete_snapshots_num + 1
46+
except Exception as e:
47+
print(e)
48+
return delete_snapshots_num
49+
50+
def get_user_created_snapshots(self):
51+
snapshots = self.ec2.describe_snapshots(
52+
Filters=[{
53+
'Name': 'owner-id', 'Values': ['your owner id'],
54+
}]) # Filters=[{'Name': 'description', 'Values': ['Created by Nimesa']}]
55+
return snapshots
56+
57+
def delete_available_volumes(self):
58+
volumes = self.ec2.describe_volumes()['Volumes']
59+
for volume in volumes:
60+
if volume['State'] == "available":
61+
self.ec2.delete_volume(VolumeId=volume['VolumeId'])
62+
63+
def delete_snapshot(self, snapshot_id):
64+
self.ec2.delete_snapshot(SnapshotId=snapshot_id)
65+
66+
def shutdown(self):
67+
instances = self.ec2.describe_instances()
68+
instance_to_stop = []
69+
instance_to_terminate = []
70+
for res in instances['Reservations']:
71+
for instance in res['Instances']:
72+
tags = instance.get('Tags')
73+
if tags is None:
74+
instance_to_terminate.append(instance['InstanceId'])
75+
continue
76+
if is_unassigned(tags):
77+
print("instance_to_terminate " + instance['InstanceId'])
78+
instance_to_terminate.append(instance['InstanceId'])
79+
if is_ignore_shutdown(tags):
80+
continue
81+
if instance['State']['Code'] == 16:
82+
instance_to_stop.append(instance['InstanceId'])
83+
84+
if any(instance_to_stop):
85+
self.ec2.stop_instances(
86+
InstanceIds=instance_to_stop
87+
)
88+
if any(instance_to_terminate):
89+
print(instance_to_terminate)
90+
self.ec2.terminate_instances(
91+
InstanceIds=instance_to_terminate
92+
)
93+
94+
95+
if __name__ == "__main__":
96+
ec2 = Ec2Instances('us-east-1')
97+
ec2.delete_snapshots(3)
98+
ec2.shutdown()
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import boto3
2+
import datetime
3+
4+
5+
class Rds(object):
6+
7+
def __init__(self, region) -> None:
8+
super().__init__()
9+
self.rds = boto3.client('rds', region)
10+
11+
def cleanup_snapshot(self):
12+
self._cleanup_snapshot_instance()
13+
self._cleanup_snapshots_clusters()
14+
15+
def cleanup_instances(self):
16+
clusters = self.rds.describe_db_clusters()
17+
for cluster in clusters['DBClusters']:
18+
self._cleanup_cluster(cluster)
19+
instances = self.rds.describe_db_instances()
20+
for instance in instances['DBInstances']:
21+
self._cleanup_instance(instance)
22+
23+
def _stop_cluster(self, identifier):
24+
self.rds.stop_db_cluster(DBClusterIdentifier=identifier)
25+
26+
def _stop_instance(self, identifier):
27+
self.rds.stop_db_instance(DBInstanceIdentifier=identifier)
28+
29+
def _delete_instance(self, identifier):
30+
self.rds.describe_db_instances(DBInstanceIdentifier=identifier)
31+
32+
def _delete_cluster(self, identifier):
33+
self.rds.describe_db_clusters(DBClusterIdentifier=identifier)
34+
35+
def _delete_instance_snapshot(self, identifier):
36+
self.rds.delete_db_snapshot(DBSnapshotIdentifier=identifier)
37+
38+
def _delete_cluster_snapshot(self, identifier):
39+
self.rds.delete_db_cluster_snapshot(DBClusterSnapshotIdentifier=identifier)
40+
41+
@staticmethod
42+
def _can_delete_instance(tags):
43+
if any('user' in tag for tag in tags):
44+
return False
45+
46+
@staticmethod
47+
def _can_stop_instance(tags):
48+
for tag in tags:
49+
if tag["Key"].lower() == 'excludepower' and tag['Value'].lower() == 'true':
50+
return False
51+
return True
52+
53+
@staticmethod
54+
def _can_delete_snapshot(tags):
55+
if tags is not None:
56+
for tag in tags:
57+
if tag['Key'].lower() == 'retain' and tag['Value'].lower() == 'true':
58+
return False
59+
return True
60+
61+
def _cleanup_instance(self, instance):
62+
identifier = instance['DBInstanceIdentifier']
63+
tags = instance['TagList']
64+
if self._can_delete_instance(tags):
65+
self._delete_instance(identifier)
66+
else:
67+
if self._can_stop_instance(tags) and instance['DBInstanceStatus'] == 'available':
68+
try:
69+
self._stop_instance(identifier)
70+
except Exception as e:
71+
print(str(e))
72+
73+
def _cleanup_cluster(self, cluster):
74+
tags = cluster['TagList']
75+
if self._can_delete_instance(tags):
76+
self._delete_cluster(cluster['DBClusterIdentifier'])
77+
else:
78+
if self._can_stop_instance(tags) and cluster['Status'] == 'available':
79+
try:
80+
self._stop_cluster(cluster['DBClusterIdentifier'])
81+
except Exception as e:
82+
print(str(e))
83+
84+
def _cleanup_snapshots_clusters(self):
85+
snapshots = self.rds.describe_db_cluster_snapshots()
86+
for snapshot in snapshots['DBClusterSnapshots']:
87+
tags = snapshot['TagList']
88+
if self._can_delete_snapshot(tags) and self._is_older_snapshot(
89+
str(snapshot['SnapshotCreateTime']).split(" ")):
90+
try:
91+
self._delete_cluster_snapshot(snapshot['DBClusterSnapshotIdentifier'])
92+
except Exception as e:
93+
print(str(e))
94+
95+
def _cleanup_snapshot_instance(self):
96+
snapshots = self.rds.describe_db_snapshots()
97+
for snapshot in snapshots['DBSnapshots']:
98+
tags = snapshot['TagList']
99+
if self._can_delete_snapshot(tags) and self._is_older_snapshot(
100+
str(snapshot['SnapshotCreateTime']).split(" ")):
101+
try:
102+
self._delete_instance_snapshot(snapshot['DBSnapshotIdentifier'])
103+
except Exception as e:
104+
print(str(e))
105+
106+
@staticmethod
107+
def _is_older_snapshot(snapshot_datetime):
108+
snapshot_date = snapshot_datetime[0].split("-")
109+
snapshot_date = datetime.date(int(snapshot_date[0]), int(snapshot_date[1]), int(snapshot_date[2]))
110+
today = datetime.date.today()
111+
if abs(today - snapshot_date).days > 2:
112+
return True
113+
else:
114+
return False
115+
116+
@staticmethod
117+
def _check_snapshot_tag(tags):
118+
flag = False
119+
for tag in tags:
120+
if tag['Key'].lower() == 'retain' and tag['Value'].lower() == 'true':
121+
flag = True
122+
if flag:
123+
return True
124+
else:
125+
return False
126+
127+
128+
if __name__ == "__main__":
129+
rds = Rds('us-east-1')
130+
# # rds.shutdown()
131+
rds.cleanup_snapshot()
132+
rds.cleanup_instances()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Aws Script for testing using moto (a AWS testing framework )
2+
3+
This python script automatically generates a set of EC2 machine's with any demo ami Id that could be used in case of testing.
4+
5+
## Installation
6+
7+
First of all install [python]("https://www.python.org/downloads/") on your system.
8+
```bash
9+
pip install moto
10+
pip install boto3
11+
pip install Flask
12+
```
13+
14+
## Results
15+
16+
![](https://i.postimg.cc/Jzx6f02c/image1.png)
17+
18+
![](https://i.postimg.cc/L6RVmRfy/image.png)
19+
20+
![](https://i.postimg.cc/Zq1r2h4x/image.png)
21+
22+
### Made with ❤️ by Shantam Sultania
23+
24+
You can find me at:-
25+
[Linkedin](https://www.linkedin.com/in/shantam-sultania-737084175/) or [Github](https://github.com/shantamsultania) .
26+
27+
Happy coding ❤️ .
28+
29+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from flask import Flask
2+
from flask_ngrok import run_with_ngrok
3+
import awstesting.awsTester as aws_testing
4+
5+
app = Flask(__name__)
6+
7+
# run_with_ngrok(app)
8+
9+
10+
@app.route("/", methods=['POST', 'GET'])
11+
def welcome():
12+
message = "Welcome to create your own test Api for AWS for Ec2 just use your Url/ec2 and your 100 ec2 " \
13+
"instance will be created in test environment if you dont want to use it in api based " \
14+
"just go to code and use awsTesting class and done "
15+
return message
16+
17+
18+
# this api is to create an ec2 for testing
19+
20+
@app.route("/ec2", methods=['POST', 'GET'])
21+
def ec2():
22+
client = aws_testing.add_service("ec2", "us-east-1")
23+
return aws_testing.test_create_ec2(client)
24+
25+
26+
# you can add more apis of your choice here like
27+
# create Volume, VPC and snapshots
28+
29+
# to do so just add a call function in awsTester class and agentMain and you are done
30+
31+
# run the app
32+
if __name__ == '__main__':
33+
app.run()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def create_ec2(client, ami_id, count):
2+
client.run_instances(ImageId=ami_id, MinCount=count, MaxCount=count)
3+
4+
5+
def create_ec2_snapshots(client, volume_id):
6+
client.create_snapshot(VolumeId=volume_id)
7+
8+
9+
def create_ec2_volume(client, AZ):
10+
ab = client.create_volume(AZ)
11+
return ab
12+
13+
14+
def create_vpc(client, cidr_block):
15+
client.create_vpc(CidrBlock=cidr_block)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /