diff --git a/app.py b/app.py index 67d6f12..d207739 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import aws_cdk as cdk +from aws_cdk.aws_scheduler_alpha import ScheduleExpression from openchallenges.bucket_stack import BucketStack from openchallenges.network_stack import NetworkStack @@ -8,6 +9,7 @@ from openchallenges.load_balancer_stack import LoadBalancerStack from openchallenges.service_props import ServiceProps, ContainerVolume from openchallenges.data_integration_stack import DataIntegrationStack +from openchallenges.data_integration_props import DataIntegrationProps import openchallenges.utils as utils app = cdk.App() @@ -329,8 +331,17 @@ app, f"{stack_name_prefix}-load-balancer", network_stack.vpc ) +data_integration_props = DataIntegrationProps( + schedule=ScheduleExpression.cron( + minute="*/5", + hour="*", + day="*", + month="*", + time_zone=cdk.TimeZone.AMERICA_LOS_ANGELES, + ) +) data_integration_stack = DataIntegrationStack( - app, f"{stack_name_prefix}-data-integration" + app, f"{stack_name_prefix}-data-integration", data_integration_props ) api_docs_props = ServiceProps( diff --git a/openchallenges/data_integration_props.py b/openchallenges/data_integration_props.py new file mode 100644 index 0000000..90f6e71 --- /dev/null +++ b/openchallenges/data_integration_props.py @@ -0,0 +1,13 @@ +from aws_cdk.aws_scheduler_alpha import ScheduleExpression + + +class DataIntegrationProps: + """ + Data integration properties + """ + + def __init__( + self, + schedule: ScheduleExpression, + ) -> None: + self.schedule = schedule diff --git a/openchallenges/data_integration_stack.py b/openchallenges/data_integration_stack.py index f91a030..e950ca5 100644 --- a/openchallenges/data_integration_stack.py +++ b/openchallenges/data_integration_stack.py @@ -1,80 +1,40 @@ import aws_cdk as cdk from aws_cdk import ( - CfnOutput, - aws_events as events, - aws_events_targets as event_target, - aws_iam as iam, - aws_scheduler as scheduler, + aws_scheduler_alpha as scheduler_alpha, + aws_scheduler_targets_alpha as scheduler_targets, ) from openchallenges.data_integration_lambda import DataIntegrationLambda +from openchallenges.data_integration_props import DataIntegrationProps from constructs import Construct class DataIntegrationStack(cdk.Stack): - def __init__(self, scope: Construct, id: str, **kwargs) -> None: + def __init__( + self, scope: Construct, id: str, props: DataIntegrationProps, **kwargs + ) -> None: super().__init__(scope, id, **kwargs) data_integration_lambda = DataIntegrationLambda(self, "data-integration-lambda") - event_bus = events.EventBus(self, "event-bus") - - event_rule = events.Rule( - self, - "event-rule", - event_bus=event_bus, - event_pattern=events.EventPattern(source=["scheduled.events"]), - ) - - # Set the event rule target to the lambda function - event_rule.add_target( - event_target.LambdaFunction(data_integration_lambda.lambda_function) - ) - - scheduler_role = iam.Role( - self, - "scheduler-role", - assumed_by=iam.ServicePrincipal("scheduler.amazonaws.com"), + target = scheduler_targets.LambdaInvoke( + data_integration_lambda.lambda_function, + input=scheduler_alpha.ScheduleTargetInput.from_object({}), ) - scheduler_events_policy = iam.PolicyStatement( - actions=["events:PutEvents"], - resources=[event_bus.event_bus_arn], - effect=iam.Effect.ALLOW, - ) - - scheduler_role.add_to_policy(scheduler_events_policy) - # Create a group for the schedule (maybe we want to add more schedules # to this group the future) - schedule_group = scheduler.CfnScheduleGroup( + schedule_group = scheduler_alpha.Group( self, - "schedule-group", - name="schedule-group", + "group", + group_name="schedule-group", ) - schedule = scheduler.CfnSchedule( + scheduler_alpha.Schedule( self, "schedule", - flexible_time_window=scheduler.CfnSchedule.FlexibleTimeWindowProperty( - mode="OFF", - ), - schedule_expression="rate(5 minutes)", - group_name=schedule_group.name, - target=scheduler.CfnSchedule.TargetProperty( - arn=event_bus.event_bus_arn, - role_arn=scheduler_role.role_arn, - event_bridge_parameters=scheduler.CfnSchedule.EventBridgeParametersProperty( - detail_type="ScheduleTriggered", source="scheduled.events" - ), - ), - ) - - # Output - CfnOutput(self, "SCHEDULE_NAME", value=schedule.ref) - CfnOutput(self, "EVENT_BUS_NAME", value=event_bus.event_bus_name) - CfnOutput( - self, - "LAMBDA_FUNCTION_NAME", - value=data_integration_lambda.lambda_function.function_name, + schedule=props.schedule, + target=target, + group=schedule_group, + description="This is a cron-based schedule that will run every 5 minutes", ) diff --git a/requirements.txt b/requirements.txt index 92c10ed..b1c51a6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ aws-cdk-lib==2.139.0 +aws-cdk.aws-scheduler-alpha==2.139.0a0 +aws-cdk.aws-scheduler-targets-alpha==2.139.0a0 constructs>=10.0.0,<11.0.0 boto3>=1.34.1