Skip to content

Commit

Permalink
refactor: avoid making the stateless stack depend on the stateful sta…
Browse files Browse the repository at this point in the history
…ck directly and use ARNs instead
  • Loading branch information
mmalenic committed Feb 12, 2024
1 parent 4c2f7dc commit 5f0181e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bin/orcabus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const statefulStack = new OrcaBusStatefulStack(app, 'OrcaBusStatefulStack', {
});

new OrcaBusStatelessStack(app, 'OrcaBusStatelessStack', {
statefulStack,
eventSourceDependency: statefulStack.intoEventSourceDependency(),
...config.stackProps.orcaBusStatelessConfig,
...props,
});
2 changes: 1 addition & 1 deletion lib/pipeline/orcabus-pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class OrcaBusDeploymentStage extends cdk.Stage {
stackProps.orcaBusStatefulConfig
);
new OrcaBusStatelessStack(this, 'OrcaBusStatelessStack', {
statefulStack,
eventSourceDependency: statefulStack.intoEventSourceDependency(),
...stackProps.orcaBusStatelessConfig,
});
}
Expand Down
12 changes: 12 additions & 0 deletions lib/workload/orcabus-stateful-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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';
import { EventSourceDependency } from './orcabus-stateless-stack';

export interface OrcaBusStatefulConfig {
schemaRegistryProps: SchemaRegistryProps;
Expand Down Expand Up @@ -54,4 +55,15 @@ export class OrcaBusStatefulStack extends cdk.Stack {
this.eventSource = new EventSource(this, 'EventSourceConstruct', props.eventSourceProps);
}
}

intoEventSourceDependency(): EventSourceDependency | undefined {
if (!this.eventSource) {
return;
}

return {
queueArn: this.eventSource.queueArn,
deadLetterQueueArn: this.eventSource.deadLetterQueueArn,
};
}
}
26 changes: 17 additions & 9 deletions lib/workload/orcabus-stateless-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -16,18 +15,27 @@ export interface OrcaBusStatelessConfig {
}

/**
* 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
* Components from the stateful event source.
*/
export type EventSourceDependency = {
/**
* The SQS queue ARN.
*/
queueArn: string;
/**
* The dead letter queue ARN.
*/
deadLetterQueueArn: string;
};

/**
* Properties from the stateful stack.
*/
export interface StatefulStackDependency {
/**
* The stateful stack which the stateless stack depends on.
* Event source prop
*/
statefulStack: OrcaBusStatefulStack;
eventSourceDependency?: EventSourceDependency;
}

export class OrcaBusStatelessStack extends cdk.Stack {
Expand Down
23 changes: 16 additions & 7 deletions lib/workload/stateful/event_source/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Alarm, ComparisonOperator, MathExpression } from 'aws-cdk-lib/aws-cloud
/**
* Properties for defining an S3 EventBridge rule.
*/
export type EventSourceRule = {
export type EventSourceProps = {
/**
* Bucket to receive events from. If not specified, captures events from all buckets.
*/
Expand All @@ -22,12 +22,7 @@ export type EventSourceRule = {
* A prefix of the objects that are matched when receiving events from the buckets.
*/
prefix?: string;
};

/**
* Props for the `EventSource` construct. An array of rules.
*/
export type EventSourceProps = EventSourceRule[];
}[];

/**
* A construct that defines an SQS S3 event source, along with a DLQ and CloudWatch alarms.
Expand Down Expand Up @@ -91,4 +86,18 @@ export class EventSource extends Construct {
alarmDescription: 'An event has been received in the dead letter queue.',
});
}

/**
* Get the SQS queue ARN.
*/
get queueArn(): string {
return this.queue.queueArn;
}

/**
* Get the dead letter queue ARN.
*/
get deadLetterQueueArn(): string {
return this.deadLetterQueue.queueArn;
}
}

0 comments on commit 5f0181e

Please sign in to comment.