Skip to content

Commit 1d323c6

Browse files
committed
added multiple-consumer solution in python
1 parent a4f4947 commit 1d323c6

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

python/eventbridge-mesh/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ cdk bootstrap aws://222222222222/us-east-1 \
4040
--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess
4141
```
4242

43+
3. (Optional: Only do this step, when you deploys multiple consumers solution) In account: `333333333333`, run the below command:
44+
```
45+
cdk bootstrap aws://333333333333/us-east-1 \
46+
--trust 000000000000 \
47+
--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess
48+
```
49+
4350
### Deploy Single consumer solution:
4451
1. Run: `cd single-consumer`
4552
2. Change the values of `producerAccountId` and `consumerAccountId` in `cdk.json`
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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_accounts: list[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 rules for each consumer account
24+
for index, consumer_account_id in enumerate(consumer_accounts):
25+
# Create rule to forward events to consumer account
26+
rule = events.Rule(
27+
self,
28+
f"{app_name}-forward-to-consumer-{index + 1}-rule",
29+
event_bus=producer_event_bus,
30+
event_pattern=events.EventPattern(
31+
source=['com.myapp.events']
32+
)
33+
)
34+
35+
# Add target to forward to consumer account's event bus
36+
consumer_bus = events.EventBus.from_event_bus_arn(
37+
self,
38+
f"{app_name}-consumer-{index + 1}-event-bus",
39+
f"arn:aws:events:{Stack.of(self).region}:{consumer_account_id}:event-bus/default"
40+
)
41+
rule.add_target(targets.EventBus(consumer_bus))
42+
43+
44+
class ConsumerStack(Stack):
45+
def __init__(self, scope: Construct, id: str, *,
46+
app_name: str,
47+
consumer_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_name}-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,
69+
f"{app_name}-{consumer_name}-rule",
70+
event_bus=consumer_event_bus,
71+
event_pattern=events.EventPattern(
72+
source=['com.myapp.events'],
73+
detail_type=['specific-event-type']
74+
)
75+
)
76+
77+
# Add target (e.g., CloudWatch)
78+
log_group = logs.LogGroup(
79+
self, f"{app_name}-{consumer_name}-logs"
80+
)
81+
consumer_rule.add_target(targets.CloudWatchLogGroup(log_group))
82+
83+
84+
app = App()
85+
86+
# Get context values
87+
app_name = app.node.try_get_context("appName")
88+
region = app.node.try_get_context("region")
89+
producer_account_id = app.node.try_get_context("producerAccountId")
90+
consumer1_account_id = app.node.try_get_context("consumer1AccountId")
91+
consumer2_account_id = app.node.try_get_context("consumer2AccountId")
92+
93+
# Create producer stack
94+
producer_stack = ProducerStack(
95+
app, f"{app_name}-producer-stack",
96+
app_name=app_name,
97+
consumer_accounts=[consumer1_account_id, consumer2_account_id],
98+
env=Environment(
99+
account=producer_account_id,
100+
region=region
101+
)
102+
)
103+
104+
# Create consumer 1 stack
105+
consumer1_stack = ConsumerStack(
106+
app, f"{app_name}-consumer1-stack",
107+
app_name=app_name,
108+
consumer_name="consumer1",
109+
producer_account_id=producer_account_id,
110+
env=Environment(
111+
account=consumer1_account_id,
112+
region=region
113+
)
114+
)
115+
116+
# Create consumer 2 stack
117+
consumer2_stack = ConsumerStack(
118+
app, f"{app_name}-consumer2-stack",
119+
app_name=app_name,
120+
consumer_name="consumer2",
121+
producer_account_id=producer_account_id,
122+
env=Environment(
123+
account=consumer2_account_id,
124+
region=region
125+
)
126+
)
127+
128+
app.synth()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"app": "python3 app.py",
3+
"context": {
4+
"appName": "eventbridge-mesh",
5+
"region": "us-east-1",
6+
"producerAccountId": "111111111111",
7+
"consumer1AccountId": "222222222222",
8+
"consumer2AccountId": "333333333333"
9+
}
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aws-cdk-lib==2.186.0
2+
constructs>=10.0.0,<11.0.0

0 commit comments

Comments
 (0)