Skip to content

Commit 05be0c8

Browse files
committed
created single producer and consumer solution
1 parent 53704ad commit 05be0c8

File tree

4 files changed

+106
-29
lines changed

4 files changed

+106
-29
lines changed
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
#!/usr/bin/env node
22
import * as cdk from 'aws-cdk-lib';
3-
import { EventbridgeMeshStack } from '../lib/eventbridge-mesh-stack';
3+
import { consumerStack } from './single-consumer-stack';
4+
import { producerStack } from './single-producer-stack';
45

56
const app = new cdk.App();
6-
new EventbridgeMeshStack(app, 'EventbridgeMeshStack', {
7-
/* If you don't specify 'env', this stack will be environment-agnostic.
8-
* Account/Region-dependent features and context lookups will not work,
9-
* but a single synthesized template can be deployed anywhere. */
107

11-
/* Uncomment the next line to specialize this stack for the AWS Account
12-
* and Region that are implied by the current CLI configuration. */
13-
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
8+
const region = 'us-east-1'
9+
const producerAccountId = '111111111111';
10+
const consumerAccountId = '222222222222';
1411

15-
/* Uncomment the next line if you know exactly what Account and Region you
16-
* want to deploy the stack to. */
17-
// env: { account: '123456789012', region: 'us-east-1' },
12+
new producerStack(app, 'producerStack', {
13+
env: {
14+
account: producerAccountId,
15+
region: region,
16+
},
17+
consumerAccountId,
18+
});
1819

19-
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
20-
});
20+
new consumerStack(app, 'consumerStack', {
21+
env: {
22+
account: consumerAccountId,
23+
region: region,
24+
},
25+
producerAccountId,
26+
});

typescript/eventbridge-mesh/lib/eventbridge-mesh-stack.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { EventBus, EventBusPolicy, Rule } from 'aws-cdk-lib/aws-events';
3+
import { CloudWatchLogGroup } from 'aws-cdk-lib/aws-events-targets';
4+
import { AccountPrincipal, Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
5+
import { LogGroup } from 'aws-cdk-lib/aws-logs';
6+
import { Construct } from 'constructs';
7+
8+
export interface consumerStackProps extends cdk.StackProps {
9+
readonly producerAccountId: string;
10+
}
11+
12+
export class consumerStack extends cdk.Stack {
13+
constructor(scope: Construct, id: string, props: consumerStackProps) {
14+
super(scope, id, props);
15+
16+
// Create or reference the consumer event bus
17+
const consumerEventBus = new EventBus(this, 'ConsumerEventBus');
18+
19+
// Add policy to allow producer account to put events
20+
consumerEventBus.addToResourcePolicy(new PolicyStatement({
21+
sid: 'allowProducerAccount',
22+
effect: Effect.ALLOW,
23+
principals: [new AccountPrincipal(props.producerAccountId)],
24+
actions: ['events:PutEvents'],
25+
resources: [consumerEventBus.eventBusArn]
26+
}));
27+
28+
// Create consumer rules
29+
const consumerRule = new Rule(this, 'ConsumerRule', {
30+
eventBus: consumerEventBus,
31+
eventPattern: {
32+
// Define more specific filtering here
33+
source: ['com.myapp.events'],
34+
detail: {
35+
type: ['specific-event-type']
36+
}
37+
}
38+
});
39+
40+
// Add target (e.g., CloudWatch)
41+
consumerRule.addTarget(new CloudWatchLogGroup(
42+
new LogGroup(this, 'ConsumerLogs')
43+
));
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import * as targets from 'aws-cdk-lib/aws-events-targets';
3+
import { EventBus, Rule } from 'aws-cdk-lib/aws-events';
4+
import { LogGroup } from 'aws-cdk-lib/aws-logs';
5+
import { Construct } from 'constructs';
6+
7+
export interface producerStackProps extends cdk.StackProps {
8+
readonly consumerAccountId: string;
9+
}
10+
11+
export class producerStack extends cdk.Stack {
12+
constructor(scope: Construct, id: string, props: producerStackProps) {
13+
super(scope, id, props);
14+
15+
// Create the EventBus
16+
const producerEventBus = new EventBus(this, 'ProducerEventBus');
17+
18+
// Create rule to forward events to consumer account
19+
const rule = new Rule(this, 'ForwardToConsumerRule', {
20+
eventBus: producerEventBus,
21+
eventPattern: {
22+
// Define your event pattern here
23+
source: ['com.myapp.events'],
24+
},
25+
});
26+
27+
// Add target to forward to consumer account's event bus
28+
rule.addTarget(new targets.EventBus(
29+
EventBus.fromEventBusArn(
30+
this,
31+
'ConsumerEventBus',
32+
`arn:aws:events:${cdk.Stack.of(this).region}:${props.consumerAccountId}:event-bus/default`
33+
)
34+
));
35+
36+
// Optional: Add CloudWatch target for monitoring
37+
rule.addTarget(new targets.CloudWatchLogGroup(
38+
new LogGroup(this, 'producerLogs')
39+
));
40+
}
41+
}
42+

0 commit comments

Comments
 (0)