|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from aws_cdk import ( |
| 3 | + aws_events as events, |
| 4 | + aws_events_targets as targets, |
| 5 | + aws_logs as logs, |
| 6 | + aws_iam as iam, |
| 7 | + App, Stack, Environment |
| 8 | +) |
| 9 | +from constructs import Construct |
| 10 | + |
| 11 | +class ProducerStack(Stack): |
| 12 | + def __init__(self, scope: Construct, id: str, *, |
| 13 | + app_name: str, |
| 14 | + consumer_account_id: str, |
| 15 | + **kwargs) -> None: |
| 16 | + super().__init__(scope, id, **kwargs) |
| 17 | + |
| 18 | + # Create the EventBus |
| 19 | + producer_event_bus = events.EventBus( |
| 20 | + self, f"{app_name}-producer-event-bus" |
| 21 | + ) |
| 22 | + |
| 23 | + # Create rule to forward events to consumer account |
| 24 | + rule = events.Rule( |
| 25 | + self, f"{app_name}-forward-to-consumer-rule", |
| 26 | + event_bus=producer_event_bus, |
| 27 | + event_pattern=events.EventPattern( |
| 28 | + source=['com.myapp.events'] |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | + # Add target to forward to consumer account's event bus |
| 33 | + consumer_bus = events.EventBus.from_event_bus_arn( |
| 34 | + self, |
| 35 | + 'ConsumerEventBus', |
| 36 | + f"arn:aws:events:{Stack.of(self).region}:{consumer_account_id}:event-bus/default" |
| 37 | + ) |
| 38 | + rule.add_target(targets.EventBus(consumer_bus)) |
| 39 | + |
| 40 | + # Optional: Add CloudWatch target for monitoring |
| 41 | + log_group = logs.LogGroup(self, f"{app_name}-producer-logs") |
| 42 | + rule.add_target(targets.CloudWatchLogGroup(log_group)) |
| 43 | + |
| 44 | + |
| 45 | +class ConsumerStack(Stack): |
| 46 | + def __init__(self, scope: Construct, id: str, *, |
| 47 | + app_name: str, |
| 48 | + producer_account_id: str, |
| 49 | + **kwargs) -> None: |
| 50 | + super().__init__(scope, id, **kwargs) |
| 51 | + |
| 52 | + # Create or reference the consumer event bus |
| 53 | + consumer_event_bus = events.EventBus( |
| 54 | + self, f"{app_name}-consumer-event-bus" |
| 55 | + ) |
| 56 | + |
| 57 | + # Add policy to allow producer account to put events |
| 58 | + consumer_event_bus.add_to_resource_policy(iam.PolicyStatement( |
| 59 | + sid="allowProducerAccount", |
| 60 | + effect=iam.Effect.ALLOW, |
| 61 | + principals=[iam.AccountPrincipal(producer_account_id)], |
| 62 | + actions=["events:PutEvents"], |
| 63 | + resources=[consumer_event_bus.event_bus_arn] |
| 64 | + )) |
| 65 | + |
| 66 | + # Create consumer rules |
| 67 | + consumer_rule = events.Rule( |
| 68 | + self, f"{app_name}-consumer-rule", |
| 69 | + event_bus=consumer_event_bus, |
| 70 | + event_pattern=events.EventPattern( |
| 71 | + source=['com.myapp.events'], |
| 72 | + detail_type=['specific-event-type'] |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + # Add target (e.g., CloudWatch) |
| 77 | + log_group = logs.LogGroup(self, f"{app_name}-consumer-logs") |
| 78 | + consumer_rule.add_target(targets.CloudWatchLogGroup(log_group)) |
| 79 | + |
| 80 | + |
| 81 | +app = App() |
| 82 | + |
| 83 | +# Get context values |
| 84 | +app_name = app.node.try_get_context("appName") |
| 85 | +region = app.node.try_get_context("region") |
| 86 | +producer_account_id = app.node.try_get_context("producerAccountId") |
| 87 | +consumer_account_id = app.node.try_get_context("consumer1AccountId") |
| 88 | + |
| 89 | +# Create producer stack |
| 90 | +producer_stack = ProducerStack( |
| 91 | + app, f"{app_name}-producer-stack", |
| 92 | + app_name=app_name, |
| 93 | + consumer_account_id=consumer_account_id, |
| 94 | + env=Environment( |
| 95 | + account=producer_account_id, |
| 96 | + region=region |
| 97 | + ) |
| 98 | +) |
| 99 | + |
| 100 | +# Create consumer stack |
| 101 | +consumer_stack = ConsumerStack( |
| 102 | + app, f"{app_name}-consumer-stack", |
| 103 | + app_name=app_name, |
| 104 | + producer_account_id=producer_account_id, |
| 105 | + env=Environment( |
| 106 | + account=consumer_account_id, |
| 107 | + region=region |
| 108 | + ) |
| 109 | +) |
| 110 | + |
| 111 | +app.synth() |
0 commit comments