Skip to content

Commit d84529a

Browse files
authored
Allow existing DynamoDB Table / Add _next/data route option (#248)
* Initial changes to cause deploy * Allow existing Table to be passed in * Extract EdgeToOrigin * Fix tests * Confirm that logical IDs do not change with tests * Add _next/data route option
1 parent d459664 commit d84529a

File tree

12 files changed

+1283
-340
lines changed

12 files changed

+1283
-340
lines changed

packages/microapps-cdk/API.md

Lines changed: 351 additions & 42 deletions
Large diffs are not rendered by default.

packages/microapps-cdk/src/MicroApps.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { RemovalPolicy } from 'aws-cdk-lib';
22
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
3+
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
34
import * as r53 from 'aws-cdk-lib/aws-route53';
45
import { Construct } from 'constructs';
56
import { IMicroAppsAPIGwy, MicroAppsAPIGwy } from './MicroAppsAPIGwy';
67
import { IMicroAppsCF, MicroAppsCF } from './MicroAppsCF';
8+
import { IMicroAppsEdgeToOrigin, MicroAppsEdgeToOrigin } from './MicroAppsEdgeToOrigin';
79
import { IMicroAppsS3, MicroAppsS3 } from './MicroAppsS3';
810
import { IMicroAppsSvcs, MicroAppsSvcs } from './MicroAppsSvcs';
911
import { reverseDomain } from './utils/ReverseDomain';
@@ -232,6 +234,22 @@ export interface MicroAppsProps {
232234
* @default undefined
233235
*/
234236
readonly originRegion?: string;
237+
238+
/**
239+
* Existing table for apps/versions/rules
240+
*
241+
* @warning - It is *strongly* suggested that production stacks create
242+
* their own DynamoDB Table and pass it into this construct, for protection
243+
* against data loss due to logical ID changes, the ability to configure
244+
* Provisioned capacity with Auto Scaling, the ability to add additional indices, etc.
245+
*
246+
* Requirements:
247+
* - Hash Key: `PK`
248+
* - Sort Key: `SK`
249+
*
250+
* @default created by construct
251+
*/
252+
readonly table?: dynamodb.ITable;
235253
}
236254

237255
/**
@@ -241,6 +259,9 @@ export interface IMicroApps {
241259
/** {@inheritdoc IMicroAppsCF} */
242260
readonly cf: IMicroAppsCF;
243261

262+
/** {@inheritdoc IMicroAppsEdgeToOrigin} */
263+
readonly edgeToOrigin?: IMicroAppsEdgeToOrigin;
264+
244265
/** {@inheritdoc IMicroAppsS3} */
245266
readonly s3: IMicroAppsS3;
246267

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

301+
private _edgeToOrigin?: MicroAppsEdgeToOrigin;
302+
public get edgeToOrigin(): IMicroAppsEdgeToOrigin | undefined {
303+
return this._edgeToOrigin;
304+
}
305+
274306
private _s3: MicroAppsS3;
275307
public get s3(): IMicroAppsS3 {
276308
return this._s3;
@@ -312,6 +344,7 @@ export class MicroApps extends Construct implements IMicroApps {
312344
replaceHostHeader = true,
313345
signingMode = 'sign',
314346
originRegion,
347+
table,
315348
} = props;
316349

317350
this._s3 = new MicroAppsS3(this, 's3', {
@@ -335,6 +368,17 @@ export class MicroApps extends Construct implements IMicroApps {
335368
rootPathPrefix,
336369
requireIAMAuthorization: signingMode !== 'none',
337370
});
371+
if (signingMode !== 'none' || replaceHostHeader || addXForwardedHostHeader) {
372+
this._edgeToOrigin = new MicroAppsEdgeToOrigin(this, 'edgeToOrigin', {
373+
assetNameRoot,
374+
assetNameSuffix,
375+
removalPolicy,
376+
addXForwardedHostHeader,
377+
replaceHostHeader,
378+
originRegion,
379+
signingMode,
380+
});
381+
}
338382
this._cf = new MicroAppsCF(this, 'cft', {
339383
removalPolicy,
340384
assetNameRoot,
@@ -348,10 +392,7 @@ export class MicroApps extends Construct implements IMicroApps {
348392
bucketLogs: this._s3.bucketLogs,
349393
rootPathPrefix,
350394
createAPIPathRoute,
351-
addXForwardedHostHeader,
352-
replaceHostHeader,
353-
signingMode,
354-
originRegion,
395+
edgeToOriginLambdas: this._edgeToOrigin ? this._edgeToOrigin.edgeToOriginLambdas : undefined,
355396
});
356397
this._svcs = new MicroAppsSvcs(this, 'svcs', {
357398
httpApi: this.apigwy.httpApi,
@@ -367,6 +408,7 @@ export class MicroApps extends Construct implements IMicroApps {
367408
s3StrictBucketPolicy,
368409
rootPathPrefix,
369410
requireIAMAuthorization: signingMode !== 'none',
411+
table,
370412
});
371413
}
372414
}

packages/microapps-cdk/src/MicroAppsAPIGwy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ export interface IMicroAppsAPIGwy {
9999
}
100100

101101
/**
102-
* Create a new MicroApps API Gateway HTTP API endpoint.
102+
* Create a new MicroApps API Gateway HTTP API endpoint, optionally
103+
* requiring IAM authorization
103104
*/
104105
export class MicroAppsAPIGwy extends Construct implements IMicroAppsAPIGwy {
105106
private _dnAppsOrigin: apigwy.DomainName | undefined;

0 commit comments

Comments
 (0)