Create a gist now

Instantly share code, notes, and snippets.

import json
import boto3
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logger = logging.getLogger()
CONFIG = {'hgmigueldb-replica1': 'i-847d9502'}
def get_source_id(message):
if 'SourceId' in message:
return message['SourceId']
else:
sourceId = message['Identifier Link']
return sourceId.split('SourceId:')[1].strip()
def lambda_handler(event, context):
message = json.loads(event['Records'][0]['Sns']['Message'])
sourceId = message['Identifier Link']
logger.debug("Source id %s", get_source_id(message))
source_id = get_source_id(message)
if not source_id in CONFIG:
return {'status': 300, 'action': 'none', 'message': 'No source id in CONFIG'}
if not 'RDS-EVENT-0014' in message['Event ID']:
return {'status': 301, 'action': 'none', 'message': 'No RDS-EVENT-0014'}
try:
client = boto3.client('ec2')
client.start_instances(InstanceIds=[CONFIG[get_source_id(message)]])
return {'status': 200, 'action': 'STARTING:' + CONFIG[source_id]}
except Exception as err:
raise Exception("Could not restore: %s" % e)
import datetime
import json
import boto3
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logger = logging.getLogger()
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return int(mktime(obj.timetuple()))
return json.JSONEncoder.default(self, obj)
def lambda_handler(event, context):
message = json.loads(event['Records'][0]['Sns']['Message'])
database = message['Trigger']['Dimensions'][0]['value']
logger.debug(database)
client = boto3.client(service_name='rds',region_name="us-east-1")
try:
response = client.describe_db_instances(DBInstanceIdentifier='hgmigueldb-replica1')
response = client.modify_db_instance(
DBInstanceIdentifier='hgmigueldb-replica1',
DBInstanceClass='db.t2.small',
ApplyImmediately=True)
logger.debug(response)
return json.dumps(response, cls = DateTimeEncoder)
except Exception as e:
raise Exception("Could not restore: %s" % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment