Skip to content

Commit

Permalink
Refactored the API Gateway into SequenceRunManager service
Browse files Browse the repository at this point in the history
Related #151
  • Loading branch information
victorskl committed Mar 18, 2024
1 parent 18c0fae commit 813cdd7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 45 deletions.
3 changes: 0 additions & 3 deletions config/param.ts

This file was deleted.

3 changes: 0 additions & 3 deletions lib/workload/orcabus-stateful-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ConfigurableDatabaseProps, Database } from './stateful/database/compone
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 @@ -56,7 +55,5 @@ export class OrcaBusStatefulStack extends cdk.Stack {
if (props.eventSourceProps) {
this.eventSource = new EventSource(this, 'EventSourceConstruct', props.eventSourceProps);
}

new SharedApiGatewayConstruct(this, 'OrcaBusSharedApiGatewayConstruct');
}
}
22 changes: 4 additions & 18 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_ssm } from 'aws-cdk-lib';
import { Arn } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { getVpc } from './stateful/vpc/component';
import { MultiSchemaConstructProps } from './stateless/schema/component';
Expand All @@ -13,8 +13,6 @@ import {
} 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;
Expand Down Expand Up @@ -44,7 +42,6 @@ export class OrcaBusStatelessStack extends cdk.Stack {
private readonly vpc: IVpc;
private readonly lambdaSecurityGroup: ISecurityGroup;
private readonly mainBus: IEventBus;
private readonly sharedHttpApi: IHttpApi;

// microservice stacks
microserviceStackArray: cdk.Stack[] = [];
Expand All @@ -65,24 +62,13 @@ export class OrcaBusStatelessStack extends cdk.Stack {

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.microserviceStackArray.push(this.createSequenceRunManager());
this.microserviceStackArray.push(this.createSequenceRunManager(props));
this.microserviceStackArray.push(this.createPostgresManager(props.postgresManagerConfig));

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

private createSequenceRunManager() {
private createSequenceRunManager(props: cdk.StackProps) {
return new SequenceRunManagerStack(this, 'SequenceRunManager', {
securityGroups: [this.lambdaSecurityGroup],
vpc: this.vpc,
mainBus: this.mainBus,
httpApi: this.sharedHttpApi,
...props,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ 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';
import { IStringParameter } from 'aws-cdk-lib/aws-ssm';

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

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 userPoolParam: IStringParameter = aws_ssm.StringParameter.fromStringParameterName(this, id + 'SSMStringParameter', this.SSM_USER_POOL_ID);
const userPool = cognito.UserPool.fromUserPoolId(scope, id + 'UserPool', userPoolParam.stringValue);

const httpApi = new HttpApi(this, id + 'HttpApi', {
apiName: 'OrcaBusSharedAPI',
this._httpApi = new HttpApi(this, id + 'HttpApi', {
apiName: 'OrcaBus SequenceRunManager API',
corsPreflight: {
allowHeaders: ['Authorization'],
allowMethods: [
Expand All @@ -31,12 +32,10 @@ export class SharedApiGatewayConstruct extends Construct {
// 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
}

get httpApi(): HttpApi {
return this._httpApi;
}
}
24 changes: 15 additions & 9 deletions lib/workload/stateless/sequence_run_manager/deploy/component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import path from 'path';
import * as cdk from 'aws-cdk-lib';
import { aws_lambda, aws_secretsmanager, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { ISecurityGroup, IVpc } from 'aws-cdk-lib/aws-ec2';
import { IEventBus } from 'aws-cdk-lib/aws-events';
import { aws_lambda, aws_secretsmanager, Stack } from 'aws-cdk-lib';
import { PythonFunction, PythonLayerVersion } from '@aws-cdk/aws-lambda-python-alpha';
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import { HttpMethod, HttpRoute, HttpRouteKey, IHttpApi } from 'aws-cdk-lib/aws-apigatewayv2';
import { HttpMethod, HttpRoute, HttpRouteKey, HttpStage } from 'aws-cdk-lib/aws-apigatewayv2';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { SRMApiGatewayConstruct } from './apigw/component';

export interface SequenceRunManagerProps {
securityGroups: ISecurityGroup[];
vpc: IVpc;
mainBus: IEventBus;
httpApi: IHttpApi;
}

export class SequenceRunManagerStack extends Stack {
Expand All @@ -26,7 +27,7 @@ export class SequenceRunManagerStack extends Stack {
private readonly lambdaRuntimePythonVersion: aws_lambda.Runtime = aws_lambda.Runtime.PYTHON_3_12;
private readonly lambdaRole: Role;

constructor(scope: Construct, id: string, props: SequenceRunManagerProps) {
constructor(scope: Construct, id: string, props: cdk.StackProps & SequenceRunManagerProps) {
super(scope, id);

this.id = id;
Expand Down Expand Up @@ -58,7 +59,7 @@ export class SequenceRunManagerStack extends Stack {
});

this.createMigrationHandler();
this.createApiHandler();
this.createApiHandlerAndIntegration();
this.createProcSqsHandler();
}

Expand All @@ -83,15 +84,20 @@ export class SequenceRunManagerStack extends Stack {
});
}

private createApiHandler() {
const apiFn = this.createPythonFunction('Api', {
private createApiHandlerAndIntegration() {
const apiFn: PythonFunction = this.createPythonFunction('Api', {
index: 'api.py',
handler: 'handler',
});

const httpApi = new SRMApiGatewayConstruct(this, this.id + 'SRMApiGatewayConstruct').httpApi;

new HttpStage(this, this.id + 'HttpStage', { httpApi: httpApi });

const apiIntegration = new HttpLambdaIntegration(this.id + 'ApiIntegration', apiFn);
new HttpRoute(this, 'OrcaBusSRMHttpRoute', {
httpApi: this.props.httpApi,

new HttpRoute(this, this.id + 'HttpRoute', {
httpApi: httpApi,
integration: apiIntegration,
routeKey: HttpRouteKey.with(this.apiNamespace + '/{proxy+}', HttpMethod.ANY),
});
Expand Down

0 comments on commit 813cdd7

Please sign in to comment.