Skip to content

Commit

Permalink
feat: add event source and stateful dependency to stateless stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalenic committed Feb 9, 2024
1 parent 683c1ea commit f33526d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
3 changes: 2 additions & 1 deletion bin/orcabus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ const props: cdk.StackProps = {
const config = getEnvironmentConfig('beta');
if (!config) throw new Error('No Config');

new OrcaBusStatefulStack(app, 'OrcaBusStatefulStack', {
const statefulStack = new OrcaBusStatefulStack(app, 'OrcaBusStatefulStack', {
...config.stackProps.orcaBusStatefulConfig,
...props,
});

new OrcaBusStatelessStack(app, 'OrcaBusStatelessStack', {
statefulStack,
...config.stackProps.orcaBusStatelessConfig,
...props,
});
6 changes: 3 additions & 3 deletions config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const getEnvironmentConfig = (
schemaRegistryProps: {
...orcaBusStatefulConfig.schemaRegistryProps,
},

eventBusProps: {
...orcaBusStatefulConfig.eventBusProps,
},
Expand All @@ -98,6 +97,9 @@ export const getEnvironmentConfig = (
securityGroupProps: {
...orcaBusStatefulConfig.securityGroupProps,
},
eventSourceProps: {
buckets: ['umccr-temp-dev'],
},
},
orcaBusStatelessConfig: orcaBusStatelessConfig,
},
Expand All @@ -112,7 +114,6 @@ export const getEnvironmentConfig = (
schemaRegistryProps: {
...orcaBusStatefulConfig.schemaRegistryProps,
},

eventBusProps: {
...orcaBusStatefulConfig.eventBusProps,
},
Expand Down Expand Up @@ -141,7 +142,6 @@ export const getEnvironmentConfig = (
schemaRegistryProps: {
...orcaBusStatefulConfig.schemaRegistryProps,
},

eventBusProps: {
...orcaBusStatefulConfig.eventBusProps,
},
Expand Down
11 changes: 9 additions & 2 deletions lib/pipeline/orcabus-pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ class OrcaBusDeploymentStage extends cdk.Stage {
) {
super(scope, environmentName, { env: { account: env?.account, region: 'ap-southeast-2' } });

new OrcaBusStatefulStack(this, 'OrcaBusStatefulStack', stackProps.orcaBusStatefulConfig);
new OrcaBusStatelessStack(this, 'OrcaBusStatelessStack', stackProps.orcaBusStatelessConfig);
const statefulStack = new OrcaBusStatefulStack(
this,
'OrcaBusStatefulStack',
stackProps.orcaBusStatefulConfig
);
new OrcaBusStatelessStack(this, 'OrcaBusStatelessStack', {
statefulStack,
...stackProps.orcaBusStatelessConfig,
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/workload/orcabus-stateful-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import { EventBusConstruct, EventBusProps } from './stateful/eventbridge/compone
import { DatabaseConstruct, DatabaseProps } from './stateful/database/component';
import { SecurityGroupConstruct, SecurityGroupProps } from './stateful/securitygroup/component';
import { SchemaRegistryConstruct, SchemaRegistryProps } from './stateful/schemaregistry/component';
import { EventSource, EventSourceProps } from './stateful/event_source/component';

export interface OrcaBusStatefulConfig {
schemaRegistryProps: SchemaRegistryProps;
eventBusProps: EventBusProps;
databaseProps: DatabaseProps;
securityGroupProps: SecurityGroupProps;
eventSourceProps?: EventSourceProps;
}

export class OrcaBusStatefulStack extends cdk.Stack {
readonly eventBus: EventBusConstruct;
readonly database: DatabaseConstruct;
readonly securityGroup: SecurityGroupConstruct;
readonly schemaRegistry: SchemaRegistryConstruct;
readonly eventSource?: EventSource;

constructor(scope: Construct, id: string, props: cdk.StackProps & OrcaBusStatefulConfig) {
super(scope, id, props);
Expand Down Expand Up @@ -46,5 +49,9 @@ export class OrcaBusStatefulStack extends cdk.Stack {
'SchemaRegistryConstruct',
props.schemaRegistryProps
);

if (props.eventSourceProps) {
this.eventSource = new EventSource(this, 'EventSourceConstruct', props.eventSourceProps);
}
}
}
22 changes: 21 additions & 1 deletion lib/workload/orcabus-stateless-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Construct } from 'constructs';
import { getVpc } from './stateful/vpc/component';
import { MultiSchemaConstructProps } from './stateless/schema/component';
import { IVpc } from 'aws-cdk-lib/aws-ec2';
import { OrcaBusStatefulStack } from './orcabus-stateful-stack';

export interface OrcaBusStatelessConfig {
multiSchemaConstructProps: MultiSchemaConstructProps;
Expand All @@ -14,9 +15,28 @@ export interface OrcaBusStatelessConfig {
rdsMasterSecretName: string;
}

/**
* The stateless stack depends on the stateful stack. Note, this could be restricted further
* so that not all of the stateful stack is passed to the stateless stack. E.g. for filemanager,
* instead of passing the whole stack, it could just be the `IQueue` that filemanager depends on.
*
* See for reference:
* https://blog.serverlessadvocate.com/serverless-aws-cdk-pipeline-best-practices-patterns-part-1-ab80962f109d#1913
*/
export interface StatefulStackDependency {
/**
* The stateful stack which the stateless stack depends on.
*/
statefulStack: OrcaBusStatefulStack;
}

export class OrcaBusStatelessStack extends cdk.Stack {
private vpc: IVpc;
constructor(scope: Construct, id: string, props: cdk.StackProps & OrcaBusStatelessConfig) {
constructor(
scope: Construct,
id: string,
props: cdk.StackProps & OrcaBusStatelessConfig & StatefulStackDependency
) {
super(scope, id, props);

// --- Constructs from Stateful stack or pre-existing resources
Expand Down
19 changes: 16 additions & 3 deletions lib/workload/stateful/event_source/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import { Alarm, ComparisonOperator, MathExpression } from 'aws-cdk-lib/aws-cloud
*/
export type EventSourceProps = {
/**
* Bucket to receive events from.
* Buckets to receive events from.
*/
buckets: string[];
/**
* The types of events to capture. If not specified, captures all events. This should be from the list
* S3 EventBridge events: https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventBridge.html
*/
eventTypes?: string[];
/**
* A prefix of the objects that are matched when receiving events from the buckets.
*/
prefix?: string;
};

/**
Expand All @@ -38,6 +42,15 @@ export class EventSource extends Construct {
bucket: {
name: props.buckets,
},
...(props.prefix && {
object: {
key: [
{
prefix: props.prefix,
},
],
},
}),
},
},
});
Expand All @@ -50,7 +63,7 @@ export class EventSource extends Construct {
})
);

const rateOfMessagesReceived = new MathExpression({
const rateOfMessages = new MathExpression({
expression: 'RATE(visible + notVisible)',
usingMetrics: {
visible: this.deadLetterQueue.metricApproximateNumberOfMessagesVisible(),
Expand All @@ -59,7 +72,7 @@ export class EventSource extends Construct {
});

this.alarm = new Alarm(this, 'Alarm', {
metric: rateOfMessagesReceived,
metric: rateOfMessages,
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
threshold: 0,
evaluationPeriods: 1,
Expand Down

0 comments on commit f33526d

Please sign in to comment.