Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow existing DynamoDB Table / Add _next/data route option #248

Merged
merged 6 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
393 changes: 351 additions & 42 deletions packages/microapps-cdk/API.md

Large diffs are not rendered by default.

52 changes: 47 additions & 5 deletions packages/microapps-cdk/src/MicroApps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { RemovalPolicy } from 'aws-cdk-lib';
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as r53 from 'aws-cdk-lib/aws-route53';
import { Construct } from 'constructs';
import { IMicroAppsAPIGwy, MicroAppsAPIGwy } from './MicroAppsAPIGwy';
import { IMicroAppsCF, MicroAppsCF } from './MicroAppsCF';
import { IMicroAppsEdgeToOrigin, MicroAppsEdgeToOrigin } from './MicroAppsEdgeToOrigin';
import { IMicroAppsS3, MicroAppsS3 } from './MicroAppsS3';
import { IMicroAppsSvcs, MicroAppsSvcs } from './MicroAppsSvcs';
import { reverseDomain } from './utils/ReverseDomain';
Expand Down Expand Up @@ -232,6 +234,22 @@ export interface MicroAppsProps {
* @default undefined
*/
readonly originRegion?: string;

/**
* Existing table for apps/versions/rules
*
* @warning - It is *strongly* suggested that production stacks create
* their own DynamoDB Table and pass it into this construct, for protection
* against data loss due to logical ID changes, the ability to configure
* Provisioned capacity with Auto Scaling, the ability to add additional indices, etc.
*
* Requirements:
* - Hash Key: `PK`
* - Sort Key: `SK`
*
* @default created by construct
*/
readonly table?: dynamodb.ITable;
}

/**
Expand All @@ -241,6 +259,9 @@ export interface IMicroApps {
/** {@inheritdoc IMicroAppsCF} */
readonly cf: IMicroAppsCF;

/** {@inheritdoc IMicroAppsEdgeToOrigin} */
readonly edgeToOrigin?: IMicroAppsEdgeToOrigin;

/** {@inheritdoc IMicroAppsS3} */
readonly s3: IMicroAppsS3;

Expand All @@ -255,14 +276,20 @@ export interface IMicroApps {
* Create a new MicroApps "turnkey" construct for simple
* deployments and for initial evaulation of the MicroApps framework.
*
* Use this construct to create a working entire stack.
* Use this construct to create a PoC working entire stack.
*
* Do not use this construct when adding MicroApps to an existing
* CloudFront, API Gateway, S3 Bucket, etc. or where access
* to all features of the AWS Resources are needed (e.g. to
* add additional Behaviors to the CloudFront distribution, set authorizors
* on API Gateway, etc.).
*
* @warning This construct is not intended for production use.
* In a production stack the DynamoDB Table, API Gateway, S3 Buckets,
* etc. should be created in a "durable" stack where the IDs will not
* change and where changes to the MicroApps construct will not
* cause failures to deploy or data to be deleted.
*
* @see {@link https://github.com/pwrdrvr/microapps-core/blob/main/packages/cdk/lib/MicroApps.ts | example usage in a CDK Stack }
*/
export class MicroApps extends Construct implements IMicroApps {
Expand All @@ -271,6 +298,11 @@ export class MicroApps extends Construct implements IMicroApps {
return this._cf;
}

private _edgeToOrigin?: MicroAppsEdgeToOrigin;
public get edgeToOrigin(): IMicroAppsEdgeToOrigin | undefined {
return this._edgeToOrigin;
}

private _s3: MicroAppsS3;
public get s3(): IMicroAppsS3 {
return this._s3;
Expand Down Expand Up @@ -312,6 +344,7 @@ export class MicroApps extends Construct implements IMicroApps {
replaceHostHeader = true,
signingMode = 'sign',
originRegion,
table,
} = props;

this._s3 = new MicroAppsS3(this, 's3', {
Expand All @@ -335,6 +368,17 @@ export class MicroApps extends Construct implements IMicroApps {
rootPathPrefix,
requireIAMAuthorization: signingMode !== 'none',
});
if (signingMode !== 'none' || replaceHostHeader || addXForwardedHostHeader) {
this._edgeToOrigin = new MicroAppsEdgeToOrigin(this, 'edgeToOrigin', {
assetNameRoot,
assetNameSuffix,
removalPolicy,
addXForwardedHostHeader,
replaceHostHeader,
originRegion,
signingMode,
});
}
this._cf = new MicroAppsCF(this, 'cft', {
removalPolicy,
assetNameRoot,
Expand All @@ -348,10 +392,7 @@ export class MicroApps extends Construct implements IMicroApps {
bucketLogs: this._s3.bucketLogs,
rootPathPrefix,
createAPIPathRoute,
addXForwardedHostHeader,
replaceHostHeader,
signingMode,
originRegion,
edgeToOriginLambdas: this._edgeToOrigin ? this._edgeToOrigin.edgeToOriginLambdas : undefined,
});
this._svcs = new MicroAppsSvcs(this, 'svcs', {
httpApi: this.apigwy.httpApi,
Expand All @@ -367,6 +408,7 @@ export class MicroApps extends Construct implements IMicroApps {
s3StrictBucketPolicy,
rootPathPrefix,
requireIAMAuthorization: signingMode !== 'none',
table,
});
}
}
3 changes: 2 additions & 1 deletion packages/microapps-cdk/src/MicroAppsAPIGwy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export interface IMicroAppsAPIGwy {
}

/**
* Create a new MicroApps API Gateway HTTP API endpoint.
* Create a new MicroApps API Gateway HTTP API endpoint, optionally
* requiring IAM authorization
*/
export class MicroAppsAPIGwy extends Construct implements IMicroAppsAPIGwy {
private _dnAppsOrigin: apigwy.DomainName | undefined;
Expand Down
Loading