-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate data integration code to L2 constructs
- Loading branch information
1 parent
7d2765f
commit 635444f
Showing
4 changed files
with
44 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |