-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
36 lines (28 loc) · 1.17 KB
/
index.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
from datetime import UTC, datetime
import boto3
from aws_lambda_powertools.utilities.data_classes import (
EventBridgeEvent,
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from botocore.exceptions import ClientError
@event_source(data_class=EventBridgeEvent)
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> None:
# Create RDS and S3 clients
rds = boto3.client("rds")
db = event["db-name"]
now = datetime.now(tz=UTC)
date = now.strftime("%d-%m-%Y")
try:
# Take snapshot of RDS database
print("Trying to create db snapshot")
snapshot_name = "db-snapshot-" + date
rds.create_db_cluster_snapshot(DBClusterSnapshotIdentifier=snapshot_name, DBClusterIdentifier=db)
print("Snapshot creating")
# create a waiter for the DB cluster snapshot to be available
waiter = rds.get_waiter("db_cluster_snapshot_available")
# wait for the DB cluster snapshot to be available
waiter.wait(DBClusterSnapshotIdentifier=snapshot_name)
print(f"DB cluster snapshot {snapshot_name} is now available.")
except ClientError as e:
print(e)