quidproquo-deploy-awscdk 0.0.49 → 0.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/QPQPrototypeSingleServiceStack.js +40 -10
- package/lib/lambdas/lambdaAPIGatewayEvent_redirect.d.ts +8 -0
- package/lib/lambdas/lambdaAPIGatewayEvent_redirect.js +39 -0
- package/package.json +1 -1
- package/src/QPQPrototypeSingleServiceStack.ts +70 -13
- package/src/lambdas/lambdaAPIGatewayEvent_redirect.ts +37 -0
|
@@ -50,24 +50,24 @@ const createServiceParameter = (stack, id, service, environment, parameterKey, v
|
|
|
50
50
|
}),
|
|
51
51
|
};
|
|
52
52
|
};
|
|
53
|
-
const createApiDomainName = (stack, id, stackProps) => {
|
|
53
|
+
const createApiDomainName = (stack, id, stackProps, subdomain) => {
|
|
54
54
|
const apexDomain = quidproquo_webserver_1.qpqWebServerUtils.getFeatureDomainName(stackProps.qpqConfig);
|
|
55
|
-
const apiDomainName =
|
|
56
|
-
const apexHostedZone = aws_cdk_lib_1.aws_route53.HostedZone.fromLookup(stack, `${id}-
|
|
55
|
+
const apiDomainName = `${subdomain}.${apexDomain}`;
|
|
56
|
+
const apexHostedZone = aws_cdk_lib_1.aws_route53.HostedZone.fromLookup(stack, `${id}-hostedZone-${subdomain}`, {
|
|
57
57
|
domainName: apexDomain,
|
|
58
58
|
});
|
|
59
|
-
const certificate = new aws_cdk_lib_1.aws_certificatemanager.Certificate(stack, `${id}-
|
|
59
|
+
const certificate = new aws_cdk_lib_1.aws_certificatemanager.Certificate(stack, `${id}-${subdomain}-certificate`, {
|
|
60
60
|
domainName: apiDomainName,
|
|
61
|
-
certificateName:
|
|
61
|
+
certificateName: `${subdomain}-cert-${quidproquo_core_1.qpqCoreUtils.getAppName(stackProps.qpqConfig)}`,
|
|
62
62
|
validation: aws_cdk_lib_1.aws_certificatemanager.CertificateValidation.fromDns(apexHostedZone),
|
|
63
63
|
});
|
|
64
|
-
const domainName = new aws_cdk_lib_1.aws_apigateway.DomainName(stack, `${id}-
|
|
64
|
+
const domainName = new aws_cdk_lib_1.aws_apigateway.DomainName(stack, `${id}-${subdomain}-custom-domain`, {
|
|
65
65
|
domainName: apiDomainName,
|
|
66
66
|
certificate,
|
|
67
67
|
securityPolicy: aws_cdk_lib_1.aws_apigateway.SecurityPolicy.TLS_1_2,
|
|
68
68
|
endpointType: aws_cdk_lib_1.aws_apigateway.EndpointType.REGIONAL,
|
|
69
69
|
});
|
|
70
|
-
new aws_cdk_lib_1.aws_route53.ARecord(stack, `${id}-a-record`, {
|
|
70
|
+
new aws_cdk_lib_1.aws_route53.ARecord(stack, `${id}-a-record-${subdomain}`, {
|
|
71
71
|
zone: apexHostedZone,
|
|
72
72
|
recordName: apiDomainName,
|
|
73
73
|
target: aws_cdk_lib_1.aws_route53.RecordTarget.fromAlias(new aws_cdk_lib_1.aws_route53_targets.ApiGatewayDomain(domainName)),
|
|
@@ -204,7 +204,7 @@ class QPQPrototypeSingleServiceStack extends aws_cdk_lib_1.Stack {
|
|
|
204
204
|
environment: quidproquo_core_1.qpqCoreUtils.getAppFeature(props.qpqConfig),
|
|
205
205
|
service: quidproquo_core_1.qpqCoreUtils.getAppName(props.qpqConfig),
|
|
206
206
|
};
|
|
207
|
-
const domainName = createApiDomainName(this, id, props);
|
|
207
|
+
const domainName = createApiDomainName(this, id, props, 'api');
|
|
208
208
|
const resourceName = (name) => {
|
|
209
209
|
return `${settings.service}-${settings.environment}-${name}`;
|
|
210
210
|
};
|
|
@@ -258,8 +258,8 @@ class QPQPrototypeSingleServiceStack extends aws_cdk_lib_1.Stack {
|
|
|
258
258
|
// Finished building resources, now we can build the lambdas
|
|
259
259
|
////////////////////////////////////////////////////////////////////////////////////
|
|
260
260
|
createWebDistribution(this, `${id}-web-dist`, props, ownedResourceSettings);
|
|
261
|
-
const customLayers = (props.layers || []).map((layer
|
|
262
|
-
const layerName = `${layer.split('/').pop()}-${
|
|
261
|
+
const customLayers = (props.layers || []).map((layer) => {
|
|
262
|
+
const layerName = `${layer.replace(/\\/g, '/').split('/').pop()}-${settings.environment}-${settings.service}`;
|
|
263
263
|
return new aws_cdk_lib_1.aws_lambda.LayerVersion(this, `${id}-${layerName}`, {
|
|
264
264
|
layerVersionName: `${layerName}`,
|
|
265
265
|
code: new aws_cdk_lib_1.aws_lambda.AssetCode(layer),
|
|
@@ -281,6 +281,36 @@ class QPQPrototypeSingleServiceStack extends aws_cdk_lib_1.Stack {
|
|
|
281
281
|
});
|
|
282
282
|
// Grant access to the lambda
|
|
283
283
|
grantReadToOwnedResources(ownedResourceSettings, lambdaHandler);
|
|
284
|
+
quidproquo_webserver_1.qpqWebServerUtils.getSubdomainRedirects(props.qpqConfig).forEach((redirect) => {
|
|
285
|
+
const redirectLambda = new aws_cdk_lib_1.aws_lambda.Function(this, `${settings.environment}-${settings.service}-lambda-redirect-${redirect.subdomain}`, {
|
|
286
|
+
functionName: `redirect-${redirect.subdomain}-${settings.service}-${settings.environment}`,
|
|
287
|
+
timeout: cdk.Duration.seconds(25),
|
|
288
|
+
runtime: aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X,
|
|
289
|
+
memorySize: 128,
|
|
290
|
+
code: aws_cdk_lib_1.aws_lambda.Code.fromAsset(path.join(props.apiBuildPath, 'lambdaAPIGatewayEvent_redirect')),
|
|
291
|
+
handler: 'index.executeAPIGatewayEvent',
|
|
292
|
+
environment: {
|
|
293
|
+
redirectConfig: JSON.stringify(redirect),
|
|
294
|
+
environment: JSON.stringify(settings.environment),
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
const restApi = new aws_cdk_lib_1.aws_apigateway.LambdaRestApi(this, `${id}-redirect-${redirect.subdomain}`, {
|
|
298
|
+
restApiName: `subdomain-redirect-${redirect.subdomain}-${settings.environment}-${settings.service}`,
|
|
299
|
+
handler: redirectLambda,
|
|
300
|
+
deployOptions: {
|
|
301
|
+
loggingLevel: aws_cdk_lib_1.aws_apigateway.MethodLoggingLevel.INFO,
|
|
302
|
+
dataTraceEnabled: true,
|
|
303
|
+
},
|
|
304
|
+
proxy: true,
|
|
305
|
+
});
|
|
306
|
+
// Map all requests to this service to /serviceName/*
|
|
307
|
+
new aws_cdk_lib_1.aws_apigateway.BasePathMapping(this, `${id}-subdomain-bpm-${redirect.subdomain}`, {
|
|
308
|
+
domainName: createApiDomainName(this, id, props, redirect.subdomain),
|
|
309
|
+
restApi: restApi,
|
|
310
|
+
// the properties below are optional
|
|
311
|
+
// basePath: settings.service,
|
|
312
|
+
});
|
|
313
|
+
});
|
|
284
314
|
// Create a rest api
|
|
285
315
|
const api = new aws_cdk_lib_1.aws_apigateway.LambdaRestApi(this, `${settings.environment}-${settings.service}-http-api`, {
|
|
286
316
|
restApiName: `${settings.environment}-${settings.service}-http-api`,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.executeAPIGatewayEvent = void 0;
|
|
13
|
+
const executeAPIGatewayEvent = (event, context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const redirectConfig = JSON.parse(process.env.redirectConfig);
|
|
15
|
+
// For direct urls ~ Go straight to the url
|
|
16
|
+
if (redirectConfig.redirectUrl.startsWith('http')) {
|
|
17
|
+
return {
|
|
18
|
+
statusCode: 301,
|
|
19
|
+
body: '',
|
|
20
|
+
headers: {
|
|
21
|
+
Location: redirectConfig.redirectUrl,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// Otherwise it must be a domain redirect
|
|
26
|
+
const environment = JSON.parse(process.env.environment);
|
|
27
|
+
const baseDomain = redirectConfig.addFeatureEnvironment
|
|
28
|
+
? `${environment}.${redirectConfig.redirectUrl}`
|
|
29
|
+
: redirectConfig.redirectUrl;
|
|
30
|
+
const fullUrl = `https://${baseDomain}${event.path}`;
|
|
31
|
+
return {
|
|
32
|
+
statusCode: 301,
|
|
33
|
+
body: '',
|
|
34
|
+
headers: {
|
|
35
|
+
Location: fullUrl,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
exports.executeAPIGatewayEvent = executeAPIGatewayEvent;
|
package/package.json
CHANGED
|
@@ -99,29 +99,38 @@ const createServiceParameter = (
|
|
|
99
99
|
};
|
|
100
100
|
};
|
|
101
101
|
|
|
102
|
-
const createApiDomainName = (
|
|
102
|
+
const createApiDomainName = (
|
|
103
|
+
stack: cdk.Stack,
|
|
104
|
+
id: string,
|
|
105
|
+
stackProps: QPQPrototypeStackProps,
|
|
106
|
+
subdomain: string,
|
|
107
|
+
) => {
|
|
103
108
|
const apexDomain = qpqWebServerUtils.getFeatureDomainName(stackProps.qpqConfig);
|
|
104
109
|
|
|
105
|
-
const apiDomainName =
|
|
110
|
+
const apiDomainName = `${subdomain}.${apexDomain}`;
|
|
106
111
|
|
|
107
|
-
const apexHostedZone = aws_route53.HostedZone.fromLookup(stack, `${id}-
|
|
112
|
+
const apexHostedZone = aws_route53.HostedZone.fromLookup(stack, `${id}-hostedZone-${subdomain}`, {
|
|
108
113
|
domainName: apexDomain,
|
|
109
114
|
});
|
|
110
115
|
|
|
111
|
-
const certificate = new aws_certificatemanager.Certificate(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
+
const certificate = new aws_certificatemanager.Certificate(
|
|
117
|
+
stack,
|
|
118
|
+
`${id}-${subdomain}-certificate`,
|
|
119
|
+
{
|
|
120
|
+
domainName: apiDomainName,
|
|
121
|
+
certificateName: `${subdomain}-cert-${qpqCoreUtils.getAppName(stackProps.qpqConfig)}`,
|
|
122
|
+
validation: aws_certificatemanager.CertificateValidation.fromDns(apexHostedZone),
|
|
123
|
+
},
|
|
124
|
+
);
|
|
116
125
|
|
|
117
|
-
const domainName = new aws_apigateway.DomainName(stack, `${id}-
|
|
126
|
+
const domainName = new aws_apigateway.DomainName(stack, `${id}-${subdomain}-custom-domain`, {
|
|
118
127
|
domainName: apiDomainName,
|
|
119
128
|
certificate,
|
|
120
129
|
securityPolicy: aws_apigateway.SecurityPolicy.TLS_1_2,
|
|
121
130
|
endpointType: aws_apigateway.EndpointType.REGIONAL,
|
|
122
131
|
});
|
|
123
132
|
|
|
124
|
-
new aws_route53.ARecord(stack, `${id}-a-record`, {
|
|
133
|
+
new aws_route53.ARecord(stack, `${id}-a-record-${subdomain}`, {
|
|
125
134
|
zone: apexHostedZone,
|
|
126
135
|
recordName: apiDomainName,
|
|
127
136
|
target: aws_route53.RecordTarget.fromAlias(
|
|
@@ -309,7 +318,7 @@ export class QPQPrototypeSingleServiceStack extends Stack {
|
|
|
309
318
|
service: qpqCoreUtils.getAppName(props.qpqConfig),
|
|
310
319
|
};
|
|
311
320
|
|
|
312
|
-
const domainName = createApiDomainName(this, id, props);
|
|
321
|
+
const domainName = createApiDomainName(this, id, props, 'api');
|
|
313
322
|
|
|
314
323
|
const resourceName = (name: string) => {
|
|
315
324
|
return `${settings.service}-${settings.environment}-${name}`;
|
|
@@ -420,10 +429,11 @@ export class QPQPrototypeSingleServiceStack extends Stack {
|
|
|
420
429
|
|
|
421
430
|
createWebDistribution(this, `${id}-web-dist`, props, ownedResourceSettings);
|
|
422
431
|
|
|
423
|
-
const customLayers = (props.layers || []).map((layer
|
|
424
|
-
const layerName = `${layer.split('/').pop()}-${
|
|
432
|
+
const customLayers = (props.layers || []).map((layer) => {
|
|
433
|
+
const layerName = `${layer.replace(/\\/g, '/').split('/').pop()}-${settings.environment}-${
|
|
425
434
|
settings.service
|
|
426
435
|
}`;
|
|
436
|
+
|
|
427
437
|
return new aws_lambda.LayerVersion(this, `${id}-${layerName}`, {
|
|
428
438
|
layerVersionName: `${layerName}`,
|
|
429
439
|
code: new aws_lambda.AssetCode(layer),
|
|
@@ -455,6 +465,53 @@ export class QPQPrototypeSingleServiceStack extends Stack {
|
|
|
455
465
|
// Grant access to the lambda
|
|
456
466
|
grantReadToOwnedResources(ownedResourceSettings, lambdaHandler);
|
|
457
467
|
|
|
468
|
+
qpqWebServerUtils.getSubdomainRedirects(props.qpqConfig).forEach((redirect) => {
|
|
469
|
+
const redirectLambda = new aws_lambda.Function(
|
|
470
|
+
this,
|
|
471
|
+
`${settings.environment}-${settings.service}-lambda-redirect-${redirect.subdomain}`,
|
|
472
|
+
{
|
|
473
|
+
functionName: `redirect-${redirect.subdomain}-${settings.service}-${settings.environment}`,
|
|
474
|
+
timeout: cdk.Duration.seconds(25),
|
|
475
|
+
|
|
476
|
+
runtime: aws_lambda.Runtime.NODEJS_16_X,
|
|
477
|
+
memorySize: 128,
|
|
478
|
+
|
|
479
|
+
code: aws_lambda.Code.fromAsset(
|
|
480
|
+
path.join(props.apiBuildPath, 'lambdaAPIGatewayEvent_redirect'),
|
|
481
|
+
),
|
|
482
|
+
handler: 'index.executeAPIGatewayEvent',
|
|
483
|
+
|
|
484
|
+
environment: {
|
|
485
|
+
redirectConfig: JSON.stringify(redirect),
|
|
486
|
+
environment: JSON.stringify(settings.environment),
|
|
487
|
+
},
|
|
488
|
+
},
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
const restApi = new aws_apigateway.LambdaRestApi(
|
|
492
|
+
this,
|
|
493
|
+
`${id}-redirect-${redirect.subdomain}`,
|
|
494
|
+
{
|
|
495
|
+
restApiName: `subdomain-redirect-${redirect.subdomain}-${settings.environment}-${settings.service}`,
|
|
496
|
+
handler: redirectLambda,
|
|
497
|
+
deployOptions: {
|
|
498
|
+
loggingLevel: aws_apigateway.MethodLoggingLevel.INFO,
|
|
499
|
+
dataTraceEnabled: true,
|
|
500
|
+
},
|
|
501
|
+
proxy: true,
|
|
502
|
+
},
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
// Map all requests to this service to /serviceName/*
|
|
506
|
+
new aws_apigateway.BasePathMapping(this, `${id}-subdomain-bpm-${redirect.subdomain}`, {
|
|
507
|
+
domainName: createApiDomainName(this, id, props, redirect.subdomain),
|
|
508
|
+
restApi: restApi,
|
|
509
|
+
|
|
510
|
+
// the properties below are optional
|
|
511
|
+
// basePath: settings.service,
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
|
|
458
515
|
// Create a rest api
|
|
459
516
|
const api = new aws_apigateway.LambdaRestApi(
|
|
460
517
|
this,
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { APIGatewayEvent, Context } from 'aws-lambda';
|
|
2
|
+
import { SubdomainRedirectQPQWebServerConfigSetting } from 'quidproquo-webserver';
|
|
3
|
+
|
|
4
|
+
export const executeAPIGatewayEvent = async (event: APIGatewayEvent, context: Context) => {
|
|
5
|
+
const redirectConfig: SubdomainRedirectQPQWebServerConfigSetting = JSON.parse(
|
|
6
|
+
process.env.redirectConfig as string,
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
// For direct urls ~ Go straight to the url
|
|
10
|
+
if (redirectConfig.redirectUrl.startsWith('http')) {
|
|
11
|
+
return {
|
|
12
|
+
statusCode: 301,
|
|
13
|
+
body: '',
|
|
14
|
+
headers: {
|
|
15
|
+
Location: redirectConfig.redirectUrl,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Otherwise it must be a domain redirect
|
|
21
|
+
const environment: SubdomainRedirectQPQWebServerConfigSetting = JSON.parse(
|
|
22
|
+
process.env.environment as string,
|
|
23
|
+
);
|
|
24
|
+
const baseDomain = redirectConfig.addFeatureEnvironment
|
|
25
|
+
? `${environment}.${redirectConfig.redirectUrl}`
|
|
26
|
+
: redirectConfig.redirectUrl;
|
|
27
|
+
|
|
28
|
+
const fullUrl = `https://${baseDomain}${event.path}`;
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
statusCode: 301,
|
|
32
|
+
body: '',
|
|
33
|
+
headers: {
|
|
34
|
+
Location: fullUrl,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|