This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
delete.py
51 lines (43 loc) · 1.84 KB
/
delete.py
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
import sys
import boto3
from botocore.exceptions import ClientError
from ec2 import generate_ec2_resource_names
from utils import get_configuration_value, console
def delete(deployment_name, config_json):
ec2_config = get_configuration_value(config_json)
_, stack_name, s3_bucket_name, repo_name, _ = generate_ec2_resource_names(
deployment_name
)
cf_client = boto3.client("cloudformation", ec2_config["region"])
console.print(f"Delete CloudFormation Stack [b]{stack_name}[/b]")
cf_client.delete_stack(StackName=stack_name)
# delete ecr repository
ecr_client = boto3.client("ecr", ec2_config["region"])
try:
console.print(f"Delete ECR repo [b]{repo_name}[/b]")
ecr_client.delete_repository(repositoryName=repo_name, force=True)
except ClientError as e:
# raise error, if the repo can't be found
if e.response and e.response["Error"]["Code"] != "RepositoryNotFoundException":
raise e
# delete s3 bucket
s3_client = boto3.client("s3", ec2_config["region"])
s3 = boto3.resource("s3")
try:
console.print(f"Delete S3 bucket [b]{s3_bucket_name}[/b]")
s3.Bucket(s3_bucket_name).objects.all().delete()
s3_client.delete_bucket(Bucket=s3_bucket_name)
except ClientError as e:
if e.response and e.response["Error"]["Code"] != "NoSuchBucket":
# If there is no bucket, we just let it silently fail, don't have to do
# any thing
raise e
if __name__ == "__main__":
if len(sys.argv) != 3:
raise Exception(
"Please provide deployment_name and configuration json as parameters"
)
deployment_name = sys.argv[1]
config_json = sys.argv[2] if sys.argv[2] else "ec2_config.json"
delete(deployment_name, config_json)
console.print(f"[bold green]Deleted {deployment_name}")