-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(events): archive events (#12060)
Add the possibility to archive events from an Event Bus. It's also possible to archive specific events by passing an event patterns. See the [following blogpost](https://aws.amazon.com/blogs/aws/new-archive-and-replay-events-with-amazon-eventbridge/) for more information about event archiving and replay. Archiving should be possible from an imported EventBus. closes #11531 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
6 changed files
with
338 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Duration, Resource } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { IEventBus } from './event-bus'; | ||
import { EventPattern } from './event-pattern'; | ||
import { CfnArchive } from './events.generated'; | ||
|
||
/** | ||
* The event archive base properties | ||
*/ | ||
export interface BaseArchiveProps { | ||
/** | ||
* The name of the archive. | ||
* | ||
* @default - Automatically generated | ||
*/ | ||
readonly archiveName?: string; | ||
/** | ||
* A description for the archive. | ||
* | ||
* @default - none | ||
*/ | ||
readonly description?: string; | ||
/** | ||
* An event pattern to use to filter events sent to the archive. | ||
*/ | ||
readonly eventPattern: EventPattern; | ||
/** | ||
* The number of days to retain events for. Default value is 0. If set to 0, events are retained indefinitely. | ||
* @default - Infinite | ||
*/ | ||
readonly retention?: Duration; | ||
} | ||
|
||
|
||
/** | ||
* The event archive properties | ||
*/ | ||
export interface ArchiveProps extends BaseArchiveProps { | ||
/** | ||
* The event source associated with the archive. | ||
*/ | ||
readonly sourceEventBus: IEventBus; | ||
} | ||
|
||
/** | ||
* Define an EventBridge Archive | ||
* | ||
* @resource AWS::Events::Archive | ||
*/ | ||
export class Archive extends Resource { | ||
/** | ||
* The archive name. | ||
* @attribute | ||
*/ | ||
public readonly archiveName: string; | ||
|
||
/** | ||
* The ARN of the archive created. | ||
* @attribute | ||
*/ | ||
public readonly archiveArn: string; | ||
|
||
constructor(scope: Construct, id: string, props: ArchiveProps) { | ||
super(scope, id, { physicalName: props.archiveName }); | ||
|
||
let archive = new CfnArchive(this, 'Archive', { | ||
sourceArn: props.sourceEventBus.eventBusArn, | ||
description: props.description, | ||
eventPattern: props.eventPattern, | ||
retentionDays: props.retention?.toDays({ integral: true }) || 0, | ||
archiveName: this.physicalName, | ||
}); | ||
|
||
this.archiveArn = archive.attrArn; | ||
this.archiveName = archive.attrArchiveName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { expect, haveResource } from '@aws-cdk/assert'; | ||
import { Duration, Stack } from '@aws-cdk/core'; | ||
import { Test } from 'nodeunit'; | ||
import { EventBus } from '../lib'; | ||
import { Archive } from '../lib/archive'; | ||
|
||
export = { | ||
'creates an archive for an EventBus'(test: Test) { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
// WHEN | ||
let eventBus = new EventBus(stack, 'Bus'); | ||
|
||
new Archive(stack, 'Archive', { | ||
sourceEventBus: eventBus, | ||
eventPattern: { | ||
account: [stack.account], | ||
}, | ||
retention: Duration.days(10), | ||
}); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::Events::EventBus', { | ||
Name: 'Bus', | ||
})); | ||
|
||
expect(stack).to(haveResource('AWS::Events::Archive', { | ||
EventPattern: { | ||
account: [{ | ||
Ref: 'AWS::AccountId', | ||
}], | ||
}, | ||
RetentionDays: 10, | ||
SourceArn: { | ||
'Fn::GetAtt': [ | ||
'BusEA82B648', | ||
'Arn', | ||
], | ||
}, | ||
})); | ||
|
||
test.done(); | ||
}, | ||
} |
Oops, something went wrong.