11import aws_cdk as cdk
2+ from aws_cdk import (
3+ CfnOutput ,
4+ aws_events as events ,
5+ aws_events_targets as event_target ,
6+ aws_iam as iam ,
7+ aws_scheduler as scheduler ,
8+ )
29from openchallenges .data_integration_lambda import DataIntegrationLambda
310from constructs import Construct
411
@@ -8,5 +15,66 @@ class DataIntegrationStack(cdk.Stack):
815 def __init__ (self , scope : Construct , id : str , ** kwargs ) -> None :
916 super ().__init__ (scope , id , ** kwargs )
1017
11- # Instantiate the DataIntegrationLambda construct
12- DataIntegrationLambda (self , "openchallenges-data-integration-lambda" )
18+ data_integration_lambda = DataIntegrationLambda (self , "data-integration-lambda" )
19+
20+ event_bus = events .EventBus (self , "event-bus" )
21+
22+ event_rule = events .Rule (
23+ self ,
24+ "event-rule" ,
25+ event_bus = event_bus ,
26+ event_pattern = events .EventPattern (source = ["scheduled.events" ]),
27+ )
28+
29+ # Set the event rule target to the lambda function
30+ event_rule .add_target (
31+ event_target .LambdaFunction (data_integration_lambda .lambda_function )
32+ )
33+
34+ scheduler_role = iam .Role (
35+ self ,
36+ "scheduler-role" ,
37+ assumed_by = iam .ServicePrincipal ("scheduler.amazonaws.com" ),
38+ )
39+
40+ scheduler_events_policy = iam .PolicyStatement (
41+ actions = ["events:PutEvents" ],
42+ resources = [event_bus .event_bus_arn ],
43+ effect = iam .Effect .ALLOW ,
44+ )
45+
46+ scheduler_role .add_to_policy (scheduler_events_policy )
47+
48+ # Create a group for the schedule (maybe we want to add more schedules
49+ # to this group the future)
50+ schedule_group = scheduler .CfnScheduleGroup (
51+ self ,
52+ "schedule-group" ,
53+ name = "schedule-group" ,
54+ )
55+
56+ schedule = scheduler .CfnSchedule (
57+ self ,
58+ "schedule" ,
59+ flexible_time_window = scheduler .CfnSchedule .FlexibleTimeWindowProperty (
60+ mode = "OFF" ,
61+ ),
62+ schedule_expression = "rate(5 minute)" ,
63+ group_name = schedule_group .name ,
64+ target = scheduler .CfnSchedule .TargetProperty (
65+ arn = event_bus .event_bus_arn ,
66+ role_arn = scheduler_role .role_arn ,
67+ event_bridge_parameters = scheduler .CfnSchedule .EventBridgeParametersProperty (
68+ detail_type = "ScheduleTriggered" , source = "scheduled.events"
69+ ),
70+ ),
71+ )
72+
73+ # Output
74+ CfnOutput (self , "SCHEDULE_NAME" , value = schedule .ref )
75+ CfnOutput (self , "EVENT_BUS_NAME" , value = event_bus .event_bus_name )
76+ CfnOutput (
77+ self ,
78+ "LAMBDA_FUNCTION_NAME" ,
79+ value = data_integration_lambda .lambda_function .function_name ,
80+ )
0 commit comments