Skip to content

Commit

Permalink
Implemented SequenceRunManager app deployment
Browse files Browse the repository at this point in the history
* Added Shared API Gateway as stateful resource
* Introduced param constants that go across stateful & stateless
* Rearranged Python dependencies into deps directory
* Removed no longer used constants
* Removed now deprecated API Gateway Alpha constructs
* Updated CDK nag validation accordingly

Resolves #139
  • Loading branch information
victorskl committed Mar 15, 2024
1 parent 187a5b6 commit 18c0fae
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 175 deletions.
4 changes: 1 addition & 3 deletions config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
FilemanagerDependencies,
OrcaBusStatelessConfig,
} from '../lib/workload/orcabus-stateless-stack';
import { Duration, aws_lambda, RemovalPolicy } from 'aws-cdk-lib';
import { Duration, RemovalPolicy } from 'aws-cdk-lib';
import { EventSourceProps } from '../lib/workload/stateful/event_source/component';
import { DbAuthType } from '../lib/workload/stateless/postgres_manager/function/type';

Expand Down Expand Up @@ -68,8 +68,6 @@ const orcaBusStatelessConfig = {
},
eventBusName: eventBusName,
lambdaSecurityGroupName: lambdaSecurityGroupName,
lambdaRuntimePythonVersion: aws_lambda.Runtime.PYTHON_3_10,
bclConvertFunctionName: 'orcabus_bcl_convert',
rdsMasterSecretName: rdsMasterSecretName,
postgresManagerConfig: {
masterSecretName: rdsMasterSecretName,
Expand Down
3 changes: 3 additions & 0 deletions config/param.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// environmental neutral; global constants for SSM parameter store coordinate that share the
// contract between stateful and stateless workloads. These constants typically be ssm param.
export const SHARED_HTTP_API_ID: string = '/orcabus/shared-http-api-id';
5 changes: 4 additions & 1 deletion lib/workload/orcabus-stateful-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { getVpc } from './stateful/vpc/component';
import { EventBusConstruct, EventBusProps } from './stateful/eventbridge/component';
import { Database, ConfigurableDatabaseProps } from './stateful/database/component';
import { ConfigurableDatabaseProps, Database } 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 { SharedApiGatewayConstruct } from './stateful/apigw/component';

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

new SharedApiGatewayConstruct(this, 'OrcaBusSharedApiGatewayConstruct');
}
}
38 changes: 29 additions & 9 deletions lib/workload/orcabus-stateless-stack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cdk from 'aws-cdk-lib';
import { Arn, aws_lambda } from 'aws-cdk-lib';
import { Arn, aws_ssm } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { getVpc } from './stateful/vpc/component';
import { MultiSchemaConstructProps } from './stateless/schema/component';
Expand All @@ -11,13 +11,15 @@ import {
PostgresManagerStack,
PostgresManagerConfig,
} from './stateless/postgres_manager/deploy/postgres-manager-stack';
import { SequenceRunManagerStack } from './stateless/sequence_run_manager/deploy/component';
import { EventBus, IEventBus } from 'aws-cdk-lib/aws-events';
import * as param from '../../config/param';
import { IHttpApi, HttpApiAttributes, HttpStage, HttpApi } from 'aws-cdk-lib/aws-apigatewayv2';

