Skip to content

Commit

Permalink
feat(aws-events): configure schema discovery on event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
rrhodes committed Jan 4, 2025
1 parent dd6a2f4 commit 8e57a94
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/aws-cdk-lib/aws-events/lib/event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { Construct } from 'constructs';
import { Archive, BaseArchiveProps } from './archive';
import { CfnEventBus, CfnEventBusPolicy } from './events.generated';
import * as eventschemas from '../../aws-eventschemas';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as sqs from '../../aws-sqs';
import { ArnFormat, IResource, Lazy, Names, Resource, Stack, Token } from '../../core';

/**
* Properties to configure schema discovery on an event bus
*/
export interface SchemaDiscoveryProps {
/**
* Allows for the discovery of the event schemas that are sent to the event bus from another account
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eventschemas-discoverer.html#cfn-eventschemas-discoverer-crossaccount
* @default - none
*/
readonly crossAccount: boolean;

/**
* A description for the discoverer
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eventschemas-discoverer.html#cfn-eventschemas-discoverer-description
* @default - none
*/
readonly description: string;
};

/**
* Interface which all EventBus based classes MUST implement
*/
Expand Down Expand Up @@ -50,6 +72,13 @@ export interface IEventBus extends IResource {
*/
archive(id: string, props: BaseArchiveProps): Archive;

/**
* Create an EventBridge schema discoverer to automatically generate schemas based on events on this event bus.
*
* @param props Properties of the schema discoverer
*/
schemaDiscovery(id: string, props?: SchemaDiscoveryProps): eventschemas.CfnDiscoverer;

/**
* Grants an IAM Principal to send custom events to the eventBus
* so that they can be matched to rules.
Expand Down Expand Up @@ -175,6 +204,14 @@ abstract class EventBusBase extends Resource implements IEventBus {
});
}

public schemaDiscovery(id: string, props?: SchemaDiscoveryProps): eventschemas.CfnDiscoverer {
return new eventschemas.CfnDiscoverer(this, id, {

Check warning on line 208 in packages/aws-cdk-lib/aws-events/lib/event-bus.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk-lib/aws-events/lib/event-bus.ts#L207-L208

Added lines #L207 - L208 were not covered by tests
crossAccount: props?.crossAccount ?? true,
description: props?.description || `Schema Discoverer for ${this.eventBusName} Event Bus`,
sourceArn: this.eventBusArn,
});
}

public grantPutEventsTo(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
Expand Down
126 changes: 126 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/event-bus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,132 @@ describe('event bus', () => {
});
});

test('can be configured for schema discovery with default properties', () => {
// GIVEN
const stack = new Stack();

// WHEN
const event = new EventBus(stack, 'Bus');

event.schemaDiscovery('MySchemaDiscoverer');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Events::EventBus', {
Name: 'Bus',
});

Template.fromStack(stack).hasResourceProperties('AWS::EventSchemas::Discoverer', {
SourceArn: {
'Fn::GetAtt': [
'BusEA82B648',
'Arn',
],
},
Description: {
'Fn::Join': [
'',
[
'Schema Discoverer for ',
{
Ref: 'BusEA82B648',
},
' Event Bus',
],
],
},
});
});

test('can be configured for schema discovery with custom properties', () => {
// GIVEN
const stack = new Stack();

// WHEN
const event = new EventBus(stack, 'Bus');

event.schemaDiscovery('MySchemaDiscoverer', {
crossAccount: false,
description: 'A custom schema discovery description.',
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Events::EventBus', {
Name: 'Bus',
});

Template.fromStack(stack).hasResourceProperties('AWS::EventSchemas::Discoverer', {
SourceArn: {
'Fn::GetAtt': [
'BusEA82B648',
'Arn',
],
},
Description: 'A custom schema discovery description.',
CrossAccount: false,
});
});

test('can be configured for schema discovery from an imported EventBus', () => {
// GIVEN
const stack = new Stack();

// WHEN
const bus = new EventBus(stack, 'Bus');

const importedBus = EventBus.fromEventBusArn(stack, 'ImportedBus', bus.eventBusArn);

importedBus.schemaDiscovery('MySchemaDiscoverer');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Events::EventBus', {
Name: 'Bus',
});

Template.fromStack(stack).hasResourceProperties('AWS::EventSchemas::Discoverer', {
SourceArn: {
'Fn::GetAtt': [
'BusEA82B648',
'Arn',
],
},
Description: {
'Fn::Join': [
'',
[
'Schema Discoverer for ',
{
'Fn::Select': [
1,
{
'Fn::Split': [
'/',
{
'Fn::Select': [
5,
{
'Fn::Split': [
':',
{
'Fn::GetAtt': [
'BusEA82B648',
'Arn',
],
},
],
},
],
},
],
},
],
},
' Event Bus',
],
],
},
});
});

test('cross account event bus uses generated physical name', () => {
// GIVEN
const app = new App();
Expand Down

0 comments on commit 8e57a94

Please sign in to comment.