Skip to content

Instantly share code, notes, and snippets.

@gimmebytes
Created July 13, 2023 08:20
Show Gist options
  • Save gimmebytes/18da4aa8369901c0e41f102963fa32ad to your computer and use it in GitHub Desktop.
Save gimmebytes/18da4aa8369901c0e41f102963fa32ad to your computer and use it in GitHub Desktop.
Incomplete example of CDK code for deploying an Eventbridge-Bus with some basic routing capabilities
import * as cdk from 'aws-cdk-lib';
import * as eventbridge from 'aws-cdk-lib/aws-events';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as iam from 'aws-cdk-lib/aws-iam';
export class EventBridgeStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create EventBridge bus
const bus = new eventbridge.EventBus(this, 'EventBus');
// Create SNS topics
const signupTopic = new sns.Topic(this, 'SignupTopic', {
displayName: 'Signup Topic',
});
const updatedTopic = new sns.Topic(this, 'UpdatedTopic', {
displayName: 'Updated Topic',
});
const deletedTopic = new sns.Topic(this, 'DeletedTopic', {
displayName: 'Deleted Topic',
});
// Add outlets to the EventBridge bus
bus.addTarget(new eventbridge.SnsTopic(signupTopic, {
message: eventbridge.RuleTargetInput.fromEventPath('$.detail.action == "signup"'),
}));
bus.addTarget(new eventbridge.SnsTopic(updatedTopic, {
message: eventbridge.RuleTargetInput.fromEventPath('$.detail.action == "updated"'),
}));
bus.addTarget(new eventbridge.SnsTopic(deletedTopic, {
message: eventbridge.RuleTargetInput.fromEventPath('$.detail.action == "deleted"'),
}));
// Grant necessary permissions to publish to the topics
const eventBridgePrincipal = new iam.ServicePrincipal('events.amazonaws.com');
signupTopic.grantPublish(eventBridgePrincipal);
updatedTopic.grantPublish(eventBridgePrincipal);
deletedTopic.grantPublish(eventBridgePrincipal);
}
}
const app = new cdk.App();
new EventBridgeStack(app, 'MyEventBridgeStack');
app.synth();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment