| 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() |