This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user registration) : create event rule and attach it to SNS targ…
…et + eventSchema (#110) * user registration event rule and attach it to SNS target * tie DLQ name to eventName * user-registration event schema * update user events dlq
- Loading branch information
Showing
8 changed files
with
214 additions
and
13 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
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,8 @@ | ||
import { config as globalConfig } from '../../config'; | ||
|
||
export const eventConfig = { | ||
name: 'UserRegistration', | ||
source: 'web-repo', | ||
detailType: ['User Registration'], | ||
bus: globalConfig.sharedEventBusName, | ||
}; |
116 changes: 116 additions & 0 deletions
116
.aws/src/event-rules/user-registration/userRegistrationEventRule.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,116 @@ | ||
import { Construct } from 'constructs'; | ||
import { Resource } from 'cdktf'; | ||
import { | ||
PocketEventBridgeProps, | ||
PocketEventBridgeRuleWithMultipleTargets, | ||
ApplicationEventBus, | ||
PocketPagerDuty, | ||
} from '@pocket-tools/terraform-modules'; | ||
import { config } from '../../config'; | ||
import { iam, sns, sqs } from '@cdktf/provider-aws'; | ||
import { eventConfig } from './eventConfig'; | ||
import { createDeadLetterQueueAlarm } from '../utils'; | ||
import * as NullProviders from '@cdktf/provider-null'; | ||
|
||
export class UserRegistrationEventRule extends Resource { | ||
public readonly snsTopic: sns.SnsTopic; | ||
public readonly snsTopicDlq: sqs.SqsQueue; | ||
|
||
constructor( | ||
scope: Construct, | ||
private name: string, | ||
private pagerDuty: PocketPagerDuty | ||
) { | ||
super(scope, name); | ||
|
||
this.snsTopic = new sns.SnsTopic(this, 'user-registration-topic', { | ||
name: `${config.prefix}-UserRegistrationTopic`, | ||
lifecycle: { | ||
preventDestroy: true, | ||
}, | ||
}); | ||
|
||
this.snsTopicDlq = new sqs.SqsQueue(this, 'sns-topic-dql', { | ||
name: `${config.prefix}-SNS-${eventConfig.name}-Topic--DLQ`, | ||
tags: config.tags, | ||
}); | ||
|
||
const userRegistrationEvent = this.createUserRegistrationEventRules(); | ||
this.createPolicyForEventBridgeToSns(); | ||
|
||
//todo: set DLQ alert after the chores ticket | ||
|
||
//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 NullProviders.Resource(this, 'null-resource', { | ||
dependsOn: [userRegistrationEvent.getEventBridge().rule, this.snsTopic], | ||
}); | ||
} | ||
|
||
/** | ||
* Rolls out event bridge rule and attaches them to sns target | ||
* for user-registration-events | ||
* @private | ||
*/ | ||
private createUserRegistrationEventRules() { | ||
const userRegistrationEventRuleProps: PocketEventBridgeProps = { | ||
eventRule: { | ||
name: `${config.prefix}-${eventConfig.name}-Rule`, | ||
eventPattern: { | ||
source: [eventConfig.source], | ||
'detail-type': eventConfig.detailType, | ||
}, | ||
eventBusName: eventConfig.bus, | ||
preventDestroy: true, | ||
}, | ||
targets: [ | ||
{ | ||
arn: this.snsTopic.arn, | ||
deadLetterArn: this.snsTopicDlq.arn, | ||
targetId: `${config.prefix}-User-Registration-Event-SNS-Target`, | ||
terraformResource: this.snsTopic, | ||
}, | ||
], | ||
}; | ||
return new PocketEventBridgeRuleWithMultipleTargets( | ||
this, | ||
`${config.prefix}-User-Registration-EventBridge-Rule`, | ||
userRegistrationEventRuleProps | ||
); | ||
} | ||
|
||
private createPolicyForEventBridgeToSns() { | ||
const eventBridgeSnsPolicy = new iam.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 sns.SnsTopicPolicy( | ||
this, | ||
'user-registration-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* user registration event is emitted in web repo when | ||
* user initiates a password recovery | ||
*/ | ||
import { Resource } from 'cdktf'; | ||
import { Construct } from 'constructs'; | ||
import { eventbridgeschemas } from '@cdktf/provider-aws'; | ||
import { SCHEMA_REGISTRY, SCHEMA_TYPE } from './types'; | ||
import { SchemasSchemaConfig } from '@cdktf/provider-aws/lib/eventbridgeschemas/schemas-schema'; | ||
|
||
export class UserRegistrationEventSchema extends Resource { | ||
public readonly userRegistrationEvent: string = 'User-Registration-Event'; | ||
|
||
constructor(scope: Construct, private name: string) { | ||
super(scope, name); | ||
this.createUserRegistrationEventSchema(); | ||
} | ||
|
||
private createUserRegistrationEventSchema() { | ||
const schemaProps: SchemasSchemaConfig = { | ||
name: this.userRegistrationEvent, | ||
description: `emitted when pocket user is registered (e.g signup)`, | ||
type: SCHEMA_TYPE, | ||
registryName: SCHEMA_REGISTRY, | ||
content: JSON.stringify(this.getUserRegistrationPayload()), | ||
}; | ||
const schema = new eventbridgeschemas.SchemasSchema( | ||
this, | ||
`${this.userRegistrationEvent}-Schema`, | ||
schemaProps | ||
); | ||
|
||
return schema; | ||
} | ||
|
||
/*** | ||
* Schema scaffold from the aws console | ||
*/ | ||
private getUserRegistrationPayload() { | ||
return { | ||
openapi: '3.0.0', | ||
info: { | ||
version: '1.0.0', | ||
title: 'Event', | ||
}, | ||
paths: {}, | ||
components: { | ||
schemas: { | ||
Event: { | ||
type: 'object', | ||
required: ['encodedUserId', 'locale', 'userId', 'email'], | ||
properties: { | ||
email: { | ||
type: 'string', | ||
}, | ||
encodedUserId: { | ||
type: 'string', | ||
}, | ||
locale: { | ||
type: 'string', | ||
}, | ||
userId: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
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