export interface OrcaBusStatelessConfig {
multiSchemaConstructProps: MultiSchemaConstructProps;
eventBusName: string;
lambdaSecurityGroupName: string;
lambdaRuntimePythonVersion: aws_lambda.Runtime;
bclConvertFunctionName: string;
rdsMasterSecretName: string;
postgresManagerConfig: PostgresManagerConfig;
filemanagerDependencies?: FilemanagerDependencies;
Expand All @@ -39,8 +41,10 @@ export interface FilemanagerDependencies {
}

export class OrcaBusStatelessStack extends cdk.Stack {
private vpc: IVpc;
private lambdaSecurityGroup: ISecurityGroup;
private readonly vpc: IVpc;
private readonly lambdaSecurityGroup: ISecurityGroup;
private readonly mainBus: IEventBus;
private readonly sharedHttpApi: IHttpApi;

// microservice stacks
microserviceStackArray: cdk.Stack[] = [];
Expand All @@ -59,14 +63,26 @@ export class OrcaBusStatelessStack extends cdk.Stack {
this.vpc
);

// const mainBus = EventBus.fromEventBusName(this, 'OrcaBusMain', props.eventBusName);
this.mainBus = EventBus.fromEventBusName(this, 'OrcaBusMain', props.eventBusName);

// You may reuse the shared HttpApi Gateway if you prefer. Doing so, please use the namespace
// in the route path prefix. You can pass along `this.sharedHttpApi` through your stack props.
const sharedHttpApiId = aws_ssm.StringParameter.valueFromLookup(this, param.SHARED_HTTP_API_ID);
const sharedHttpApiAttributes: HttpApiAttributes = { httpApiId: sharedHttpApiId };
this.sharedHttpApi = HttpApi.fromHttpApiAttributes(
this,
'OrcaBusSharedHttpApi',
sharedHttpApiAttributes
);
new HttpStage(this, 'OrcaBusSharedHttpApiStage', { httpApi: this.sharedHttpApi });

// --- Create Stateless resources

// new MultiSchemaConstruct(this, 'MultiSchema', props.multiSchemaConstructProps);

// hook microservice construct components here
this.createSequenceRunManager();

this.microserviceStackArray.push(this.createSequenceRunManager());
this.microserviceStackArray.push(this.createPostgresManager(props.postgresManagerConfig));

if (props.filemanagerDependencies) {
Expand All @@ -78,8 +94,12 @@ export class OrcaBusStatelessStack extends cdk.Stack {
}

private createSequenceRunManager() {
// TODO new SequenceRunManagerConstruct() from lib/workload/stateless/sequence_run_manager/deploy/component.ts
// However, the implementation is still incomplete...
return new SequenceRunManagerStack(this, 'SequenceRunManager', {
securityGroups: [this.lambdaSecurityGroup],
vpc: this.vpc,
mainBus: this.mainBus,
httpApi: this.sharedHttpApi,
});
}

private createPostgresManager(config: PostgresManagerConfig) {
Expand Down
42 changes: 42 additions & 0 deletions lib/workload/stateful/apigw/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Construct } from 'constructs';
import { aws_ssm, Duration } from 'aws-cdk-lib';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import { HttpUserPoolAuthorizer } from 'aws-cdk-lib/aws-apigatewayv2-authorizers';
import { SHARED_HTTP_API_ID } from '../../../../config/param';
import { CorsHttpMethod, HttpApi } from 'aws-cdk-lib/aws-apigatewayv2';

export class SharedApiGatewayConstruct extends Construct {
private readonly SSM_USER_POOL_ID: string = '/data_portal/client/cog_user_pool_id'; // FIXME one fine day in future

constructor(scope: Construct, id: string) {
super(scope, id);

const userPoolId: string = aws_ssm.StringParameter.valueFromLookup(this, this.SSM_USER_POOL_ID);
const userPool = cognito.UserPool.fromUserPoolId(this, id, userPoolId);

const httpApi = new HttpApi(this, id + 'HttpApi', {
apiName: 'OrcaBusSharedAPI',
corsPreflight: {
allowHeaders: ['Authorization'],
allowMethods: [
CorsHttpMethod.GET,
CorsHttpMethod.HEAD,
CorsHttpMethod.OPTIONS,
CorsHttpMethod.POST,
],
allowOrigins: ['*'], // FIXME allowed origins from config constant
maxAge: Duration.days(10),
},
defaultAuthorizer: new HttpUserPoolAuthorizer(id + 'HttpUserPoolAuthorizer', userPool),
// defaultDomainMapping: ... TODO
});

new aws_ssm.StringParameter(this, 'sharedHttpApiIdParameter', {
description: 'OrcaBus Shared API Gateway httpApiId',
parameterName: SHARED_HTTP_API_ID,
stringValue: httpApi.httpApiId,
});

// TODO setup cloud map service discovery perhaps
}
}
6 changes: 3 additions & 3 deletions lib/workload/stateless/sequence_run_manager/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.PHONY: test suite

install:
@pip install -r requirements-dev.txt
@pip install -r deps/requirements-dev.txt

check: lint

lint:
@black -t py312 --check ./src --exclude skel --exclude .venv
@black -t py312 --check . --exclude .venv

lint-fix:
@black -t py312 ./src --exclude skel --exclude .venv
@black -t py312 . --exclude .venv

# full mock suite test pipeline - install deps, bring up compose stack, run suite, bring down compose stack
test: install up suite down
Expand Down
4 changes: 2 additions & 2 deletions lib/workload/stateless/sequence_run_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ _*If you are PyCharmer and opening the whole `orcabus` project (i.e. not doing s

- Setup Python environment (conda or venv)
```
conda create -n orcabus python=3.12
conda activate orcabus
conda create -n sequence_run_manager python=3.12
conda activate sequence_run_manager
```

### Make
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ This directory contains CDK code constructs that will be called and assembled by

You may have multi-level directory structure under this folder as see fit to arrange your CDK constructs.

However. Collectively, all CDK constructs created under this deploy directory will form as **one deployable component unit** for the higher level CDK Stack. Hence, just single `component.ts` file might be sufficed if your app deployment is a simpler CDK deployment construction.
However. Collectively, all CDK constructs created under this deploy directory will form as **one deployable stack** for the higher level CDK Stack. Hence, just single `stack-name.ts` file might be sufficed if your app deployment is a simpler CDK deployment construction.
Loading

0 comments on commit 18c0fae

Please sign in to comment.