-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_stack.py
69 lines (63 loc) · 2.72 KB
/
scheduler_stack.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
57
58
59
60
61
62
63
64
65
66
67
68
69
from aws_cdk import (
Stack,
aws_iam as iam,
aws_scheduler as scheduler,
CfnOutput,
)
from constructs import Construct
import json
class SchedulerStack(Stack):
def __init__(
self,
scope: Construct,
id: str,
lambdafn,
**kwargs,) -> None:
super().__init__(scope, id, **kwargs)
# Service role for task-scheduler
# [NOTE] At this time (May 2023, version 2.79.1),
# CDK doesn't have a L2 EvventBridge Scheduler construct now
# (Ref: https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_scheduler/README.html)
# We have to create the Role and Policies by ourselves
role_scheduler_lambda = iam.Role(self, "InvokeLambdaSchedulerServiceRole",
description="Service role for task-scheduler",
assumed_by=iam.ServicePrincipal("scheduler.amazonaws.com"),
)
# Policy for invoking Lambda Function
policy_invoke_lambda = iam.Policy(
self, "InvokeLambdaFunctionPolicy",
statements=[
iam.PolicyStatement(
actions=[
"lambda:InvokeFunction",
],
resources=[
lambdafn.function_arn,
f"{lambdafn.function_arn}:*",
]
),
],
)
role_scheduler_lambda.attach_inline_policy(policy_invoke_lambda)
# EventBridge Scheduler to invoke send-task-lambda-function
scheduler_lambda = scheduler.CfnSchedule(
self, "task-scheduler",
description="Scheduler to invoke send-task-lambda-function",
# group_name=None, # [TODO] Has a bug now, add this feature later
flexible_time_window=scheduler.CfnSchedule.FlexibleTimeWindowProperty(mode="OFF",),
schedule_expression="at(2037-12-31T23:59:59)",
schedule_expression_timezone="Asia/Taipei",
target=scheduler.CfnSchedule.TargetProperty(
arn=lambdafn.function_arn,
role_arn=role_scheduler_lambda.role_arn,
input=json.dumps({}),
retry_policy=scheduler.CfnSchedule.RetryPolicyProperty(
maximum_event_age_in_seconds=3600, # 1 Hour
maximum_retry_attempts=3,
),
),
)
CfnOutput(self, "SchedulerRoleName", value=role_scheduler_lambda.role_name,)
CfnOutput(self, "SchedulerRoleARN", value=role_scheduler_lambda.role_arn,)
# CfnOutput(self, "SchedulerName", value=scheduler_lambda.name,) # Causes error for unknown reason
CfnOutput(self, "SchedulerARN", value=scheduler_lambda.attr_arn,)