-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.py
More file actions
84 lines (67 loc) · 2.27 KB
/
Copy pathcleanup.py
File metadata and controls
84 lines (67 loc) · 2.27 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import boto3
from botocore.exceptions import ClientError
TABLE_NAME = "serverless-notes-api-notes"
ROLE_NAME = "serverless-notes-api-role"
ROLE_POLICY_NAME = "serverless-notes-api-dynamodb"
LAMBDA_NAME = "serverless-notes-api"
API_NAME = "serverless-notes-api"
def _delete_api(apigw):
apis = apigw.get_rest_apis(limit=500)["items"]
for api in apis:
if api["name"] == API_NAME:
apigw.delete_rest_api(restApiId=api["id"])
return True
return False
def _delete_lambda(lambda_client):
try:
lambda_client.delete_function(FunctionName=LAMBDA_NAME)
return True
except ClientError as exc:
if exc.response["Error"]["Code"] != "ResourceNotFoundException":
raise
return False
def _delete_role(iam):
try:
iam.delete_role_policy(RoleName=ROLE_NAME, PolicyName=ROLE_POLICY_NAME)
except ClientError as exc:
if exc.response["Error"]["Code"] != "NoSuchEntity":
raise
try:
iam.detach_role_policy(
RoleName=ROLE_NAME,
PolicyArn="arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
)
except ClientError as exc:
if exc.response["Error"]["Code"] != "NoSuchEntity":
raise
try:
iam.delete_role(RoleName=ROLE_NAME)
return True
except ClientError as exc:
if exc.response["Error"]["Code"] != "NoSuchEntity":
raise
return False
def _delete_table(dynamodb):
try:
dynamodb.meta.client.delete_table(TableName=TABLE_NAME)
return True
except ClientError as exc:
if exc.response["Error"]["Code"] != "ResourceNotFoundException":
raise
return False
def main():
session = boto3.session.Session()
apigw = session.client("apigateway")
lambda_client = session.client("lambda")
iam = session.client("iam")
dynamodb = session.resource("dynamodb")
deleted_api = _delete_api(apigw)
deleted_lambda = _delete_lambda(lambda_client)
deleted_role = _delete_role(iam)
deleted_table = _delete_table(dynamodb)
print("Deleted API:", deleted_api)
print("Deleted Lambda:", deleted_lambda)
print("Deleted IAM role:", deleted_role)
print("Deleted table:", deleted_table)
if __name__ == "__main__":
main()