quidproquo-deploy-awscdk 0.0.20 → 0.0.22

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.
@@ -0,0 +1,6 @@
1
+ import { QPQConfig } from 'quidproquo-core';
2
+ import { DeploymentType } from './DeploymentType';
3
+ export interface DeploymentSettings {
4
+ environment: DeploymentType;
5
+ qpqConfig: QPQConfig;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export declare enum DeploymentType {
2
+ Dev = "development",
3
+ Staging = "staging",
4
+ Prod = "production"
5
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeploymentType = void 0;
4
+ var DeploymentType;
5
+ (function (DeploymentType) {
6
+ DeploymentType["Dev"] = "development";
7
+ DeploymentType["Staging"] = "staging";
8
+ DeploymentType["Prod"] = "production";
9
+ })(DeploymentType = exports.DeploymentType || (exports.DeploymentType = {}));
@@ -0,0 +1,12 @@
1
+ import { DeploymentSettings } from './DeploymentSettings';
2
+ import { Stack } from 'aws-cdk-lib';
3
+ import { Construct } from 'constructs';
4
+ export interface QPQPrototypeStackProps extends DeploymentSettings {
5
+ account: string;
6
+ region: string;
7
+ apiBuildPath: string;
8
+ webBuildPath: string;
9
+ }
10
+ export declare class QPQPrototypeSingleServiceStack extends Stack {
11
+ constructor(scope: Construct, id: string, props: QPQPrototypeStackProps);
12
+ }
@@ -0,0 +1,273 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.QPQPrototypeSingleServiceStack = void 0;
27
+ const path = __importStar(require("path"));
28
+ const aws_cdk_lib_1 = require("aws-cdk-lib");
29
+ const cdk = __importStar(require("aws-cdk-lib"));
30
+ const quidproquo_core_1 = require("quidproquo-core");
31
+ const quidproquo_webserver_1 = require("quidproquo-webserver");
32
+ const DeploymentType_1 = require("./DeploymentType");
33
+ const getEnvironmentDomain = (stackProps) => {
34
+ const apexDomain = quidproquo_webserver_1.qpqWebServerUtils.getDomainName(stackProps.qpqConfig);
35
+ if (stackProps.environment === DeploymentType_1.DeploymentType.Prod) {
36
+ return apexDomain;
37
+ }
38
+ return `${stackProps.environment}.${apexDomain}`;
39
+ };
40
+ const createApiDomainName = (stack, id, stackProps) => {
41
+ const apexDomain = getEnvironmentDomain(stackProps);
42
+ const apiDomainName = `api.${apexDomain}`;
43
+ const apexHostedZone = aws_cdk_lib_1.aws_route53.HostedZone.fromLookup(stack, `${id}-apex-hostedZone`, {
44
+ domainName: apexDomain,
45
+ });
46
+ const certificate = new aws_cdk_lib_1.aws_certificatemanager.Certificate(stack, `${id}-api-certificate`, {
47
+ domainName: apiDomainName,
48
+ certificateName: `api-cert-${quidproquo_core_1.qpqCoreUtils.getAppName(stackProps.qpqConfig)}`,
49
+ validation: aws_cdk_lib_1.aws_certificatemanager.CertificateValidation.fromDns(apexHostedZone),
50
+ });
51
+ const domainName = new aws_cdk_lib_1.aws_apigateway.DomainName(stack, `${id}-api-custom-domain`, {
52
+ domainName: apiDomainName,
53
+ certificate,
54
+ securityPolicy: aws_cdk_lib_1.aws_apigateway.SecurityPolicy.TLS_1_2,
55
+ endpointType: aws_cdk_lib_1.aws_apigateway.EndpointType.REGIONAL,
56
+ });
57
+ new aws_cdk_lib_1.aws_route53.ARecord(stack, `${id}-a-record`, {
58
+ zone: apexHostedZone,
59
+ recordName: apiDomainName,
60
+ target: aws_cdk_lib_1.aws_route53.RecordTarget.fromAlias(new aws_cdk_lib_1.aws_route53_targets.ApiGatewayDomain(domainName)),
61
+ });
62
+ return domainName;
63
+ };
64
+ const createWebDistribution = (stack, id, stackProps) => {
65
+ const apexDomain = getEnvironmentDomain(stackProps);
66
+ const serviceName = quidproquo_core_1.qpqCoreUtils.getAppName(stackProps.qpqConfig);
67
+ // create an s3 bucket
68
+ const staticWebFilesBucket = new aws_cdk_lib_1.aws_s3.Bucket(stack, `${id}-web`, {
69
+ bucketName: `${serviceName}-${stackProps.environment}-web`,
70
+ // Disable public access to this bucket, CloudFront will do that
71
+ publicReadAccess: false,
72
+ blockPublicAccess: aws_cdk_lib_1.aws_s3.BlockPublicAccess.BLOCK_ALL,
73
+ // Default website file
74
+ websiteIndexDocument: 'index.html',
75
+ // Allow bucket to auto delete upon cdk:Destroy
76
+ removalPolicy: cdk.RemovalPolicy.DESTROY,
77
+ autoDeleteObjects: true,
78
+ });
79
+ // Create OriginAccessIdentity for the bucket.
80
+ const websiteOAI = new aws_cdk_lib_1.aws_cloudfront.OriginAccessIdentity(stack, `${id}-website-OAI`);
81
+ staticWebFilesBucket.grantRead(websiteOAI);
82
+ // Grab the hosted zone we want to add
83
+ const serviceHostedZone = aws_cdk_lib_1.aws_route53.HostedZone.fromLookup(stack, 'hosted-zone', {
84
+ domainName: apexDomain,
85
+ });
86
+ // Create a certificate for the distribution - Seems to a bug where Route 53 records not cleaned up
87
+ // after removing the DNS Validated certificate see: https://github.com/aws/aws-cdk/issues/3333
88
+ // `switch over to using the Certificate with the new built-in (CloudFormation-based) DNS validation`
89
+ const validationCertificate = aws_cdk_lib_1.aws_certificatemanager.CertificateValidation.fromDns(serviceHostedZone);
90
+ const certificate = new aws_cdk_lib_1.aws_certificatemanager.DnsValidatedCertificate(stack, `${id}-viewer-cert`, {
91
+ hostedZone: serviceHostedZone,
92
+ domainName: apexDomain,
93
+ region: 'us-east-1',
94
+ validation: validationCertificate,
95
+ });
96
+ const viewerCertificate = aws_cdk_lib_1.aws_cloudfront.ViewerCertificate.fromAcmCertificate(certificate, {
97
+ aliases: [apexDomain],
98
+ });
99
+ new aws_cdk_lib_1.aws_s3_deployment.BucketDeployment(stack, `${id}-DeployWebsite`, {
100
+ sources: [aws_cdk_lib_1.aws_s3_deployment.Source.asset(stackProps.webBuildPath)],
101
+ destinationBucket: staticWebFilesBucket,
102
+ });
103
+ // Create a cloud front distribution
104
+ // TODO: use aws_cloudfront.Distribution
105
+ const distribution = new aws_cdk_lib_1.aws_cloudfront.CloudFrontWebDistribution(stack, 'cf-distribution', {
106
+ originConfigs: [
107
+ {
108
+ s3OriginSource: {
109
+ s3BucketSource: staticWebFilesBucket,
110
+ originAccessIdentity: websiteOAI,
111
+ },
112
+ behaviors: [
113
+ ...[
114
+ '/remoteEntry.js',
115
+ '/index.js',
116
+ '/index.html', // Add this if we need it
117
+ ].map((pp) => ({
118
+ pathPattern: pp,
119
+ maxTtl: cdk.Duration.seconds(0),
120
+ minTtl: cdk.Duration.seconds(0),
121
+ defaultTtl: cdk.Duration.seconds(0),
122
+ })),
123
+ {
124
+ isDefaultBehavior: true,
125
+ },
126
+ ],
127
+ },
128
+ ],
129
+ viewerCertificate: viewerCertificate,
130
+ errorConfigurations: [
131
+ {
132
+ errorCode: 404,
133
+ responseCode: 200,
134
+ responsePagePath: '/',
135
+ },
136
+ ],
137
+ });
138
+ // Create a cdn link
139
+ new aws_cdk_lib_1.aws_route53.ARecord(stack, `${id}-web-alias`, {
140
+ zone: serviceHostedZone,
141
+ recordName: apexDomain,
142
+ target: aws_cdk_lib_1.aws_route53.RecordTarget.fromAlias(new aws_cdk_lib_1.aws_route53_targets.CloudFrontTarget(distribution)),
143
+ });
144
+ return distribution;
145
+ };
146
+ class QPQPrototypeSingleServiceStack extends aws_cdk_lib_1.Stack {
147
+ constructor(scope, id, props) {
148
+ super(scope, id, {
149
+ env: {
150
+ account: props.account,
151
+ region: props.region,
152
+ },
153
+ });
154
+ const settings = {
155
+ environment: props.environment,
156
+ service: quidproquo_core_1.qpqCoreUtils.getAppName(props.qpqConfig),
157
+ };
158
+ const domainName = createApiDomainName(this, id, props);
159
+ createWebDistribution(this, `${id}-web-dist`, props);
160
+ const resourceName = (name) => {
161
+ return `${settings.service}-${settings.environment}-${name}`;
162
+ };
163
+ const BLLayer = new aws_cdk_lib_1.aws_lambda.LayerVersion(this, `${id}-BLLayer`, {
164
+ layerVersionName: `${id}-BLLayer`,
165
+ code: new aws_cdk_lib_1.aws_lambda.AssetCode(props.apiBuildPath),
166
+ compatibleRuntimes: [aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X],
167
+ });
168
+ const ownedSecrets = quidproquo_core_1.qpqCoreUtils.getOwnedSecrets(props.qpqConfig).map((secret) => {
169
+ const realSecretName = `${secret.key}-${settings.service}-${settings.environment}`;
170
+ return {
171
+ secretName: secret.key,
172
+ realName: realSecretName,
173
+ secret: new aws_cdk_lib_1.aws_secretsmanager.Secret(this, `${id}-secret-${secret.key}`, {
174
+ secretName: realSecretName,
175
+ removalPolicy: cdk.RemovalPolicy.DESTROY,
176
+ description: `${settings.environment}-${settings.service}`,
177
+ }),
178
+ };
179
+ });
180
+ const buckets = quidproquo_core_1.qpqCoreUtils.getStorageDriveNames(props.qpqConfig).map((driveName) => {
181
+ const bucketName = resourceName(driveName);
182
+ const bucket = new aws_cdk_lib_1.aws_s3.Bucket(this, bucketName, {
183
+ bucketName: resourceName(driveName),
184
+ // Disable public access to this bucket, CloudFront will do that
185
+ publicReadAccess: false,
186
+ blockPublicAccess: aws_cdk_lib_1.aws_s3.BlockPublicAccess.BLOCK_ALL,
187
+ // Allow bucket to auto delete upon cdk:Destroy
188
+ removalPolicy: cdk.RemovalPolicy.DESTROY,
189
+ autoDeleteObjects: true,
190
+ });
191
+ return {
192
+ driveName,
193
+ bucketName: bucketName,
194
+ bucket: bucket,
195
+ };
196
+ });
197
+ // This should be all resource names
198
+ const runtimeConfigLambdaConfig = {
199
+ qpqConfig: props.qpqConfig,
200
+ secretNameMap: ownedSecrets.reduce((acc, os) => (Object.assign(Object.assign({}, acc), { [os.secretName]: os.realName })), {}),
201
+ resourceNameMap: buckets.reduce((acc, b) => (Object.assign(Object.assign({}, acc), { [b.driveName]: b.bucketName })), {}),
202
+ };
203
+ const lambdaHandler = new aws_cdk_lib_1.aws_lambda_nodejs.NodejsFunction(this, `${settings.environment}-${settings.service}-lambda-rest`, {
204
+ functionName: `lambda-rest-${settings.service}-${settings.environment}`,
205
+ entry: path.resolve(__dirname, 'lambdas', 'lambdaAPIGatewayEvent.js'),
206
+ handler: 'execute',
207
+ timeout: cdk.Duration.seconds(25),
208
+ runtime: aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X,
209
+ layers: [BLLayer],
210
+ bundling: {
211
+ tsconfig: './tsconfig.json',
212
+ define: {
213
+ 'process.env.lambdaRuntimeConfig': JSON.stringify(JSON.stringify(runtimeConfigLambdaConfig)),
214
+ },
215
+ },
216
+ environment: {
217
+ TABLES: JSON.stringify([]),
218
+ },
219
+ });
220
+ // Grant access to the lambda
221
+ buckets.forEach((b) => b.bucket.grantReadWrite(lambdaHandler));
222
+ ownedSecrets.forEach((os) => os.secret.grantRead(lambdaHandler));
223
+ // Create a rest api
224
+ const api = new aws_cdk_lib_1.aws_apigateway.LambdaRestApi(this, `${settings.environment}-${settings.service}-http-api`, {
225
+ restApiName: `${settings.environment}-${settings.service}-http-api`,
226
+ handler: lambdaHandler,
227
+ deployOptions: {
228
+ loggingLevel: aws_cdk_lib_1.aws_apigateway.MethodLoggingLevel.INFO,
229
+ dataTraceEnabled: true,
230
+ },
231
+ proxy: true,
232
+ });
233
+ // Map all requests to this service to /serviceName/*
234
+ new aws_cdk_lib_1.aws_apigateway.BasePathMapping(this, `${id}-rest-bpm`, {
235
+ domainName: domainName,
236
+ restApi: api,
237
+ // the properties below are optional
238
+ // basePath: settings.service,
239
+ });
240
+ quidproquo_core_1.qpqCoreUtils.getScheduleEvents(props.qpqConfig).forEach((se, index) => {
241
+ const schedulerFunction = new aws_cdk_lib_1.aws_lambda_nodejs.NodejsFunction(this, `${settings.environment}-${settings.service}-SE-${index}`, {
242
+ functionName: `SE-${index}-${se.runtime}-${settings.environment}-${settings.service}`,
243
+ entry: path.resolve(__dirname, 'lambdas', 'lambdaEventBridgeEvent.js'),
244
+ handler: 'execute',
245
+ timeout: cdk.Duration.minutes(15),
246
+ runtime: aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X,
247
+ memorySize: 400,
248
+ layers: [BLLayer],
249
+ bundling: {
250
+ tsconfig: './tsconfig.json',
251
+ define: {
252
+ 'process.env.lambdaRuntimeConfig': JSON.stringify(JSON.stringify(Object.assign(Object.assign({}, runtimeConfigLambdaConfig), { lambdaRuntimeConfig: {
253
+ src: se.src,
254
+ runtime: se.runtime,
255
+ } }))),
256
+ },
257
+ },
258
+ environment: {
259
+ TABLES: JSON.stringify([]),
260
+ },
261
+ });
262
+ buckets.forEach((b) => b.bucket.grantReadWrite(schedulerFunction));
263
+ ownedSecrets.forEach((os) => os.secret.grantRead(schedulerFunction));
264
+ // EventBridge rule which runs every five minutes
265
+ const cronRule = new aws_cdk_lib_1.aws_events.Rule(this, `${id}-se-cronrule-${index}`, {
266
+ schedule: aws_cdk_lib_1.aws_events.Schedule.expression(`cron(${se.cronExpression})`),
267
+ });
268
+ // Set the target as lambda function
269
+ cronRule.addTarget(new aws_cdk_lib_1.aws_events_targets.LambdaFunction(schedulerFunction));
270
+ });
271
+ }
272
+ }
273
+ exports.QPQPrototypeSingleServiceStack = QPQPrototypeSingleServiceStack;
package/lib/index.d.ts CHANGED
@@ -1 +1,4 @@
1
- export * from './QPQPrototypeStack';
1
+ export * from './utils';
2
+ export * from './DeploymentType';
3
+ export * from './DeploymentSettings';
4
+ export * from './QPQPrototypeSingleServiceStack';
package/lib/index.js CHANGED
@@ -14,4 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./QPQPrototypeStack"), exports);
17
+ __exportStar(require("./utils"), exports);
18
+ __exportStar(require("./DeploymentType"), exports);
19
+ __exportStar(require("./DeploymentSettings"), exports);
20
+ __exportStar(require("./QPQPrototypeSingleServiceStack"), exports);
@@ -0,0 +1,7 @@
1
+ import { Context } from 'aws-lambda';
2
+ export declare const getDateNow: () => string;
3
+ export declare const execute: (event: import("aws-lambda").APIGatewayProxyEvent, context: Context) => Promise<{
4
+ statusCode: any;
5
+ body: string;
6
+ headers: any;
7
+ }>;
@@ -0,0 +1,69 @@
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.execute = exports.getDateNow = void 0;
13
+ const quidproquo_actionprocessor_node_1 = require("quidproquo-actionprocessor-node");
14
+ const quidproquo_actionprocessor_awslambda_1 = require("quidproquo-actionprocessor-awslambda");
15
+ const quidproquo_core_1 = require("quidproquo-core");
16
+ const lambdaConfig_1 = require("./lambdaConfig");
17
+ const getDateNow = () => new Date().toISOString();
18
+ exports.getDateNow = getDateNow;
19
+ const ErrorTypeHttpResponseMap = {
20
+ [quidproquo_core_1.ErrorTypeEnum.BadRequest]: 400,
21
+ [quidproquo_core_1.ErrorTypeEnum.Unauthorized]: 401,
22
+ [quidproquo_core_1.ErrorTypeEnum.PaymentRequired]: 402,
23
+ [quidproquo_core_1.ErrorTypeEnum.Forbidden]: 403,
24
+ [quidproquo_core_1.ErrorTypeEnum.NotFound]: 404,
25
+ [quidproquo_core_1.ErrorTypeEnum.TimeOut]: 408,
26
+ [quidproquo_core_1.ErrorTypeEnum.UnsupportedMediaType]: 415,
27
+ [quidproquo_core_1.ErrorTypeEnum.OutOfResources]: 500,
28
+ [quidproquo_core_1.ErrorTypeEnum.GenericError]: 500,
29
+ [quidproquo_core_1.ErrorTypeEnum.NotImplemented]: 501,
30
+ [quidproquo_core_1.ErrorTypeEnum.NoContent]: 204,
31
+ };
32
+ const execute = (event, context) => __awaiter(void 0, void 0, void 0, function* () {
33
+ // Build a processor for the session and stuff
34
+ // Remove the non route ones ~ let the story execute action add them
35
+ const storyActionProcessor = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, quidproquo_actionprocessor_node_1.coreActionProcessor), quidproquo_actionprocessor_node_1.webserverActionProcessor), (0, quidproquo_actionprocessor_awslambda_1.getAPIGatewayEventActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig.qpqConfig)), (0, quidproquo_actionprocessor_awslambda_1.getConfigGetSecretActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig)), (0, quidproquo_actionprocessor_awslambda_1.getSystemActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig)), (0, quidproquo_actionprocessor_awslambda_1.getFileActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig));
36
+ // const doWeCarePath = (event.path || "").replace(
37
+ // new RegExp(`^(\/${envConfig.appName})/`),
38
+ // "/"
39
+ // );
40
+ const logger = (result) => __awaiter(void 0, void 0, void 0, function* () {
41
+ // addResult(
42
+ // envConfig.appName,
43
+ // getDateNow(),
44
+ // doWeCarePath,
45
+ // "route-infrastructure",
46
+ // "infrastructure",
47
+ // "askRoute",
48
+ // result
49
+ // );
50
+ });
51
+ // // Run the callback
52
+ const resolveStory = (0, quidproquo_core_1.createRuntime)({}, storyActionProcessor, exports.getDateNow, logger, quidproquo_actionprocessor_awslambda_1.awsLambdaUtils.randomGuid);
53
+ const result = yield resolveStory(quidproquo_core_1.askProcessEvent, [event, context]);
54
+ // // Run the callback
55
+ if (!result.error) {
56
+ return {
57
+ statusCode: result.result.statusCode,
58
+ body: JSON.stringify(result.error ? result.error : result.result.body),
59
+ headers: result.result.headers,
60
+ };
61
+ }
62
+ const code = ErrorTypeHttpResponseMap[result.error.errorType];
63
+ return {
64
+ statusCode: code || 500,
65
+ body: JSON.stringify(result.error),
66
+ headers: {},
67
+ };
68
+ });
69
+ exports.execute = execute;
@@ -0,0 +1,2 @@
1
+ import { QPQAWSLambdaConfig } from 'quidproquo-actionprocessor-awslambda';
2
+ export declare const lambdaRuntimeConfig: QPQAWSLambdaConfig;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lambdaRuntimeConfig = void 0;
4
+ exports.lambdaRuntimeConfig = JSON.parse(process.env.lambdaRuntimeConfig || '{}');
@@ -0,0 +1,3 @@
1
+ import { EventBridgeEvent, Context } from 'aws-lambda';
2
+ export declare const getDateNow: () => string;
3
+ export declare const execute: (event: EventBridgeEvent<string, void>, context: Context) => Promise<void>;
@@ -0,0 +1,29 @@
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.execute = exports.getDateNow = void 0;
13
+ const quidproquo_actionprocessor_node_1 = require("quidproquo-actionprocessor-node");
14
+ const quidproquo_actionprocessor_awslambda_1 = require("quidproquo-actionprocessor-awslambda");
15
+ const quidproquo_core_1 = require("quidproquo-core");
16
+ const lambdaConfig_1 = require("./lambdaConfig");
17
+ // TODO: Make this a util or something based on server time or something..
18
+ const getDateNow = () => new Date().toISOString();
19
+ exports.getDateNow = getDateNow;
20
+ const execute = (event, context) => __awaiter(void 0, void 0, void 0, function* () {
21
+ // Build a processor for the session and stuff
22
+ // Remove the non event ones
23
+ const storyActionProcessor = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, quidproquo_actionprocessor_node_1.coreActionProcessor), quidproquo_actionprocessor_node_1.webserverActionProcessor), (0, quidproquo_actionprocessor_awslambda_1.getEventBridgeEventActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig)), (0, quidproquo_actionprocessor_awslambda_1.getSystemActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig)), (0, quidproquo_actionprocessor_awslambda_1.getFileActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig)), (0, quidproquo_actionprocessor_awslambda_1.getConfigGetSecretActionProcessor)(lambdaConfig_1.lambdaRuntimeConfig));
24
+ const logger = (result) => __awaiter(void 0, void 0, void 0, function* () { });
25
+ // Run the callback
26
+ const resolveStory = (0, quidproquo_core_1.createRuntime)({}, storyActionProcessor, exports.getDateNow, logger, quidproquo_actionprocessor_awslambda_1.awsLambdaUtils.randomGuid);
27
+ yield resolveStory(quidproquo_core_1.askProcessEvent, [event, context]);
28
+ });
29
+ exports.execute = execute;
package/lib/utils.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { DeploymentSettings } from './DeploymentSettings';
2
+ export declare const getStackName: (deploymentSettings: DeploymentSettings) => string;
package/lib/utils.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStackName = void 0;
4
+ const quidproquo_core_1 = require("quidproquo-core");
5
+ const getStackName = (deploymentSettings) => {
6
+ const appName = quidproquo_core_1.qpqCoreUtils.getAppName(deploymentSettings.qpqConfig);
7
+ return `${appName}-${deploymentSettings.environment}`;
8
+ };
9
+ exports.getStackName = getStackName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quidproquo-deploy-awscdk",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.js",
@@ -1,16 +0,0 @@
1
- import { Stack, StackProps } from 'aws-cdk-lib';
2
- import { Construct } from 'constructs';
3
- import { QPQConfig } from 'quidproquo-core';
4
- export interface LambdaEntry {
5
- src: string;
6
- runtime: string;
7
- }
8
- export interface QPQPrototypeStackProps extends StackProps {
9
- serviceBuildPath: string;
10
- qpqConfig: QPQConfig;
11
- routeEntry: LambdaEntry;
12
- eventEntry: LambdaEntry;
13
- }
14
- export declare class QPQPrototypeStack extends Stack {
15
- constructor(scope: Construct, id: string, props: QPQPrototypeStackProps);
16
- }
@@ -1,138 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.QPQPrototypeStack = void 0;
27
- const aws_cdk_lib_1 = require("aws-cdk-lib");
28
- const cdk = __importStar(require("aws-cdk-lib"));
29
- const quidproquo_core_1 = require("quidproquo-core");
30
- class QPQPrototypeStack extends aws_cdk_lib_1.Stack {
31
- constructor(scope, id, props) {
32
- super(scope, id, props);
33
- const settings = {
34
- environment: process.env.ENVIRONMENT || 'dev',
35
- service: quidproquo_core_1.qpqCoreUtils.getAppName(props.qpqConfig),
36
- aws: {
37
- account: process.env.CDK_DEPLOY_ACCOUNT || process.env.CDK_DEFAULT_ACCOUNT,
38
- region: process.env.CDK_DEPLOY_REGION || process.env.CDK_DEFAULT_REGION,
39
- },
40
- };
41
- const resourceName = (name) => {
42
- return `${settings.service}-${settings.environment}-${name}`;
43
- };
44
- const BLLayer = new aws_cdk_lib_1.aws_lambda.LayerVersion(this, `${id}-BLLayer`, {
45
- layerVersionName: `${id}-BLLayer`,
46
- code: new aws_cdk_lib_1.aws_lambda.AssetCode(props.serviceBuildPath),
47
- compatibleRuntimes: [aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X],
48
- });
49
- const ownedSecrets = quidproquo_core_1.qpqCoreUtils.getOwnedSecrets(props.qpqConfig).map((secret) => new aws_cdk_lib_1.aws_secretsmanager.Secret(this, `secret-${secret.key}`, {
50
- secretName: secret.key,
51
- removalPolicy: cdk.RemovalPolicy.DESTROY,
52
- description: `${settings.environment}-${settings.service}`,
53
- }));
54
- const buckets = quidproquo_core_1.qpqCoreUtils.getStorageDriveNames(props.qpqConfig).map((driveName) => {
55
- const bucketName = resourceName(driveName);
56
- const bucket = new aws_cdk_lib_1.aws_s3.Bucket(this, bucketName, {
57
- bucketName: resourceName(driveName),
58
- // Disable public access to this bucket, CloudFront will do that
59
- publicReadAccess: false,
60
- blockPublicAccess: aws_cdk_lib_1.aws_s3.BlockPublicAccess.BLOCK_ALL,
61
- // Allow bucket to auto delete upon cdk:Destroy
62
- removalPolicy: cdk.RemovalPolicy.DESTROY,
63
- autoDeleteObjects: true,
64
- });
65
- return {
66
- driveName,
67
- bucketName: bucketName,
68
- bucket: bucket,
69
- };
70
- });
71
- // This should be all resource names
72
- const runtimeConfigLambdaConfig = {
73
- resourceNameMap: buckets.reduce((acc, b) => (Object.assign(Object.assign({}, acc), { [b.driveName]: b.bucketName })), {}),
74
- };
75
- const lambdaHandler = new aws_cdk_lib_1.aws_lambda_nodejs.NodejsFunction(this, `${settings.environment}-${settings.service}-lambda-rest`, {
76
- functionName: `lambda-rest-${settings.service}-${settings.environment}`,
77
- entry: props.routeEntry.src,
78
- handler: props.routeEntry.runtime,
79
- timeout: cdk.Duration.seconds(25),
80
- runtime: aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X,
81
- layers: [BLLayer],
82
- bundling: {
83
- tsconfig: './tsconfig.json',
84
- define: {
85
- 'process.env.lambdaRuntimeConfig': JSON.stringify(JSON.stringify(runtimeConfigLambdaConfig)),
86
- },
87
- },
88
- environment: {
89
- TABLES: JSON.stringify([]),
90
- },
91
- });
92
- // Grant access to the lambda
93
- buckets.forEach((b) => b.bucket.grantReadWrite(lambdaHandler));
94
- ownedSecrets.forEach((os) => os.grantRead(lambdaHandler));
95
- // Create a rest api
96
- const api = new aws_cdk_lib_1.aws_apigateway.LambdaRestApi(this, `${settings.environment}-${settings.service}-http-api`, {
97
- restApiName: `${settings.environment}-${settings.service}-http-api`,
98
- handler: lambdaHandler,
99
- deployOptions: {
100
- loggingLevel: aws_cdk_lib_1.aws_apigateway.MethodLoggingLevel.INFO,
101
- dataTraceEnabled: true,
102
- },
103
- proxy: true,
104
- });
105
- quidproquo_core_1.qpqCoreUtils.getScheduleEvents(props.qpqConfig).forEach((se, index) => {
106
- const schedulerFunction = new aws_cdk_lib_1.aws_lambda_nodejs.NodejsFunction(this, `${settings.environment}-${settings.service}-SE-${index}`, {
107
- functionName: `SE-${index}-${se.runtime}-${settings.environment}-${settings.service}`,
108
- entry: props.eventEntry.src,
109
- handler: props.eventEntry.runtime,
110
- timeout: cdk.Duration.minutes(15),
111
- runtime: aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_16_X,
112
- memorySize: 400,
113
- layers: [BLLayer],
114
- bundling: {
115
- tsconfig: './tsconfig.json',
116
- define: {
117
- 'process.env.lambdaRuntimeConfig': JSON.stringify(JSON.stringify(Object.assign(Object.assign({}, runtimeConfigLambdaConfig), { lambdaRuntimeConfig: {
118
- src: se.src,
119
- runtime: se.runtime,
120
- } }))),
121
- },
122
- },
123
- environment: {
124
- TABLES: JSON.stringify([]),
125
- },
126
- });
127
- buckets.forEach((b) => b.bucket.grantReadWrite(schedulerFunction));
128
- ownedSecrets.forEach((os) => os.grantRead(schedulerFunction));
129
- // EventBridge rule which runs every five minutes
130
- const cronRule = new aws_cdk_lib_1.aws_events.Rule(this, `se-cronrule-${index}`, {
131
- schedule: aws_cdk_lib_1.aws_events.Schedule.expression(`cron(${se.cronExpression})`),
132
- });
133
- // Set the targert as lambda function
134
- cronRule.addTarget(new aws_cdk_lib_1.aws_events_targets.LambdaFunction(schedulerFunction));
135
- });
136
- }
137
- }
138
- exports.QPQPrototypeStack = QPQPrototypeStack;