Last active
January 8, 2020 11:54
-
-
Save tkuchiki/87c3b7b1169d1e21ab21 to your computer and use it in GitHub Desktop.
AWS Lambda で RDS の snapshot を取る(ついでに指定日時過ぎたら削除する)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"logs:CreateLogGroup", | |
"logs:CreateLogStream", | |
"logs:PutLogEvents" | |
], | |
"Resource": "arn:aws:logs:*:*:*" | |
}, | |
{ | |
"Action": [ | |
"rds:CreateDBSnapshot", | |
"rds:DeleteDBSnapshot", | |
"rds:DescribeDBSnapshots" | |
], | |
"Effect": "Allow", | |
"Resource": "*" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import time | |
from botocore.client import ClientError | |
from datetime import datetime, timedelta, tzinfo | |
rds = boto3.client('rds') | |
class JST(tzinfo): | |
def utcoffset(self, dt): | |
return timedelta(hours=9) | |
def dst(self, dt): | |
return timedelta(0) | |
def tzname(self, dt): | |
return 'JST' | |
def create_snapshot(prefix, instanceid): | |
snapshotid = "-".join([prefix, datetime.now(tz=JST()).strftime("%Y-%m-%dT%H%M")]) | |
for i in range(0, 5): | |
try: | |
snapshot = rds.create_db_snapshot( | |
DBSnapshotIdentifier=snapshotid, | |
DBInstanceIdentifier=instanceid | |
) | |
return | |
except ClientError as e: | |
print(str(e)) | |
time.sleep(1) | |
def delete_snapshots(prefix, days): | |
snapshots = rds.describe_db_snapshots() | |
now = datetime.utcnow().replace(tzinfo=None) | |
for snapshot in snapshots['DBSnapshots']: | |
# creating snapshot | |
if not snapshot.has_key('SnapshotCreateTime'): | |
continue | |
delta = now - snapshot['SnapshotCreateTime'].replace(tzinfo=None) | |
if snapshot['DBSnapshotIdentifier'].startswith(prefix) and delta.days >= days: | |
rds.delete_db_snapshot(DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']) | |
def lambda_handler(event, context): | |
delete_days = 1 | |
snapshot_prefix = "manually-snapshot-xxx" | |
instances = ["xxx-db"] | |
for instance in instances: | |
create_snapshot(snapshot_prefix, instance) | |
delete_snapshots(snapshot_prefix, delete_days) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import time | |
from botocore.client import ClientError | |
from datetime import datetime, timedelta, tzinfo | |
rds = boto3.client('rds') | |
class JST(tzinfo): | |
def utcoffset(self, dt): | |
return timedelta(hours=9) | |
def dst(self, dt): | |
return timedelta(0) | |
def tzname(self, dt): | |
return 'JST' | |
def create_snapshot(prefix, clusterid): | |
snapshotid = "-".join([prefix, clusterid, datetime.now(tz=JST()).strftime("%Y-%m-%dT%H%M")]) | |
for i in range(0, 5): | |
try: | |
snapshot = rds.create_db_cluster_snapshot( | |
DBClusterSnapshotIdentifier=snapshotid, | |
DBClusterIdentifier=clusterid | |
) | |
return | |
except ClientError as e: | |
print(str(e)) | |
time.sleep(1) | |
def delete_snapshots(prefix, days): | |
snapshots = rds.describe_db_cluster_snapshots() | |
now = datetime.utcnow().replace(tzinfo=None) | |
for snapshot in snapshots['DBClusterSnapshots']: | |
# creating snapshot | |
if not snapshot.has_key('SnapshotCreateTime'): | |
continue | |
delta = now - snapshot['SnapshotCreateTime'].replace(tzinfo=None) | |
if snapshot['DBClusterSnapshotIdentifier'].startswith(prefix) and delta.days >= days: | |
rds.delete_db_cluster_snapshot(DBClusterSnapshotIdentifier=snapshot['DBClusterSnapshotIdentifier']) | |
def lambda_handler(event, context): | |
delete_days = 1 | |
snapshot_prefix = "manually-snapshot-xxx" | |
clusters = ["xxx-db"] | |
for cluster in clusters: | |
create_snapshot(snapshot_prefix, cluster) | |
delete_snapshots(snapshot_prefix, delete_days) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment