-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search events): implement search event emitter and consumer (#666)
* feat(search events): initial scaffolding * feat(search/snowplow): search event emitter and consumer Emit search result events from user-list-search for corpus searches, to the event bus. Add consumer to shared snowplow event consumer. [POCKET-10238] * chore: fix snowplow tests and update snowtype * chore: fixes for lint and typescript Removed the unnecessary snowplow codgen -- will need to be repeated if they are regenerated unless the version becomes compatible with the new typescript?
- Loading branch information
1 parent
dbaa24b
commit f16a902
Showing
37 changed files
with
4,449 additions
and
1,956 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
infrastructure/pocket-event-bridge/src/event-rules/search-api-events/eventConfig.ts
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,7 @@ | ||
export const eventConfig = { | ||
PocketSearch: { | ||
name: 'SearchApiEvents', | ||
source: 'search-api-events', | ||
detailType: ['corpus_search_result'], | ||
}, | ||
}; |
132 changes: 132 additions & 0 deletions
132
...structure/pocket-event-bridge/src/event-rules/search-api-events/pocketSearchEventRules.ts
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,132 @@ | ||
import { Construct } from 'constructs'; | ||
import { | ||
PocketEventBridgeProps, | ||
PocketEventBridgeRuleWithMultipleTargets, | ||
ApplicationEventBus, | ||
} from '@pocket-tools/terraform-modules'; | ||
import { config } from '../../config'; | ||
import { | ||
sqsQueue, | ||
snsTopic, | ||
dataAwsIamPolicyDocument, | ||
snsTopicPolicy, | ||
dataAwsSnsTopic, | ||
} from '@cdktf/provider-aws'; | ||
import { resource } from '@cdktf/provider-null'; | ||
import { eventConfig } from './eventConfig'; | ||
import { createDeadLetterQueueAlarm } from '../utils'; | ||
|
||
export class SearchApiEvents extends Construct { | ||
public readonly snsTopic: snsTopic.SnsTopic; | ||
public readonly snsTopicDlq: sqsQueue.SqsQueue; | ||
|
||
constructor( | ||
scope: Construct, | ||
private name: string, | ||
private sharedEventBus: ApplicationEventBus, | ||
private snsAlarmTopic: dataAwsSnsTopic.DataAwsSnsTopic, | ||
) { | ||
super(scope, name); | ||
|
||
this.snsTopic = new snsTopic.SnsTopic(this, 'search-api-event-topic', { | ||
name: `${config.prefix}-SearchApiEventTopic`, | ||
lifecycle: { | ||
preventDestroy: true, | ||
}, | ||
}); | ||
|
||
this.snsTopicDlq = new sqsQueue.SqsQueue(this, 'sns-topic-dql', { | ||
name: `${config.prefix}-SNS-${eventConfig.PocketSearch.name}-Topic-DLQ`, | ||
tags: config.tags, | ||
}); | ||
|
||
const searchEvent = this.createSearchApiEventRules(); | ||
this.createPolicyForEventBridgeToSns(); | ||
|
||
//get alerted if we get 10 messages in DLQ in 4 evaluation period of 5 minutes (for shareable-list) | ||
createDeadLetterQueueAlarm( | ||
this, | ||
snsAlarmTopic, | ||
this.snsTopicDlq.name, | ||
`${eventConfig.PocketSearch.name}-Rule-dlq-alarm`, | ||
true, | ||
4, | ||
300, | ||
10, | ||
); | ||
|
||
//place-holder resource used to make sure we are not | ||
//removing the event-rule or the SNS by mistake | ||
//if the resources are removed, this would act as an additional check | ||
//to prevent resource deletion in-addition to preventDestroy | ||
//e.g removing any of the dependsOn resource and running npm build would | ||
//throw error | ||
new resource.Resource(this, 'null-resource', { | ||
dependsOn: [searchEvent.getEventBridge().rule, this.snsTopic], | ||
}); | ||
} | ||
|
||
/** | ||
* Rolls out event bridge rule and attaches them to sns target | ||
* for searchapi-events | ||
* @private | ||
*/ | ||
private createSearchApiEventRules() { | ||
const searchEventRuleProps: PocketEventBridgeProps = { | ||
eventRule: { | ||
name: `${config.prefix}-${eventConfig.PocketSearch.name}-Rule`, | ||
eventPattern: { | ||
source: [eventConfig.PocketSearch.source], | ||
'detail-type': eventConfig.PocketSearch.detailType, | ||
}, | ||
eventBusName: this.sharedEventBus.bus.name, | ||
preventDestroy: true, | ||
}, | ||
targets: [ | ||
{ | ||
arn: this.snsTopic.arn, | ||
deadLetterArn: this.snsTopicDlq.arn, | ||
targetId: `${config.prefix}-SearchApi-Event-SNS-Target`, | ||
terraformResource: this.snsTopic, | ||
}, | ||
], | ||
}; | ||
return new PocketEventBridgeRuleWithMultipleTargets( | ||
this, | ||
`${config.prefix}-SearchApi-EventBridge-Rule`, | ||
searchEventRuleProps, | ||
); | ||
} | ||
|
||
private createPolicyForEventBridgeToSns() { | ||
const eventBridgeSnsPolicy = | ||
new dataAwsIamPolicyDocument.DataAwsIamPolicyDocument( | ||
this, | ||
`${config.prefix}-EventBridge-SNS-Policy`, | ||
{ | ||
statement: [ | ||
{ | ||
effect: 'Allow', | ||
actions: ['sns:Publish'], | ||
resources: [this.snsTopic.arn], | ||
principals: [ | ||
{ | ||
identifiers: ['events.amazonaws.com'], | ||
type: 'Service', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
).json; | ||
|
||
return new snsTopicPolicy.SnsTopicPolicy( | ||
this, | ||
'searchapi-events-sns-topic-policy', | ||
{ | ||
arn: this.snsTopic.arn, | ||
policy: eventBridgeSnsPolicy, | ||
}, | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.
f16a902
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plan Result (user-list-search-cdk)
CI link
This plan contains resource delete operation. Please check the plan result very carefully!
Change Result (Click me)
f16a902
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plan Result (shared-snowplow-consumer-cdk)
CI link
Change Result (Click me)
f16a902
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plan Result (pocket-event-bridge-cdk)
CI link
Change Result (Click me)