Skip to content

Commit

Permalink
Migrate data integration code to L2 constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaffter committed Dec 6, 2024
1 parent 7d2765f commit 635444f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 58 deletions.
13 changes: 12 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions openchallenges/data_integration_props.py
Original file line number Diff line number Diff line change
@@ -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
74 changes: 17 additions & 57 deletions openchallenges/data_integration_stack.py
Original file line number Diff line number Diff line change
@@ -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",
)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 635444f

Please sign in to comment.