-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (48 loc) · 1.59 KB
/
app.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from aws_cdk import core
import os
from aws_cdk import (
aws_s3 as s3,
aws_lambda as _lambda,
aws_events as events,
aws_events_targets as targets,
aws_iam as iam,
core
)
class R53BackupStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# S3 bucket for backups
bucket = s3.Bucket(self, "R53BackupBucket")
# Lambda function for R53 backups
lambda_function_name='r53_backup'
lambda_fn = _lambda.Function(
self, "R53BackupFunction",
runtime=_lambda.Runtime.PYTHON_3_9,
handler=f"index.lambda_handler",
code=_lambda.Code.asset(f'lambda/{lambda_function_name}'), # Adjust path as needed
environment={
'BUCKET_NAME': bucket.bucket_name
},
timeout = core.Duration.minutes(5)
)
# IAM role permissions
bucket.grant_put(lambda_fn)
lambda_fn.add_to_role_policy(iam.PolicyStatement(
actions=["route53:ListHostedZones", "route53:ListResourceRecordSets"],
resources=["*"]
))
# EventBridge rule to trigger Lambda every day at 1:00 AM UTC
rule = events.Rule(
self, "Rule",
schedule=events.Schedule.cron(
minute='0',
hour='1',
month='*',
year='*',
day='*'
)
)
rule.add_target(targets.LambdaFunction(lambda_fn))
app = core.App()
R53BackupStack(app, "R53BackupStack")
app.synth()