Commit bd0bf28c authored by Nagarjuna D N's avatar Nagarjuna D N
Browse files

feature

parent b372732c
# Lambda IAM Role Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"ec2:StartInstances",
"ec2:StopInstances"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"redshift:CreateClusterSnapshot",
"redshift:CreateCluster",
"redshift:DeleteCluster",
"redshift:DeleteClusterSnapshot",
"redshift:RestoreFromClusterSnapshot",
"redshift:Describe*"
],
"Resource": "*"
}
]
}
import boto3
import datetime
ec2_client = boto3.client('ec2','ap-south-1')
redshift_client = boto3.client('redshift','ap-south-1')
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['i-0f73XXXXX']
##Enter your redshift cluster name
CLUSTER_NAME="lambdatest"
#Append with current date to create redshift snapshot
TODAY = datetime.datetime.now()
CURRENT_DATE = TODAY.strftime('%d-%m-%Y')
NEW_SNAPSHOT_NAME=CLUSTER_NAME+"-snap-"+CURRENT_DATE
#days old to delete
DELETE_DAY= TODAY - datetime.timedelta(days=4)
DELETE_DATE=DELETE_DAY.strftime('%d-%m-%Y')
OLD_SNAPSHOT_DELETE=CLUSTER_NAME+"-snap-"+DELETE_DATE
def lambda_handler(event, context):
# shutting down EC2...
try:
ec2_stop=ec2_client.stop_instances(
InstanceIds=instances
)
print 'stopped instance(s): ' + str(instances)
except Exception as e:
print "%s" % e.message
# shutting down redshift...
#aws redshift delete-cluster --cluster-identifier mycluster --final-cluster-snapshot-identifier my-snapshot-id
try:
delete_redshift_take_snapshot = redshift_client.delete_cluster(
ClusterIdentifier=CLUSTER_NAME,
SkipFinalClusterSnapshot=False,
FinalClusterSnapshotIdentifier=NEW_SNAPSHOT_NAME
)
print "\"%s\" cluster is deleted by taking the new snapshot \"%s\"" %(CLUSTER_NAME,NEW_SNAPSHOT_NAME)
except Exception as e:
print "%s" % e.message
try:
##delete older than some days
delete_old_redshift_snapshot = redshift_client.delete_cluster_snapshot(
SnapshotIdentifier=OLD_SNAPSHOT_DELETE,
SnapshotClusterIdentifier=CLUSTER_NAME
)
print " \"%s\" snapshot is deleted for the cluster \"%s\"" %(OLD_SNAPSHOT_DELETE,CLUSTER_NAME)
except Exception as e:
print "%s" % e.message
\ No newline at end of file
import boto3
import datetime
ec2_client = boto3.client('ec2', 'ap-south-1')
redshift_client = boto3.client('redshift','ap-south-1')
# Enter the instances to start: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['i-0f7316XXX']
#Latest snapshot date to be considered
TODAY = datetime.datetime.now()
LAST_DAY= TODAY - datetime.timedelta(days=0)
LATEST_SNAP_DATE=LAST_DAY.strftime('%d-%m-%Y')
#Enter the redshift cluster name and the latest snapshot to restore from
CLUSTER_NAME="lambdatest"
LATEST_SNAPSHOT_NAME=CLUSTER_NAME+"-snap-"+LATEST_SNAP_DATE
def lambda_handler(event, context):
# starting up EC2...
try:
ec2_start=ec2_client.start_instances(
InstanceIds=instances
)
print 'started instance(s): ' + str(instances)
except Exception as e:
print "%s" % e.message
# Starting up redshift...
#aws redshift restore-from-cluster-snapshot --cluster-identifier mycluster --snapshot-identifier my-snapshot-id
try:
restored_cluster = redshift_client.restore_from_cluster_snapshot(
ClusterIdentifier=CLUSTER_NAME,
SnapshotIdentifier=LATEST_SNAPSHOT_NAME,
)
print " \"%s\" Cluster is restored from the latest snapshot \"%s\"" %(CLUSTER_NAME,LATEST_SNAPSHOT_NAME)
except Exception as e:
print "%s" % e.message
......@@ -9,7 +9,7 @@ import datetime
SOURCE_REGION = 'ap-south-1'
client = boto3.client('elasticache',region_name=SOURCE_REGION)
s3bucket = 'elasticache-backup-mumbai'
s3bucket = 'ec-backup-mumbai'
#To sort out the snapshots date matches todays date to copy
today = datetime.datetime.now()
......
# Lambda IAM Role Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Action": [
"elasticache:Describe*",
"elasticache:CopySnapshot",
"elasticache:AddTagsToResource",
"elasticache:CreateSnapshot",
"elasticache:DeleteSnapshot",
"elasticache:ListTagsForResource"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*",
"S3:Put*"
],
"Resource": [
"arn:aws:s3:::ec-backup-mumbai",
"arn:aws:s3:::ec-backup-mumbai/*"
]
}
]
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment