-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodify_rds_instance.py
More file actions
56 lines (45 loc) · 2.07 KB
/
modify_rds_instance.py
File metadata and controls
56 lines (45 loc) · 2.07 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
import boto3
import os
import json
def modify_instance(event, context):
rds = boto3.client('rds')
s3_client = boto3.client('s3')
# Initialize variables
bucket_name = os.environ['S3_BUCKET_NAME']
dir_name = os.environ['S3_DIR_NAME']
rds_metadata_obj_name = dir_name + '/' + os.environ['RDS_METADATA_FILENAME']
message = event['Records'][0]['Sns']['Message']
for pair in message.split(','):
if 'Event Source' in pair:
event_source = str(pair.split(':')[1].strip('{').strip('}').strip('"'))
if 'Event Message' in pair:
event_message = str(pair.split(':')[1].strip('{').strip('}').strip('"'))
if 'Source ID' in pair:
db_instance_id = str(pair.split(':')[1].strip('{').strip('}').strip('"'))
if 'Restored from snapshot' not in event_message and \
'-lifecycle-snapshot-' not in event_message:
print "NOTICE: This is not the message we're looking for...exiting"
return
else:
print "NOTICE: This is the message we're looking for...continuing"
# Get the previous RDS instance parameters from S3 as recorded by the delete process
try:
j = s3_client.get_object(Bucket=bucket_name, Key=rds_metadata_obj_name)['Body'].read()
except Exception, err:
print "ERROR: Error retreiving RDS instance metadata from Bucket: %s, Key: %s: %s" % (bucket_name, rds_metadata_obj_name, err)
return
try:
rds_instances_metadata = json.loads(j)
except Exception, err:
print "ERROR: Error converting S3 text file to dictionary: %s" % err
return
db_security_groups = [sg['VpcSecurityGroupId'] for sg in rds_instances_metadata[db_instance_id]['VpcSecurityGroups']]
try:
response = rds.modify_db_instance(
ApplyImmediately=True,
DBInstanceIdentifier=db_instance_id,
VpcSecurityGroupIds=db_security_groups
)
print "NOTICE: Modified DB instance '%s'" % db_instance_id
except Exception, err:
print "ERROR: Could not modify DB instance '%s': %s" % (db_instance_id, err)