-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_infra.py
50 lines (40 loc) · 1.6 KB
/
delete_infra.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
import boto3
import configparser
from botocore.exceptions import ClientError
import pandas as pd
# Load configuration from dwh.cfg
config = configparser.ConfigParser()
config.read('dwh.cfg')
# AWS credentials and configuration
AWS_KEY = config.get('AWS', 'KEY')
AWS_SECRET = config.get('AWS', 'SECRET')
REGION = config.get('AWS', 'REGION')
# Redshift cluster configuration
CLUSTER_IDENTIFIER = config.get('CLUSTER', 'CLUSTER_IDENTIFIER')
IAM_ROLE_NAME = config.get('IAM_ROLE', 'ROLE_NAME')
# Create clients
iam = boto3.client('iam', aws_access_key_id=AWS_KEY, aws_secret_access_key=AWS_SECRET, region_name=REGION)
redshift = boto3.client('redshift', aws_access_key_id=AWS_KEY, aws_secret_access_key=AWS_SECRET, region_name=REGION)
# Delete Redshift Cluster
try:
print("Deleting Redshift Cluster")
redshift.delete_cluster(ClusterIdentifier=CLUSTER_IDENTIFIER, SkipFinalClusterSnapshot=True)
redshift.get_waiter('cluster_deleted').wait(ClusterIdentifier=CLUSTER_IDENTIFIER)
print("Redshift Cluster deleted")
except ClientError as e:
print(e)
# Detach and delete IAM Role
try:
print("Detaching policy from IAM Role")
iam.detach_role_policy(RoleName=IAM_ROLE_NAME, PolicyArn="arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess")
print("Deleting IAM Role")
iam.delete_role(RoleName=IAM_ROLE_NAME)
print("IAM Role deleted")
except ClientError as e:
print(e)
# Remove ARN and HOST entries from dwh.cfg
config.remove_option('CLUSTER', 'HOST')
config.remove_option('IAM_ROLE', 'ARN')
with open('dwh.cfg', 'w') as configfile:
config.write(configfile)
print("Infrastructure deletion complete.")