carlin 3.2.1 → 5.0.0
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/dist/defineConfig.d.mts +11 -15
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1145 -958
- package/package.json +32 -32
package/dist/index.mjs
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
import { builtinModules, createRequire } from "node:module";
|
|
2
|
-
import * as path$
|
|
3
|
-
import
|
|
4
|
-
import * as fs$3 from "node:fs";
|
|
2
|
+
import path, * as path$1 from "node:path";
|
|
3
|
+
import * as fs$1 from "node:fs";
|
|
5
4
|
import fs, { chmodSync, createReadStream, existsSync, statSync } from "node:fs";
|
|
6
|
-
import
|
|
5
|
+
import { CORE_SCHEMA, defineScalarTag, defineSequenceTag, dump, load, mergeTag } from "js-yaml";
|
|
7
6
|
import * as esbuild from "esbuild";
|
|
8
|
-
import AWS from "aws-sdk";
|
|
9
7
|
import { camelCase, constantCase, kebabCase, pascalCase } from "change-case";
|
|
10
8
|
import deepEqual from "deep-equal";
|
|
11
|
-
import
|
|
9
|
+
import deepmerge from "deepmerge";
|
|
12
10
|
import dotenv from "dotenv";
|
|
13
11
|
import findUpSync from "findup-sync";
|
|
14
12
|
import yargs from "yargs";
|
|
15
13
|
import { hideBin } from "yargs/helpers";
|
|
16
|
-
import
|
|
17
|
-
import "
|
|
14
|
+
import { InvokeCommand, LambdaClient } from "@aws-sdk/client-lambda";
|
|
15
|
+
import { format, styleText } from "node:util";
|
|
16
|
+
import { BatchGetBuildsCommand, CodeBuildClient, StartBuildCommand } from "@aws-sdk/client-codebuild";
|
|
17
|
+
import { spawn } from "node:child_process";
|
|
18
18
|
import "prettier";
|
|
19
|
-
import
|
|
20
|
-
import
|
|
21
|
-
import
|
|
22
|
-
import { CloudFormationClient, CreateStackCommand, DeleteStackCommand, DescribeStackEventsCommand, DescribeStackResourceCommand, DescribeStacksCommand, UpdateStackCommand, UpdateTerminationProtectionCommand, ValidateTemplateCommand } from "@aws-sdk/client-cloudformation";
|
|
19
|
+
import "uglify-js";
|
|
20
|
+
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
|
|
21
|
+
import { simpleGit } from "simple-git";
|
|
22
|
+
import { CloudFormationClient, CreateStackCommand, DeleteStackCommand, DescribeStackEventsCommand, DescribeStackResourceCommand, DescribeStacksCommand, ListStackResourcesCommand, UpdateStackCommand, UpdateTerminationProtectionCommand, ValidateTemplateCommand, waitUntilStackCreateComplete, waitUntilStackDeleteComplete, waitUntilStackUpdateComplete } from "@aws-sdk/client-cloudformation";
|
|
23
23
|
import { CopyObjectCommand, DeleteObjectsCommand, HeadObjectCommand, ListObjectVersionsCommand, ListObjectsV2Command, S3Client } from "@aws-sdk/client-s3";
|
|
24
24
|
import { Upload } from "@aws-sdk/lib-storage";
|
|
25
25
|
import { glob } from "glob";
|
|
26
26
|
import mime from "mime-types";
|
|
27
|
-
import * as path$1 from "path";
|
|
28
27
|
import { typescriptConfig } from "@ttoss/config";
|
|
29
28
|
import AdmZip from "adm-zip";
|
|
30
|
-
import {
|
|
29
|
+
import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-cloudfront";
|
|
31
30
|
//#region \0rolldown/runtime.js
|
|
32
|
-
var __require = /*
|
|
31
|
+
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
33
32
|
//#endregion
|
|
34
33
|
//#region ../read-config-file/src/loadConfig.ts
|
|
35
34
|
const nodeRequire = createRequire(import.meta.url);
|
|
@@ -73,7 +72,7 @@ const readConfigFileSync = ({ configFilePath, options }) => {
|
|
|
73
72
|
const extension = configFilePath.split(".").pop();
|
|
74
73
|
if (extension === "yaml" || extension === "yml") {
|
|
75
74
|
const file = fs.readFileSync(configFilePath, "utf8");
|
|
76
|
-
return
|
|
75
|
+
return load(file, { schema: CORE_SCHEMA.withTags(mergeTag) });
|
|
77
76
|
}
|
|
78
77
|
if (extension === "json") {
|
|
79
78
|
const file = fs.readFileSync(configFilePath, "utf8");
|
|
@@ -113,6 +112,94 @@ const CLOUDFRONT_REGION = "us-east-1";
|
|
|
113
112
|
*/
|
|
114
113
|
const DEFAULT_NODE_RUNTIME = `nodejs24.x`;
|
|
115
114
|
//#endregion
|
|
115
|
+
//#region src/utils/environmentVariables.ts
|
|
116
|
+
const cache = /* @__PURE__ */ new Map();
|
|
117
|
+
const getEnvVar = (key) => {
|
|
118
|
+
return cache.has(key) && cache.get(key) ? cache.get(key) : void 0;
|
|
119
|
+
};
|
|
120
|
+
const setEnvVar = (key, value) => {
|
|
121
|
+
if (!value) return cache.delete(key);
|
|
122
|
+
return cache.set(key, value);
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/utils/log.ts
|
|
126
|
+
const levels = {
|
|
127
|
+
info: {
|
|
128
|
+
label: "info",
|
|
129
|
+
color: "green"
|
|
130
|
+
},
|
|
131
|
+
warn: {
|
|
132
|
+
label: "WARN",
|
|
133
|
+
color: "yellow"
|
|
134
|
+
},
|
|
135
|
+
error: {
|
|
136
|
+
label: "ERR!",
|
|
137
|
+
color: "red"
|
|
138
|
+
},
|
|
139
|
+
event: {
|
|
140
|
+
label: "event",
|
|
141
|
+
color: "yellow"
|
|
142
|
+
},
|
|
143
|
+
output: {
|
|
144
|
+
label: "output",
|
|
145
|
+
color: "blue"
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Color only when stderr supports it, honoring `NO_COLOR` and `FORCE_COLOR`.
|
|
150
|
+
*/
|
|
151
|
+
const paint = ({ color, text }) => {
|
|
152
|
+
return styleText(color, text, { stream: process.stderr });
|
|
153
|
+
};
|
|
154
|
+
const write = ({ level, prefix, args }) => {
|
|
155
|
+
const { label, color } = levels[level];
|
|
156
|
+
const header = [paint({
|
|
157
|
+
color,
|
|
158
|
+
text: label
|
|
159
|
+
}), prefix ? paint({
|
|
160
|
+
color: "magenta",
|
|
161
|
+
text: String(prefix)
|
|
162
|
+
}) : ""].filter(Boolean).join(" ");
|
|
163
|
+
/**
|
|
164
|
+
* Every line of a multi-line message carries the level and the prefix, so
|
|
165
|
+
* interleaved output stays attributable.
|
|
166
|
+
*/
|
|
167
|
+
const lines = format(...args).split("\n").map((line) => {
|
|
168
|
+
return line ? `${header} ${line}` : header;
|
|
169
|
+
});
|
|
170
|
+
/**
|
|
171
|
+
* Logs go to stderr so stdout stays clean for command output.
|
|
172
|
+
*/
|
|
173
|
+
console.error(lines.join("\n"));
|
|
174
|
+
};
|
|
175
|
+
const createLevel = (level) => {
|
|
176
|
+
return (prefix, ...args) => {
|
|
177
|
+
write({
|
|
178
|
+
level,
|
|
179
|
+
prefix,
|
|
180
|
+
args
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Minimal logger with the `log.<level>(prefix, message, ...args)` signature.
|
|
186
|
+
* Messages are formatted with `util.format` and written to stderr as
|
|
187
|
+
* `<level> <prefix> <message>`.
|
|
188
|
+
*/
|
|
189
|
+
const log = {
|
|
190
|
+
info: createLevel("info"),
|
|
191
|
+
warn: createLevel("warn"),
|
|
192
|
+
error: createLevel("error"),
|
|
193
|
+
/**
|
|
194
|
+
* CloudFormation stack events.
|
|
195
|
+
*/
|
|
196
|
+
event: createLevel("event"),
|
|
197
|
+
/**
|
|
198
|
+
* CloudFormation stack outputs.
|
|
199
|
+
*/
|
|
200
|
+
output: createLevel("output")
|
|
201
|
+
};
|
|
202
|
+
//#endregion
|
|
116
203
|
//#region src/deploy/cicd/ecsTaskReportCommand.ts
|
|
117
204
|
const logPrefix$20 = "cicd-ecs-task-report";
|
|
118
205
|
/**
|
|
@@ -125,14 +212,19 @@ const sendEcsTaskReport = async ({ status }) => {
|
|
|
125
212
|
log.info(logPrefix$20, "ECS_TASK_REPORT_HANDLER_NAME not defined.");
|
|
126
213
|
return;
|
|
127
214
|
}
|
|
128
|
-
const lambda = new
|
|
215
|
+
const lambda = new LambdaClient({ region: getEnvVar("REGION") });
|
|
129
216
|
const payload = { status };
|
|
130
217
|
if (process.env.ECS_TASK_ARN) payload.ecsTaskArn = process.env.ECS_TASK_ARN;
|
|
131
218
|
if (process.env.PIPELINE_NAME) payload.pipelineName = process.env.PIPELINE_NAME;
|
|
132
|
-
|
|
219
|
+
/**
|
|
220
|
+
* `Event` invokes the function asynchronously, replacing the deprecated
|
|
221
|
+
* InvokeAsync API.
|
|
222
|
+
*/
|
|
223
|
+
await lambda.send(new InvokeCommand({
|
|
133
224
|
FunctionName: process.env.ECS_TASK_REPORT_HANDLER_NAME,
|
|
134
|
-
|
|
135
|
-
|
|
225
|
+
InvocationType: "Event",
|
|
226
|
+
Payload: JSON.stringify(payload)
|
|
227
|
+
}));
|
|
136
228
|
log.info(logPrefix$20, "Report sent.");
|
|
137
229
|
};
|
|
138
230
|
const options$7 = { status: {
|
|
@@ -154,29 +246,30 @@ const ecsTaskReportCommand = {
|
|
|
154
246
|
return yargs.options(options$7);
|
|
155
247
|
},
|
|
156
248
|
handler: async (args) => {
|
|
157
|
-
return sendEcsTaskReport(args);
|
|
249
|
+
return sendEcsTaskReport({ status: args.status });
|
|
158
250
|
}
|
|
159
251
|
};
|
|
160
252
|
//#endregion
|
|
161
253
|
//#region src/utils/addGroupToOptions.ts
|
|
162
254
|
const addGroupToOptions = (options, group) => {
|
|
163
|
-
Object.values(options)
|
|
164
|
-
option.group = group;
|
|
165
|
-
});
|
|
255
|
+
for (const option of Object.values(options)) option.group = group;
|
|
166
256
|
return options;
|
|
167
257
|
};
|
|
168
258
|
//#endregion
|
|
169
259
|
//#region src/utils/codeBuild.ts
|
|
170
260
|
const logPrefix$19 = "codebuild";
|
|
171
|
-
const WAIT_TIME =
|
|
261
|
+
const WAIT_TIME = 1e4;
|
|
262
|
+
const codeBuild = () => {
|
|
263
|
+
return new CodeBuildClient({ region: getEnvVar("REGION") });
|
|
264
|
+
};
|
|
172
265
|
/**
|
|
173
266
|
* @param param.name name used to identify the build.
|
|
174
267
|
*/
|
|
175
268
|
const waitCodeBuildFinish = async ({ buildId, name }) => {
|
|
176
|
-
const
|
|
269
|
+
const client = codeBuild();
|
|
177
270
|
let result;
|
|
178
271
|
const checkIfBuildIsFinished = async () => {
|
|
179
|
-
const { builds } = await
|
|
272
|
+
const { builds } = await client.send(new BatchGetBuildsCommand({ ids: [buildId] }));
|
|
180
273
|
return new Promise((resolve, reject) => {
|
|
181
274
|
setTimeout(() => {
|
|
182
275
|
const executedBuild = builds?.find(({ id }) => {
|
|
@@ -195,27 +288,14 @@ const waitCodeBuildFinish = async ({ buildId, name }) => {
|
|
|
195
288
|
return result;
|
|
196
289
|
};
|
|
197
290
|
const startCodeBuildBuild = async ({ projectName }) => {
|
|
198
|
-
const { build } = await
|
|
291
|
+
const { build } = await codeBuild().send(new StartBuildCommand({ projectName }));
|
|
199
292
|
if (!build) throw new Error(`Cannot start ${projectName} build`);
|
|
200
293
|
return build;
|
|
201
294
|
};
|
|
202
295
|
//#endregion
|
|
203
|
-
//#region src/utils/environmentVariables.ts
|
|
204
|
-
const cache = /* @__PURE__ */ new Map();
|
|
205
|
-
const getEnvVar = (key) => {
|
|
206
|
-
return cache.has(key) && cache.get(key) ? cache.get(key) : void 0;
|
|
207
|
-
};
|
|
208
|
-
const setEnvVar = (key, value) => {
|
|
209
|
-
if (!value) return cache.delete(key);
|
|
210
|
-
return cache.set(key, value);
|
|
211
|
-
};
|
|
212
|
-
//#endregion
|
|
213
|
-
//#region src/utils/exec.ts
|
|
214
|
-
log.heading = "exec";
|
|
215
|
-
//#endregion
|
|
216
296
|
//#region src/utils/getAwsAccountId.ts
|
|
217
297
|
const getAwsAccountId = async () => {
|
|
218
|
-
const { Account } = await new
|
|
298
|
+
const { Account } = await new STSClient({ region: getEnvVar("REGION") }).send(new GetCallerIdentityCommand({}));
|
|
219
299
|
return Account;
|
|
220
300
|
};
|
|
221
301
|
/**
|
|
@@ -234,9 +314,9 @@ const getAwsAccountId = async () => {
|
|
|
234
314
|
const getCurrentBranch = async () => {
|
|
235
315
|
try {
|
|
236
316
|
if (getEnvVar("BRANCH")) return getEnvVar("BRANCH");
|
|
237
|
-
const { current } = await
|
|
317
|
+
const { current } = await simpleGit().branch();
|
|
238
318
|
return current || "";
|
|
239
|
-
} catch
|
|
319
|
+
} catch {
|
|
240
320
|
return "";
|
|
241
321
|
}
|
|
242
322
|
};
|
|
@@ -247,13 +327,15 @@ const getEnvironment = () => {
|
|
|
247
327
|
};
|
|
248
328
|
//#endregion
|
|
249
329
|
//#region src/utils/getIamPath.ts
|
|
250
|
-
const getIamPath = () =>
|
|
330
|
+
const getIamPath = () => {
|
|
331
|
+
return `/${NAME}/`;
|
|
332
|
+
};
|
|
251
333
|
//#endregion
|
|
252
334
|
//#region src/utils/packageJson.ts
|
|
253
335
|
const readPackageJson = () => {
|
|
254
336
|
const packageJsonDir = findUpSync("package.json");
|
|
255
337
|
if (!packageJsonDir) return {};
|
|
256
|
-
return JSON.parse(fs
|
|
338
|
+
return JSON.parse(fs.readFileSync(packageJsonDir).toString());
|
|
257
339
|
};
|
|
258
340
|
const getPackageJsonProperty = ({ property }) => {
|
|
259
341
|
try {
|
|
@@ -289,14 +371,11 @@ const getProjectName = () => {
|
|
|
289
371
|
if (!name) return "";
|
|
290
372
|
try {
|
|
291
373
|
return pascalCase(name.split(/[@/]/)[1]);
|
|
292
|
-
} catch
|
|
374
|
+
} catch {
|
|
293
375
|
return pascalCase(name);
|
|
294
376
|
}
|
|
295
377
|
};
|
|
296
378
|
//#endregion
|
|
297
|
-
//#region src/utils/spawn.ts
|
|
298
|
-
log.heading = "exec";
|
|
299
|
-
//#endregion
|
|
300
379
|
//#region src/deploy/addDefaults.cloudformation.ts
|
|
301
380
|
const addDefaultsParametersAndTagsToParams = async (params) => {
|
|
302
381
|
const branchName = await getCurrentBranch();
|
|
@@ -411,9 +490,7 @@ const CRITICAL_RESOURCES_TYPES = ["AWS::Cognito::UserPool", "AWS::DynamoDB::Tabl
|
|
|
411
490
|
*/
|
|
412
491
|
const addRetainToCriticalResources = async (template) => {
|
|
413
492
|
const environment = getEnvironment();
|
|
414
|
-
for (const [, resource] of Object.entries(template.Resources)) if (CRITICAL_RESOURCES_TYPES.includes(resource.Type))
|
|
415
|
-
if (!resource.DeletionPolicy && environment) resource.DeletionPolicy = "Retain";
|
|
416
|
-
}
|
|
493
|
+
for (const [, resource] of Object.entries(template.Resources)) if (CRITICAL_RESOURCES_TYPES.includes(resource.Type) && !resource.DeletionPolicy && environment) resource.DeletionPolicy = "Retain";
|
|
417
494
|
};
|
|
418
495
|
/**
|
|
419
496
|
* Base URL for the AWS AppSync Console page.
|
|
@@ -503,14 +580,6 @@ const getBaseStackResource = async (resource) => {
|
|
|
503
580
|
return resources[resource];
|
|
504
581
|
};
|
|
505
582
|
//#endregion
|
|
506
|
-
//#region src/deploy/config.ts
|
|
507
|
-
/**
|
|
508
|
-
* Besides saving the deploy information in the `$STACK_NAME.json` file,
|
|
509
|
-
* we also save in a "latest-deploy.json" file to be used by tests and other
|
|
510
|
-
* packages that need to know the latest deploy information.
|
|
511
|
-
*/
|
|
512
|
-
const LATEST_DEPLOY_OUTPUTS_FILENAME = "latest-deploy.json";
|
|
513
|
-
//#endregion
|
|
514
583
|
//#region src/deploy/s3.ts
|
|
515
584
|
const logPrefix$18 = "s3";
|
|
516
585
|
/**
|
|
@@ -689,9 +758,10 @@ const emptyS3Directory = async ({ bucket, directory = "" }) => {
|
|
|
689
758
|
*/
|
|
690
759
|
const BATCH_SIZE = 1e3;
|
|
691
760
|
for (let i = 0; i < objectsWithVersionsIds.length; i += BATCH_SIZE) {
|
|
761
|
+
const batch = objectsWithVersionsIds.slice(i, i + BATCH_SIZE);
|
|
692
762
|
const deleteCommand = new DeleteObjectsCommand({
|
|
693
763
|
Bucket: bucket,
|
|
694
|
-
Delete: { Objects:
|
|
764
|
+
Delete: { Objects: batch }
|
|
695
765
|
});
|
|
696
766
|
const result = await s3().send(deleteCommand);
|
|
697
767
|
if (result.Errors && result.Errors.length > 0) {
|
|
@@ -752,9 +822,10 @@ const deleteOldS3Files = async ({ bucket, continuationToken, directory = "", ret
|
|
|
752
822
|
*/
|
|
753
823
|
const BATCH_SIZE = 1e3;
|
|
754
824
|
for (let i = 0; i < oldFiles.length; i += BATCH_SIZE) {
|
|
825
|
+
const batch = oldFiles.slice(i, i + BATCH_SIZE);
|
|
755
826
|
const deleteCommand = new DeleteObjectsCommand({
|
|
756
827
|
Bucket: bucket,
|
|
757
|
-
Delete: { Objects:
|
|
828
|
+
Delete: { Objects: batch.map((Key) => {
|
|
758
829
|
return { Key };
|
|
759
830
|
}) }
|
|
760
831
|
});
|
|
@@ -787,10 +858,39 @@ const deleteOldS3Files = async ({ bucket, continuationToken, directory = "", ret
|
|
|
787
858
|
}
|
|
788
859
|
};
|
|
789
860
|
//#endregion
|
|
861
|
+
//#region src/deploy/config.ts
|
|
862
|
+
/**
|
|
863
|
+
* Besides saving the deploy information in the `$STACK_NAME.json` file,
|
|
864
|
+
* we also save in a "latest-deploy.json" file to be used by tests and other
|
|
865
|
+
* packages that need to know the latest deploy information.
|
|
866
|
+
*/
|
|
867
|
+
const LATEST_DEPLOY_OUTPUTS_FILENAME = "latest-deploy.json";
|
|
868
|
+
//#endregion
|
|
869
|
+
//#region src/deploy/saveEnvironmentOutput.ts
|
|
870
|
+
const saveEnvironmentOutput = async ({ outputs, stackName }) => {
|
|
871
|
+
const envFile = {
|
|
872
|
+
stackName,
|
|
873
|
+
environment: getEnvironment(),
|
|
874
|
+
projectName: getProjectName(),
|
|
875
|
+
packageName: getPackageName()
|
|
876
|
+
};
|
|
877
|
+
envFile.outputs = outputs.reduce((acc, output) => {
|
|
878
|
+
if (!output.OutputKey || !output) return acc;
|
|
879
|
+
return {
|
|
880
|
+
...acc,
|
|
881
|
+
[output.OutputKey]: output
|
|
882
|
+
};
|
|
883
|
+
}, {});
|
|
884
|
+
const dotCarlinFolderPath = path$1.join(process.cwd(), ".carlin");
|
|
885
|
+
if (!fs$1.existsSync(dotCarlinFolderPath)) await fs$1.promises.mkdir(dotCarlinFolderPath);
|
|
886
|
+
const filePath = path$1.join(dotCarlinFolderPath, `${stackName}.json`);
|
|
887
|
+
await fs$1.promises.writeFile(filePath, JSON.stringify(envFile, null, 2));
|
|
888
|
+
const latestFilePath = path$1.join(dotCarlinFolderPath, LATEST_DEPLOY_OUTPUTS_FILENAME);
|
|
889
|
+
await fs$1.promises.writeFile(latestFilePath, JSON.stringify(envFile, null, 2));
|
|
890
|
+
};
|
|
891
|
+
//#endregion
|
|
790
892
|
//#region src/deploy/cloudformation.core.ts
|
|
791
893
|
const logPrefix$17 = "cloudformation";
|
|
792
|
-
log.addLevel("event", 1e4, { fg: "yellow" });
|
|
793
|
-
log.addLevel("output", 1e4, { fg: "blue" });
|
|
794
894
|
/**
|
|
795
895
|
* https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html
|
|
796
896
|
*/
|
|
@@ -804,8 +904,9 @@ const isTemplateBodyGreaterThanMaxSize = (template) => {
|
|
|
804
904
|
* @param input.template: CloudFormation template.
|
|
805
905
|
*/
|
|
806
906
|
const uploadTemplateToBaseStackBucket = async ({ stackName, template }) => {
|
|
907
|
+
const bucketName = await getBaseStackResource("BASE_STACK_BUCKET_LOGICAL_NAME");
|
|
807
908
|
const { url } = await uploadFileToS3({
|
|
808
|
-
bucket:
|
|
909
|
+
bucket: bucketName,
|
|
809
910
|
contentType: "application/json",
|
|
810
911
|
key: `${BASE_STACK_BUCKET_TEMPLATES_FOLDER}/${stackName}.json`,
|
|
811
912
|
file: Buffer.from(JSON.stringify(template, null, 2))
|
|
@@ -829,8 +930,16 @@ const cloudformation = () => {
|
|
|
829
930
|
});
|
|
830
931
|
return cloudFormationClients[key];
|
|
831
932
|
};
|
|
832
|
-
|
|
833
|
-
|
|
933
|
+
/**
|
|
934
|
+
* Stack waiters poll every 30 seconds by default. One hour matches the
|
|
935
|
+
* timeout of the AWS SDK v2 waiters carlin used before.
|
|
936
|
+
*/
|
|
937
|
+
const STACK_WAITER_MAX_WAIT_TIME_IN_SECONDS = 3600;
|
|
938
|
+
const getStackWaiterParams = () => {
|
|
939
|
+
return {
|
|
940
|
+
client: cloudformation(),
|
|
941
|
+
maxWaitTime: STACK_WAITER_MAX_WAIT_TIME_IN_SECONDS
|
|
942
|
+
};
|
|
834
943
|
};
|
|
835
944
|
const describeStacks = async ({ stackName } = {}) => {
|
|
836
945
|
const { Stacks } = await cloudformation().send(new DescribeStacksCommand({ StackName: stackName }));
|
|
@@ -857,7 +966,7 @@ const describeStackEvents = async ({ stackName }) => {
|
|
|
857
966
|
log.error(logPrefix$17, "Stack events:");
|
|
858
967
|
const { StackEvents } = await cloudformation().send(new DescribeStackEventsCommand({ StackName: stackName }));
|
|
859
968
|
const events = (StackEvents || []).filter(({ Timestamp }) => {
|
|
860
|
-
return Date.now() - Number(Timestamp) <
|
|
969
|
+
return Date.now() - Number(Timestamp) < 6e5;
|
|
861
970
|
}).filter(({ ResourceStatusReason }) => {
|
|
862
971
|
return ResourceStatusReason;
|
|
863
972
|
}).reverse();
|
|
@@ -877,27 +986,6 @@ const getStackOutput = async ({ stackName, outputKey }) => {
|
|
|
877
986
|
if (!output) throw new Error(`Output ${outputKey} doesn't exist on ${stackName} stack`);
|
|
878
987
|
return output;
|
|
879
988
|
};
|
|
880
|
-
const saveEnvironmentOutput = async ({ outputs, stackName }) => {
|
|
881
|
-
const envFile = {
|
|
882
|
-
stackName,
|
|
883
|
-
environment: getEnvironment(),
|
|
884
|
-
projectName: getProjectName(),
|
|
885
|
-
packageName: getPackageName()
|
|
886
|
-
};
|
|
887
|
-
envFile.outputs = outputs.reduce((acc, output) => {
|
|
888
|
-
if (!output.OutputKey || !output) return acc;
|
|
889
|
-
return {
|
|
890
|
-
...acc,
|
|
891
|
-
[output.OutputKey]: output
|
|
892
|
-
};
|
|
893
|
-
}, {});
|
|
894
|
-
const dotCarlinFolderPath = path$2.join(process.cwd(), ".carlin");
|
|
895
|
-
if (!fs$3.existsSync(dotCarlinFolderPath)) await fs$3.promises.mkdir(dotCarlinFolderPath);
|
|
896
|
-
const filePath = path$2.join(dotCarlinFolderPath, `${stackName}.json`);
|
|
897
|
-
await fs$3.promises.writeFile(filePath, JSON.stringify(envFile, null, 2));
|
|
898
|
-
const latestFilePath = path$2.join(dotCarlinFolderPath, LATEST_DEPLOY_OUTPUTS_FILENAME);
|
|
899
|
-
await fs$3.promises.writeFile(latestFilePath, JSON.stringify(envFile, null, 2));
|
|
900
|
-
};
|
|
901
989
|
/**
|
|
902
990
|
* After deployment, Carlin prints the outputs defined in your CloudFormation
|
|
903
991
|
* template and saves them in two files:
|
|
@@ -934,7 +1022,7 @@ const deleteStack = async ({ stackName }) => {
|
|
|
934
1022
|
log.info(logPrefix$17, `Deleting stack ${stackName}...`);
|
|
935
1023
|
await cloudformation().send(new DeleteStackCommand({ StackName: stackName }));
|
|
936
1024
|
try {
|
|
937
|
-
await
|
|
1025
|
+
await waitUntilStackDeleteComplete(getStackWaiterParams(), { StackName: stackName });
|
|
938
1026
|
} catch (error) {
|
|
939
1027
|
log.error(logPrefix$17, `An error occurred when deleting stack ${stackName}.`);
|
|
940
1028
|
await describeStackEvents({ stackName });
|
|
@@ -947,7 +1035,7 @@ const createStack = async ({ params }) => {
|
|
|
947
1035
|
log.info(logPrefix$17, `Creating stack ${stackName}...`);
|
|
948
1036
|
await cloudformation().send(new CreateStackCommand(params));
|
|
949
1037
|
try {
|
|
950
|
-
await
|
|
1038
|
+
await waitUntilStackCreateComplete(getStackWaiterParams(), { StackName: stackName });
|
|
951
1039
|
} catch (error) {
|
|
952
1040
|
log.error(logPrefix$17, `An error occurred when creating stack ${stackName}.`);
|
|
953
1041
|
await describeStackEvents({ stackName });
|
|
@@ -961,7 +1049,7 @@ const updateStack = async ({ params }) => {
|
|
|
961
1049
|
log.info(logPrefix$17, `Updating stack ${stackName}...`);
|
|
962
1050
|
try {
|
|
963
1051
|
await cloudformation().send(new UpdateStackCommand(params));
|
|
964
|
-
await
|
|
1052
|
+
await waitUntilStackUpdateComplete(getStackWaiterParams(), { StackName: stackName });
|
|
965
1053
|
} catch (error) {
|
|
966
1054
|
if (error.message === "No updates are to be performed.") {
|
|
967
1055
|
log.info(logPrefix$17, error.message);
|
|
@@ -1034,6 +1122,48 @@ const canDestroyStack = async ({ stackName }) => {
|
|
|
1034
1122
|
if (EnableTerminationProtection) return false;
|
|
1035
1123
|
return true;
|
|
1036
1124
|
};
|
|
1125
|
+
const emptyStackBuckets = async ({ stackName }) => {
|
|
1126
|
+
const buckets = [];
|
|
1127
|
+
await (async ({ nextToken }) => {
|
|
1128
|
+
const { NextToken, StackResourceSummaries } = await cloudformation().send(new ListStackResourcesCommand({
|
|
1129
|
+
StackName: stackName,
|
|
1130
|
+
NextToken: nextToken
|
|
1131
|
+
}));
|
|
1132
|
+
if (NextToken) {}
|
|
1133
|
+
for (const { ResourceType, PhysicalResourceId } of StackResourceSummaries || []) if (ResourceType === "AWS::S3::Bucket" && PhysicalResourceId) buckets.push(PhysicalResourceId);
|
|
1134
|
+
})({});
|
|
1135
|
+
return Promise.all(buckets.map((bucket) => {
|
|
1136
|
+
return emptyS3Directory({ bucket });
|
|
1137
|
+
}));
|
|
1138
|
+
};
|
|
1139
|
+
/**
|
|
1140
|
+
* 1. Check if `environment` is defined. If defined, return. It doesn't destroy
|
|
1141
|
+
* stacks with defined `environment`.
|
|
1142
|
+
* 1. Check if termination protection is disabled.
|
|
1143
|
+
* 1. If the stack deployed buckets, empty all buckets.
|
|
1144
|
+
* 1. Delete the stack.
|
|
1145
|
+
*/
|
|
1146
|
+
const destroy = async ({ stackName }) => {
|
|
1147
|
+
const environment = getEnvironment();
|
|
1148
|
+
if (environment) {
|
|
1149
|
+
log.info(logPrefix$17, `Cannot destroy stack when environment (${environment}) is defined.`);
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
if (!await doesStackExist({ stackName })) {
|
|
1153
|
+
log.info(logPrefix$17, `Stack ${stackName} doesn't exist.`);
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
if (!await canDestroyStack({ stackName })) {
|
|
1157
|
+
const message = `Stack ${stackName} cannot be destroyed while TerminationProtection is enabled.`;
|
|
1158
|
+
throw new Error(message);
|
|
1159
|
+
}
|
|
1160
|
+
try {
|
|
1161
|
+
await emptyStackBuckets({ stackName });
|
|
1162
|
+
} catch (error) {
|
|
1163
|
+
log.warn(logPrefix$17, `Failed to empty buckets for stack ${stackName}: ${error?.message || error}. Proceeding with stack deletion.`);
|
|
1164
|
+
}
|
|
1165
|
+
await deleteStack({ stackName });
|
|
1166
|
+
};
|
|
1037
1167
|
const validateTemplate = async ({ stackName, template }) => {
|
|
1038
1168
|
const validateTemplateCommandInput = {};
|
|
1039
1169
|
if (isTemplateBodyGreaterThanMaxSize(template)) {
|
|
@@ -1117,12 +1247,13 @@ const getStackName = async () => {
|
|
|
1117
1247
|
getEnvironment(),
|
|
1118
1248
|
getPackageName()
|
|
1119
1249
|
]);
|
|
1120
|
-
|
|
1250
|
+
const name = [packageName ? pascalCase(packageName) : `Stack-${Math.round(Math.random() * 1e5)}`, (() => {
|
|
1121
1251
|
if (environment) return environment;
|
|
1122
1252
|
if (currentBranch) return kebabCase(currentBranch);
|
|
1123
1253
|
})()].filter((word) => {
|
|
1124
1254
|
return !!word;
|
|
1125
|
-
}).join("-")
|
|
1255
|
+
}).join("-");
|
|
1256
|
+
return limitStackName(sanitizeStackName(name));
|
|
1126
1257
|
};
|
|
1127
1258
|
//#endregion
|
|
1128
1259
|
//#region src/deploy/utils.ts
|
|
@@ -1315,7 +1446,7 @@ const getLambdaImageBuilderTemplate = () => {
|
|
|
1315
1446
|
} },
|
|
1316
1447
|
ServiceRole: { "Fn::GetAtt": [CODE_BUILD_PROJECT_SERVICE_ROLE_LOGICAL_ID, "Arn"] },
|
|
1317
1448
|
Source: {
|
|
1318
|
-
BuildSpec:
|
|
1449
|
+
BuildSpec: dump({
|
|
1319
1450
|
version: "0.2",
|
|
1320
1451
|
phases: {
|
|
1321
1452
|
install: { commands: [
|
|
@@ -1386,202 +1517,230 @@ artifacts:
|
|
|
1386
1517
|
name: $PACKAGE_NAME.zip
|
|
1387
1518
|
`.trim();
|
|
1388
1519
|
};
|
|
1520
|
+
const getS3BucketArn = (suffix) => {
|
|
1521
|
+
return { "Fn::Sub": [`arn:aws:s3:::\${BucketName}${suffix}`, { BucketName: { Ref: BASE_STACK_BUCKET_LOGICAL_NAME } }] };
|
|
1522
|
+
};
|
|
1523
|
+
const getCodeBuildProjectIamRole = () => {
|
|
1524
|
+
return {
|
|
1525
|
+
Type: "AWS::IAM::Role",
|
|
1526
|
+
Properties: {
|
|
1527
|
+
AssumeRolePolicyDocument: {
|
|
1528
|
+
Version: "2012-10-17",
|
|
1529
|
+
Statement: [{
|
|
1530
|
+
Effect: "Allow",
|
|
1531
|
+
Principal: { Service: ["codebuild.amazonaws.com"] },
|
|
1532
|
+
Action: ["sts:AssumeRole"]
|
|
1533
|
+
}]
|
|
1534
|
+
},
|
|
1535
|
+
Path: getIamPath(),
|
|
1536
|
+
Policies: [{
|
|
1537
|
+
PolicyName: `${CODE_BUILD_PROJECT_IAM_ROLE_LOGICAL_ID}Policy`,
|
|
1538
|
+
PolicyDocument: {
|
|
1539
|
+
Version: "2012-10-17",
|
|
1540
|
+
Statement: [{
|
|
1541
|
+
Effect: "Allow",
|
|
1542
|
+
Action: ["logs:CreateLogStream", "logs:PutLogEvents"],
|
|
1543
|
+
Resource: "*"
|
|
1544
|
+
}, {
|
|
1545
|
+
Effect: "Allow",
|
|
1546
|
+
Action: ["s3:*"],
|
|
1547
|
+
Resource: [getS3BucketArn(""), getS3BucketArn("/*")]
|
|
1548
|
+
}]
|
|
1549
|
+
}
|
|
1550
|
+
}]
|
|
1551
|
+
}
|
|
1552
|
+
};
|
|
1553
|
+
};
|
|
1554
|
+
const getCodeBuildProject = ({ runtime }) => {
|
|
1555
|
+
return {
|
|
1556
|
+
Type: "AWS::CodeBuild::Project",
|
|
1557
|
+
Properties: {
|
|
1558
|
+
Artifacts: {
|
|
1559
|
+
Location: { Ref: BASE_STACK_BUCKET_LOGICAL_NAME },
|
|
1560
|
+
NamespaceType: "NONE",
|
|
1561
|
+
OverrideArtifactName: true,
|
|
1562
|
+
Packaging: "ZIP",
|
|
1563
|
+
Path: "lambda-layers/packages",
|
|
1564
|
+
Type: "S3"
|
|
1565
|
+
},
|
|
1566
|
+
Environment: {
|
|
1567
|
+
ComputeType: "BUILD_GENERAL1_SMALL",
|
|
1568
|
+
/**
|
|
1569
|
+
* Image should match the runtime of the buildspec.
|
|
1570
|
+
* https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html
|
|
1571
|
+
*/
|
|
1572
|
+
Image: "aws/codebuild/standard:7.0",
|
|
1573
|
+
Type: "LINUX_CONTAINER"
|
|
1574
|
+
},
|
|
1575
|
+
LogsConfig: { CloudWatchLogs: {
|
|
1576
|
+
GroupName: { Ref: `${CODE_BUILD_PROJECT_LOGS_GROUP_LOGICAL_ID}` },
|
|
1577
|
+
Status: "ENABLED"
|
|
1578
|
+
} },
|
|
1579
|
+
ServiceRole: { "Fn::GetAtt": `${CODE_BUILD_PROJECT_IAM_ROLE_LOGICAL_ID}.Arn` },
|
|
1580
|
+
Source: {
|
|
1581
|
+
BuildSpec: getBuildSpec({ runtime }),
|
|
1582
|
+
Type: "NO_SOURCE"
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
};
|
|
1389
1587
|
/**
|
|
1390
1588
|
* https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html
|
|
1391
1589
|
*/
|
|
1392
1590
|
const getLambdaLayerBuilderTemplate = ({ runtime } = {}) => {
|
|
1393
1591
|
return {
|
|
1394
1592
|
Resources: {
|
|
1395
|
-
[CODE_BUILD_PROJECT_IAM_ROLE_LOGICAL_ID]:
|
|
1396
|
-
Type: "AWS::IAM::Role",
|
|
1397
|
-
Properties: {
|
|
1398
|
-
AssumeRolePolicyDocument: {
|
|
1399
|
-
Version: "2012-10-17",
|
|
1400
|
-
Statement: [{
|
|
1401
|
-
Effect: "Allow",
|
|
1402
|
-
Principal: { Service: ["codebuild.amazonaws.com"] },
|
|
1403
|
-
Action: ["sts:AssumeRole"]
|
|
1404
|
-
}]
|
|
1405
|
-
},
|
|
1406
|
-
Path: getIamPath(),
|
|
1407
|
-
Policies: [{
|
|
1408
|
-
PolicyName: `${CODE_BUILD_PROJECT_IAM_ROLE_LOGICAL_ID}Policy`,
|
|
1409
|
-
PolicyDocument: {
|
|
1410
|
-
Version: "2012-10-17",
|
|
1411
|
-
Statement: [{
|
|
1412
|
-
Effect: "Allow",
|
|
1413
|
-
Action: ["logs:CreateLogStream", "logs:PutLogEvents"],
|
|
1414
|
-
Resource: "*"
|
|
1415
|
-
}, {
|
|
1416
|
-
Effect: "Allow",
|
|
1417
|
-
Action: ["s3:*"],
|
|
1418
|
-
Resource: [{ "Fn::Sub": ["arn:aws:s3:::${BucketName}", { BucketName: { Ref: BASE_STACK_BUCKET_LOGICAL_NAME } }] }, { "Fn::Sub": ["arn:aws:s3:::${BucketName}/*", { BucketName: { Ref: BASE_STACK_BUCKET_LOGICAL_NAME } }] }]
|
|
1419
|
-
}]
|
|
1420
|
-
}
|
|
1421
|
-
}]
|
|
1422
|
-
}
|
|
1423
|
-
},
|
|
1593
|
+
[CODE_BUILD_PROJECT_IAM_ROLE_LOGICAL_ID]: getCodeBuildProjectIamRole(),
|
|
1424
1594
|
[CODE_BUILD_PROJECT_LOGS_GROUP_LOGICAL_ID]: {
|
|
1425
1595
|
Type: "AWS::Logs::LogGroup",
|
|
1426
1596
|
DeletionPolicy: "Delete",
|
|
1427
1597
|
Properties: {}
|
|
1428
1598
|
},
|
|
1429
|
-
[BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME]: {
|
|
1430
|
-
Type: "AWS::CodeBuild::Project",
|
|
1431
|
-
Properties: {
|
|
1432
|
-
Artifacts: {
|
|
1433
|
-
Location: { Ref: BASE_STACK_BUCKET_LOGICAL_NAME },
|
|
1434
|
-
NamespaceType: "NONE",
|
|
1435
|
-
OverrideArtifactName: true,
|
|
1436
|
-
Packaging: "ZIP",
|
|
1437
|
-
Path: "lambda-layers/packages",
|
|
1438
|
-
Type: "S3"
|
|
1439
|
-
},
|
|
1440
|
-
Environment: {
|
|
1441
|
-
ComputeType: "BUILD_GENERAL1_SMALL",
|
|
1442
|
-
/**
|
|
1443
|
-
* Image should match the runtime of the buildspec.
|
|
1444
|
-
* https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html
|
|
1445
|
-
*/
|
|
1446
|
-
Image: "aws/codebuild/standard:7.0",
|
|
1447
|
-
Type: "LINUX_CONTAINER"
|
|
1448
|
-
},
|
|
1449
|
-
LogsConfig: { CloudWatchLogs: {
|
|
1450
|
-
GroupName: { Ref: `${CODE_BUILD_PROJECT_LOGS_GROUP_LOGICAL_ID}` },
|
|
1451
|
-
Status: "ENABLED"
|
|
1452
|
-
} },
|
|
1453
|
-
ServiceRole: { "Fn::GetAtt": `${CODE_BUILD_PROJECT_IAM_ROLE_LOGICAL_ID}.Arn` },
|
|
1454
|
-
Source: {
|
|
1455
|
-
BuildSpec: getBuildSpec({ runtime }),
|
|
1456
|
-
Type: "NO_SOURCE"
|
|
1457
|
-
}
|
|
1458
|
-
}
|
|
1459
|
-
}
|
|
1599
|
+
[BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME]: getCodeBuildProject({ runtime })
|
|
1460
1600
|
},
|
|
1461
1601
|
Outputs: { [BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME]: { Value: { Ref: BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME } } }
|
|
1462
1602
|
};
|
|
1463
1603
|
};
|
|
1464
1604
|
//#endregion
|
|
1465
1605
|
//#region src/deploy/baseStack/getVpcTemplate.ts
|
|
1466
|
-
const
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
CidrBlock: { "Fn::FindInMap": [
|
|
1479
|
-
"CidrMappings",
|
|
1480
|
-
"VPC",
|
|
1481
|
-
"CIDR"
|
|
1482
|
-
] },
|
|
1483
|
-
EnableDnsHostnames: true,
|
|
1484
|
-
EnableDnsSupport: true,
|
|
1485
|
-
Tags: [{
|
|
1486
|
-
Key: "Name",
|
|
1487
|
-
Value: vpcName
|
|
1488
|
-
}]
|
|
1489
|
-
}
|
|
1490
|
-
},
|
|
1491
|
-
[EC2_INTERNET_GATEWAY_LOGICAL_ID]: {
|
|
1492
|
-
Type: "AWS::EC2::InternetGateway",
|
|
1493
|
-
Properties: {}
|
|
1494
|
-
},
|
|
1495
|
-
EC2VPCGatewayAttachment: {
|
|
1496
|
-
Type: "AWS::EC2::VPCGatewayAttachment",
|
|
1497
|
-
Properties: {
|
|
1498
|
-
InternetGatewayId: { Ref: EC2_INTERNET_GATEWAY_LOGICAL_ID },
|
|
1499
|
-
VpcId: { Ref: EC2_VPC_LOGICAL_ID }
|
|
1500
|
-
}
|
|
1501
|
-
},
|
|
1502
|
-
[EC2_ROUTE_TABLE_LOGICAL_ID]: {
|
|
1503
|
-
Type: "AWS::EC2::RouteTable",
|
|
1504
|
-
Properties: {
|
|
1505
|
-
Tags: [{
|
|
1506
|
-
Key: "Name",
|
|
1507
|
-
Value: { "Fn::Join": [" ", [
|
|
1508
|
-
vpcName,
|
|
1509
|
-
"-",
|
|
1510
|
-
EC2_ROUTE_TABLE_LOGICAL_ID
|
|
1511
|
-
]] }
|
|
1512
|
-
}],
|
|
1513
|
-
VpcId: { Ref: EC2_VPC_LOGICAL_ID }
|
|
1514
|
-
}
|
|
1515
|
-
},
|
|
1516
|
-
EC2Route: {
|
|
1517
|
-
Type: "AWS::EC2::Route",
|
|
1518
|
-
Properties: {
|
|
1519
|
-
DestinationCidrBlock: "0.0.0.0/0",
|
|
1520
|
-
GatewayId: { Ref: EC2_INTERNET_GATEWAY_LOGICAL_ID },
|
|
1521
|
-
RouteTableId: { Ref: EC2_ROUTE_TABLE_LOGICAL_ID }
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
},
|
|
1525
|
-
Outputs: {
|
|
1526
|
-
VPCId: {
|
|
1527
|
-
Value: { Ref: EC2_VPC_LOGICAL_ID },
|
|
1528
|
-
Export: { Name: BASE_STACK_VPC_ID_EXPORTED_NAME }
|
|
1529
|
-
},
|
|
1530
|
-
VPCDefaultSecurityGroup: {
|
|
1531
|
-
Value: { "Fn::GetAtt": [EC2_VPC_LOGICAL_ID, "DefaultSecurityGroup"] },
|
|
1532
|
-
Export: { Name: BASE_STACK_VPC_DEFAULT_SECURITY_GROUP_EXPORTED_NAME }
|
|
1533
|
-
}
|
|
1534
|
-
}
|
|
1535
|
-
};
|
|
1536
|
-
for (const [index, publicSubnetExportedName] of [
|
|
1537
|
-
BASE_STACK_VPC_PUBLIC_SUBNET_0_EXPORTED_NAME,
|
|
1538
|
-
BASE_STACK_VPC_PUBLIC_SUBNET_1_EXPORTED_NAME,
|
|
1539
|
-
BASE_STACK_VPC_PUBLIC_SUBNET_2_EXPORTED_NAME
|
|
1540
|
-
].entries()) {
|
|
1541
|
-
const publicSubnetLogicalId = `PublicSubnet${index}EC2Subnet`;
|
|
1542
|
-
const publicSubnetCidrMappings = `PublicSubnet${index}`;
|
|
1543
|
-
if (!template.Mappings) template.Mappings = {};
|
|
1544
|
-
template.Mappings.CidrMappings[publicSubnetCidrMappings] = { CIDR: `10.0.${index}.0/24` };
|
|
1545
|
-
template.Resources[publicSubnetLogicalId] = {
|
|
1546
|
-
Type: "AWS::EC2::Subnet",
|
|
1606
|
+
const EC2_INTERNET_GATEWAY_LOGICAL_ID = "EC2InternetGateway";
|
|
1607
|
+
const EC2_ROUTE_TABLE_LOGICAL_ID = "EC2RouteTable";
|
|
1608
|
+
const EC2_VPC_LOGICAL_ID = "EC2VCP";
|
|
1609
|
+
const PUBLIC_SUBNETS_EXPORTED_NAMES = [
|
|
1610
|
+
BASE_STACK_VPC_PUBLIC_SUBNET_0_EXPORTED_NAME,
|
|
1611
|
+
BASE_STACK_VPC_PUBLIC_SUBNET_1_EXPORTED_NAME,
|
|
1612
|
+
BASE_STACK_VPC_PUBLIC_SUBNET_2_EXPORTED_NAME
|
|
1613
|
+
];
|
|
1614
|
+
const getVpcResources = ({ vpcName }) => {
|
|
1615
|
+
return {
|
|
1616
|
+
[EC2_VPC_LOGICAL_ID]: {
|
|
1617
|
+
Type: "AWS::EC2::VPC",
|
|
1547
1618
|
Properties: {
|
|
1548
|
-
AvailabilityZone: { "Fn::Select": [index, { "Fn::GetAZs": { Ref: "AWS::Region" } }] },
|
|
1549
1619
|
CidrBlock: { "Fn::FindInMap": [
|
|
1550
1620
|
"CidrMappings",
|
|
1551
|
-
|
|
1621
|
+
"VPC",
|
|
1552
1622
|
"CIDR"
|
|
1553
1623
|
] },
|
|
1554
|
-
|
|
1624
|
+
EnableDnsHostnames: true,
|
|
1625
|
+
EnableDnsSupport: true,
|
|
1626
|
+
Tags: [{
|
|
1627
|
+
Key: "Name",
|
|
1628
|
+
Value: vpcName
|
|
1629
|
+
}]
|
|
1630
|
+
}
|
|
1631
|
+
},
|
|
1632
|
+
[EC2_INTERNET_GATEWAY_LOGICAL_ID]: {
|
|
1633
|
+
Type: "AWS::EC2::InternetGateway",
|
|
1634
|
+
Properties: {}
|
|
1635
|
+
},
|
|
1636
|
+
EC2VPCGatewayAttachment: {
|
|
1637
|
+
Type: "AWS::EC2::VPCGatewayAttachment",
|
|
1638
|
+
Properties: {
|
|
1639
|
+
InternetGatewayId: { Ref: EC2_INTERNET_GATEWAY_LOGICAL_ID },
|
|
1640
|
+
VpcId: { Ref: EC2_VPC_LOGICAL_ID }
|
|
1641
|
+
}
|
|
1642
|
+
},
|
|
1643
|
+
[EC2_ROUTE_TABLE_LOGICAL_ID]: {
|
|
1644
|
+
Type: "AWS::EC2::RouteTable",
|
|
1645
|
+
Properties: {
|
|
1555
1646
|
Tags: [{
|
|
1556
1647
|
Key: "Name",
|
|
1557
1648
|
Value: { "Fn::Join": [" ", [
|
|
1558
|
-
|
|
1649
|
+
vpcName,
|
|
1559
1650
|
"-",
|
|
1560
|
-
|
|
1651
|
+
EC2_ROUTE_TABLE_LOGICAL_ID
|
|
1561
1652
|
]] }
|
|
1562
1653
|
}],
|
|
1563
1654
|
VpcId: { Ref: EC2_VPC_LOGICAL_ID }
|
|
1564
1655
|
}
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
Type: "AWS::EC2::
|
|
1656
|
+
},
|
|
1657
|
+
EC2Route: {
|
|
1658
|
+
Type: "AWS::EC2::Route",
|
|
1568
1659
|
Properties: {
|
|
1569
|
-
|
|
1570
|
-
|
|
1660
|
+
DestinationCidrBlock: "0.0.0.0/0",
|
|
1661
|
+
GatewayId: { Ref: EC2_INTERNET_GATEWAY_LOGICAL_ID },
|
|
1662
|
+
RouteTableId: { Ref: EC2_ROUTE_TABLE_LOGICAL_ID }
|
|
1571
1663
|
}
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1664
|
+
}
|
|
1665
|
+
};
|
|
1666
|
+
};
|
|
1667
|
+
const getVpcOutputs = () => {
|
|
1668
|
+
return {
|
|
1669
|
+
VPCId: {
|
|
1670
|
+
Value: { Ref: EC2_VPC_LOGICAL_ID },
|
|
1671
|
+
Export: { Name: BASE_STACK_VPC_ID_EXPORTED_NAME }
|
|
1672
|
+
},
|
|
1673
|
+
VPCDefaultSecurityGroup: {
|
|
1674
|
+
Value: { "Fn::GetAtt": [EC2_VPC_LOGICAL_ID, "DefaultSecurityGroup"] },
|
|
1675
|
+
Export: { Name: BASE_STACK_VPC_DEFAULT_SECURITY_GROUP_EXPORTED_NAME }
|
|
1676
|
+
}
|
|
1677
|
+
};
|
|
1678
|
+
};
|
|
1679
|
+
const getPublicSubnetResource = ({ index, publicSubnetLogicalId, publicSubnetCidrMappings }) => {
|
|
1680
|
+
return {
|
|
1681
|
+
Type: "AWS::EC2::Subnet",
|
|
1682
|
+
Properties: {
|
|
1683
|
+
AvailabilityZone: { "Fn::Select": [index, { "Fn::GetAZs": { Ref: "AWS::Region" } }] },
|
|
1684
|
+
CidrBlock: { "Fn::FindInMap": [
|
|
1685
|
+
"CidrMappings",
|
|
1686
|
+
publicSubnetCidrMappings,
|
|
1687
|
+
"CIDR"
|
|
1688
|
+
] },
|
|
1689
|
+
MapPublicIpOnLaunch: true,
|
|
1690
|
+
Tags: [{
|
|
1691
|
+
Key: "Name",
|
|
1692
|
+
Value: { "Fn::Join": [" ", [
|
|
1693
|
+
EC2_VPC_LOGICAL_ID,
|
|
1694
|
+
"-",
|
|
1695
|
+
publicSubnetLogicalId
|
|
1696
|
+
]] }
|
|
1697
|
+
}],
|
|
1698
|
+
VpcId: { Ref: EC2_VPC_LOGICAL_ID }
|
|
1699
|
+
}
|
|
1700
|
+
};
|
|
1701
|
+
};
|
|
1702
|
+
const addPublicSubnet = ({ template, index, publicSubnetExportedName }) => {
|
|
1703
|
+
const publicSubnetLogicalId = `PublicSubnet${index}EC2Subnet`;
|
|
1704
|
+
const publicSubnetCidrMappings = `PublicSubnet${index}`;
|
|
1705
|
+
if (!template.Mappings) template.Mappings = {};
|
|
1706
|
+
template.Mappings.CidrMappings[publicSubnetCidrMappings] = { CIDR: `10.0.${index}.0/24` };
|
|
1707
|
+
template.Resources[publicSubnetLogicalId] = getPublicSubnetResource({
|
|
1708
|
+
index,
|
|
1709
|
+
publicSubnetLogicalId,
|
|
1710
|
+
publicSubnetCidrMappings
|
|
1711
|
+
});
|
|
1712
|
+
template.Resources[`PublicSubnet${index}EC2SubnetRouteTableAssociation`] = {
|
|
1713
|
+
Type: "AWS::EC2::SubnetRouteTableAssociation",
|
|
1714
|
+
Properties: {
|
|
1715
|
+
RouteTableId: { Ref: EC2_ROUTE_TABLE_LOGICAL_ID },
|
|
1716
|
+
SubnetId: { Ref: publicSubnetLogicalId }
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
if (!template.Outputs) template.Outputs = {};
|
|
1720
|
+
template.Outputs[publicSubnetLogicalId] = {
|
|
1721
|
+
Value: { Ref: publicSubnetLogicalId },
|
|
1722
|
+
Export: { Name: publicSubnetExportedName }
|
|
1723
|
+
};
|
|
1724
|
+
};
|
|
1725
|
+
const getVpcTemplate = () => {
|
|
1726
|
+
const vpcName = `${pascalCase(NAME)}VPC`;
|
|
1727
|
+
const template = {
|
|
1728
|
+
AWSTemplateFormatVersion: "2010-09-09",
|
|
1729
|
+
Mappings: { CidrMappings: { VPC: { CIDR: "10.0.0.0/16" } } },
|
|
1730
|
+
Resources: getVpcResources({ vpcName }),
|
|
1731
|
+
Outputs: getVpcOutputs()
|
|
1732
|
+
};
|
|
1733
|
+
for (const [index, publicSubnetExportedName] of PUBLIC_SUBNETS_EXPORTED_NAMES.entries()) addPublicSubnet({
|
|
1734
|
+
template,
|
|
1735
|
+
index,
|
|
1736
|
+
publicSubnetExportedName
|
|
1737
|
+
});
|
|
1579
1738
|
return template;
|
|
1580
1739
|
};
|
|
1581
1740
|
//#endregion
|
|
1582
1741
|
//#region src/deploy/baseStack/deployBaseStack.ts
|
|
1583
1742
|
const logPrefix$16 = "base-stack";
|
|
1584
|
-
const baseStackTemplate =
|
|
1743
|
+
const baseStackTemplate = deepmerge.all([
|
|
1585
1744
|
getBucketTemplate(),
|
|
1586
1745
|
getCloudFrontTemplate$1(),
|
|
1587
1746
|
getLambdaImageBuilderTemplate(),
|
|
@@ -1709,143 +1868,413 @@ const getCicdConfig = () => {
|
|
|
1709
1868
|
}, {});
|
|
1710
1869
|
};
|
|
1711
1870
|
//#endregion
|
|
1712
|
-
//#region src/deploy/
|
|
1871
|
+
//#region src/deploy/lambda/buildLambdaCode.ts
|
|
1872
|
+
const logPrefix$15 = "lambda";
|
|
1713
1873
|
/**
|
|
1714
|
-
*
|
|
1715
|
-
* will
|
|
1874
|
+
* Carlin builds the Lambda code using esbuild. It can build the code as ESM or
|
|
1875
|
+
* CJS format. When building as ESM, it will split the code into multiple files,
|
|
1876
|
+
* which reduces the payload size when deploying the Lambda function. The file
|
|
1877
|
+
* extension of the output files will be `.mjs` for ESM and `.cjs` for CJS.
|
|
1716
1878
|
*/
|
|
1717
|
-
const
|
|
1718
|
-
|
|
1879
|
+
const buildLambdaCode = async ({ lambdaEntryPoints, lambdaEntryPointsBaseDir = ".", lambdaExternal = [], lambdaFormat = "esm", lambdaOutdir, lambdaSourceMaps = false }) => {
|
|
1880
|
+
log.info(logPrefix$15, "Building Lambda single file...");
|
|
1881
|
+
/**
|
|
1882
|
+
* Remove the output directory if it exists to not mix old files with the
|
|
1883
|
+
* new ones.
|
|
1884
|
+
*/
|
|
1885
|
+
if (fs.existsSync(lambdaOutdir)) fs.rmSync(lambdaOutdir, { recursive: true });
|
|
1886
|
+
const entryPoints = lambdaEntryPoints.map((entryPoint) => {
|
|
1887
|
+
return path.resolve(process.cwd(), lambdaEntryPointsBaseDir, entryPoint);
|
|
1888
|
+
});
|
|
1889
|
+
const { errors } = esbuild.buildSync({
|
|
1890
|
+
banner: { js: "// Powered by carlin (https://ttoss.dev/docs/carlin/)" },
|
|
1891
|
+
bundle: true,
|
|
1892
|
+
entryPoints,
|
|
1893
|
+
external: [
|
|
1894
|
+
"@aws-sdk/*",
|
|
1895
|
+
...builtinModules,
|
|
1896
|
+
...lambdaExternal
|
|
1897
|
+
],
|
|
1898
|
+
/**
|
|
1899
|
+
* Some packages as `graphql` are not compatible with ESM yet.
|
|
1900
|
+
* https://github.com/graphql/graphql-js/issues/3603
|
|
1901
|
+
*/
|
|
1902
|
+
format: lambdaFormat,
|
|
1903
|
+
/**
|
|
1904
|
+
* https://esbuild.github.io/api/#minify
|
|
1905
|
+
*/
|
|
1906
|
+
minifySyntax: true,
|
|
1907
|
+
platform: "node",
|
|
1908
|
+
/**
|
|
1909
|
+
* Node applies the map only with `--enable-source-maps`, which
|
|
1910
|
+
* `addSourceMapsNodeOptions` sets on every function. Without
|
|
1911
|
+
* `sourcesContent`: Node maps a frame from names and lines alone, and the
|
|
1912
|
+
* source text would only grow the zip.
|
|
1913
|
+
*/
|
|
1914
|
+
...lambdaSourceMaps ? {
|
|
1915
|
+
sourcemap: "linked",
|
|
1916
|
+
sourcesContent: false
|
|
1917
|
+
} : {},
|
|
1918
|
+
splitting: lambdaFormat === "esm",
|
|
1919
|
+
outbase: path.join(process.cwd(), lambdaEntryPointsBaseDir),
|
|
1920
|
+
outdir: path.join(process.cwd(), lambdaOutdir),
|
|
1921
|
+
outExtension: { ".js": lambdaFormat === "esm" ? ".mjs" : ".cjs" },
|
|
1922
|
+
target: typescriptConfig.target,
|
|
1923
|
+
treeShaking: true
|
|
1924
|
+
});
|
|
1925
|
+
if (errors.length > 0) throw errors;
|
|
1719
1926
|
};
|
|
1720
1927
|
//#endregion
|
|
1721
|
-
//#region src/deploy/
|
|
1722
|
-
const
|
|
1723
|
-
const
|
|
1724
|
-
const
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
const
|
|
1736
|
-
const
|
|
1737
|
-
|
|
1738
|
-
const
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1928
|
+
//#region src/deploy/lambdaLayer/getPackageLambdaLayerStackName.ts
|
|
1929
|
+
const lambdaLayerStackNamePrefix = `LambdaLayer`;
|
|
1930
|
+
const getPackageLambdaLayerStackName = (packageName) => {
|
|
1931
|
+
const [scopedName, version] = packageName.split("@").filter((part) => {
|
|
1932
|
+
return !!part;
|
|
1933
|
+
});
|
|
1934
|
+
return [
|
|
1935
|
+
lambdaLayerStackNamePrefix,
|
|
1936
|
+
pascalCase(scopedName),
|
|
1937
|
+
version.replace(/[^0-9.]/g, "").replace(/\./g, "-")
|
|
1938
|
+
].join("-");
|
|
1939
|
+
};
|
|
1940
|
+
//#endregion
|
|
1941
|
+
//#region src/deploy/lambdaLayer/deployLambdaLayer.ts
|
|
1942
|
+
const logPrefix$14 = "lambda-layer";
|
|
1943
|
+
const createLambdaLayerZipFile = async ({ codeBuildProjectName, packageName }) => {
|
|
1944
|
+
log.info(logPrefix$14, `Creating zip file for package ${packageName}...`);
|
|
1945
|
+
const { build } = await codeBuild().send(new StartBuildCommand({
|
|
1946
|
+
environmentVariablesOverride: [{
|
|
1947
|
+
name: "PACKAGE_NAME",
|
|
1948
|
+
value: packageName
|
|
1949
|
+
}],
|
|
1950
|
+
projectName: codeBuildProjectName
|
|
1951
|
+
}));
|
|
1952
|
+
if (!build?.id) throw new Error("Cannot start build.");
|
|
1953
|
+
const result = await waitCodeBuildFinish({
|
|
1954
|
+
buildId: build.id,
|
|
1955
|
+
name: packageName
|
|
1956
|
+
});
|
|
1957
|
+
if (result.artifacts?.location) {
|
|
1958
|
+
const location = result.artifacts.location.split("/");
|
|
1959
|
+
const bucket = location.shift()?.replace("arn:aws:s3:::", "");
|
|
1960
|
+
if (!bucket) throw new Error("Cannot retrieve bucket name.");
|
|
1961
|
+
return {
|
|
1962
|
+
bucket,
|
|
1963
|
+
key: location.join("/")
|
|
1964
|
+
};
|
|
1965
|
+
}
|
|
1966
|
+
throw new Error(`Cannot get artifact location for package ${packageName}`);
|
|
1967
|
+
};
|
|
1742
1968
|
/**
|
|
1743
|
-
*
|
|
1744
|
-
*
|
|
1745
|
-
*
|
|
1746
|
-
* with Linux as operational system to build the image.
|
|
1969
|
+
* The CloudFormation template created to deploy a Lambda Layer.
|
|
1970
|
+
*
|
|
1971
|
+
* - The Layer name is the same as the Stack name.
|
|
1747
1972
|
*/
|
|
1748
|
-
const
|
|
1973
|
+
const getLambdaLayerTemplate = ({ bucket, key, packageName, runtime }) => {
|
|
1974
|
+
const description = packageName.substring(0, 256);
|
|
1749
1975
|
return {
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
{
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1976
|
+
AWSTemplateFormatVersion: "2010-09-09",
|
|
1977
|
+
Resources: { LambdaLayer: {
|
|
1978
|
+
Type: "AWS::Lambda::LayerVersion",
|
|
1979
|
+
Properties: {
|
|
1980
|
+
CompatibleRuntimes: [runtime || DEFAULT_NODE_RUNTIME],
|
|
1981
|
+
Content: {
|
|
1982
|
+
S3Bucket: bucket,
|
|
1983
|
+
S3Key: key
|
|
1984
|
+
},
|
|
1985
|
+
Description: description,
|
|
1986
|
+
LayerName: { Ref: "AWS::StackName" }
|
|
1987
|
+
}
|
|
1988
|
+
} },
|
|
1989
|
+
Outputs: { LambdaLayerVersion: {
|
|
1990
|
+
Description: description,
|
|
1991
|
+
Value: { Ref: "LambdaLayer" },
|
|
1992
|
+
Export: { Name: { Ref: "AWS::StackName" } }
|
|
1993
|
+
} }
|
|
1994
|
+
};
|
|
1995
|
+
};
|
|
1996
|
+
const getPackagesThatAreNotDeployed = async ({ packages }) => {
|
|
1997
|
+
return (await Promise.all(packages.map(async (packageName) => {
|
|
1998
|
+
const stackName = getPackageLambdaLayerStackName(packageName);
|
|
1999
|
+
return await doesStackExist({ stackName }) ? "" : packageName;
|
|
2000
|
+
}))).filter((packageName) => {
|
|
2001
|
+
return !!packageName;
|
|
2002
|
+
});
|
|
2003
|
+
};
|
|
2004
|
+
const deployLambdaLayer = async ({ packages, deployIfExists = true, runtime }) => {
|
|
2005
|
+
try {
|
|
2006
|
+
const packagesToBeDeployed = deployIfExists ? packages : await getPackagesThatAreNotDeployed({ packages });
|
|
2007
|
+
if (packagesToBeDeployed.length === 0) return;
|
|
2008
|
+
const codeBuildProjectName = await getBaseStackResource("BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME");
|
|
2009
|
+
if (!codeBuildProjectName) throw new Error("Cannot deploy lambda-layer because AWS CodeBuild project doesn't exist.");
|
|
2010
|
+
const deployLambdaLayerSinglePackage = async (packageName) => {
|
|
2011
|
+
try {
|
|
2012
|
+
const { bucket, key } = await createLambdaLayerZipFile({
|
|
2013
|
+
codeBuildProjectName,
|
|
2014
|
+
packageName
|
|
2015
|
+
});
|
|
2016
|
+
const lambdaLayerTemplate = getLambdaLayerTemplate({
|
|
2017
|
+
packageName,
|
|
2018
|
+
bucket,
|
|
2019
|
+
key,
|
|
2020
|
+
runtime
|
|
2021
|
+
});
|
|
2022
|
+
await deploy({
|
|
2023
|
+
template: lambdaLayerTemplate,
|
|
2024
|
+
terminationProtection: true,
|
|
2025
|
+
params: { StackName: getPackageLambdaLayerStackName(packageName) }
|
|
2026
|
+
});
|
|
2027
|
+
} catch (error) {
|
|
2028
|
+
handleDeployError({
|
|
2029
|
+
error,
|
|
2030
|
+
logPrefix: logPrefix$14
|
|
2031
|
+
});
|
|
2032
|
+
}
|
|
2033
|
+
};
|
|
2034
|
+
await Promise.all(packagesToBeDeployed.map((packageName) => {
|
|
2035
|
+
return deployLambdaLayerSinglePackage(packageName);
|
|
2036
|
+
}));
|
|
2037
|
+
} catch (error) {
|
|
2038
|
+
handleDeployError({
|
|
2039
|
+
error,
|
|
2040
|
+
logPrefix: logPrefix$14
|
|
2041
|
+
});
|
|
2042
|
+
}
|
|
2043
|
+
};
|
|
2044
|
+
//#endregion
|
|
2045
|
+
//#region src/deploy/lambda/deployLambdaLayers.ts
|
|
2046
|
+
const logPrefix$13 = "lambda";
|
|
2047
|
+
const deployLambdaLayers = async ({ lambdaExternal = [] }) => {
|
|
2048
|
+
if (lambdaExternal.length === 0) return;
|
|
2049
|
+
log.info(logPrefix$13, `--lambda-externals [${lambdaExternal.join(", ")}] was found. Creating other layers...`);
|
|
2050
|
+
const { dependencies = {} } = (() => {
|
|
2051
|
+
try {
|
|
2052
|
+
return JSON.parse(fs$1.readFileSync(path$1.resolve(process.cwd(), "package.json"), "utf8"));
|
|
2053
|
+
} catch (error) {
|
|
2054
|
+
log.error(logPrefix$13, "Cannot read package.json. Error message: %j", error.message);
|
|
2055
|
+
return {};
|
|
2056
|
+
}
|
|
2057
|
+
})();
|
|
2058
|
+
const packages = lambdaExternal.map((external) => {
|
|
2059
|
+
try {
|
|
2060
|
+
return `${external}@${dependencies[external].replace(/(~|\^)/g, "")}`;
|
|
2061
|
+
} catch {
|
|
2062
|
+
throw new Error(`Cannot find ${external} on package.json dependencies.`);
|
|
2063
|
+
}
|
|
2064
|
+
});
|
|
2065
|
+
await deployLambdaLayer({
|
|
2066
|
+
packages,
|
|
2067
|
+
deployIfExists: false
|
|
2068
|
+
});
|
|
2069
|
+
};
|
|
2070
|
+
//#endregion
|
|
2071
|
+
//#region src/deploy/lambda/uploadCodeToECR.ts
|
|
2072
|
+
new CodeBuildClient({ region: AWS_DEFAULT_REGION });
|
|
2073
|
+
const uploadCodeToECR = async ({ bucket, key, lambdaExternal, lambdaDockerfile }) => {
|
|
2074
|
+
throw new Error("uploadCodeToECR not finished yet.");
|
|
2075
|
+
};
|
|
2076
|
+
//#endregion
|
|
2077
|
+
//#region src/deploy/lambda/uploadCodeToS3.ts
|
|
2078
|
+
const logPrefix$12 = "lambda";
|
|
2079
|
+
const zipFileName = "lambda.zip";
|
|
2080
|
+
const uploadCodeToS3 = async ({ stackName, lambdaOutdir }) => {
|
|
2081
|
+
log.info(logPrefix$12, `Uploading code to S3...`);
|
|
2082
|
+
const zip = new AdmZip();
|
|
2083
|
+
const zipFile = `${lambdaOutdir}/${zipFileName}`;
|
|
2084
|
+
/**
|
|
2085
|
+
* Check if the zip file already exists and delete it before creating a new.
|
|
2086
|
+
*/
|
|
2087
|
+
if (fs.existsSync(zipFile)) await fs.promises.rm(zipFile);
|
|
2088
|
+
/**
|
|
2089
|
+
* Zip entire directory.
|
|
2090
|
+
*/
|
|
2091
|
+
zip.addLocalFolder(lambdaOutdir);
|
|
2092
|
+
zip.writeZip(`${lambdaOutdir}/${zipFileName}`);
|
|
2093
|
+
const bucketName = await getBaseStackResource("BASE_STACK_BUCKET_LOGICAL_NAME");
|
|
2094
|
+
return uploadFileToS3({
|
|
2095
|
+
bucket: bucketName,
|
|
2096
|
+
contentType: "application/zip",
|
|
2097
|
+
key: `lambdas/${stackName}/${zipFileName}`,
|
|
2098
|
+
file: zip.toBuffer()
|
|
2099
|
+
});
|
|
2100
|
+
};
|
|
2101
|
+
//#endregion
|
|
2102
|
+
//#region src/deploy/lambda/deployLambdaCode.ts
|
|
2103
|
+
const logPrefix$11 = "lambda";
|
|
2104
|
+
const deployLambdaCode = async ({ lambdaDockerfile, lambdaExternal = [], lambdaImage, lambdaEntryPoints, lambdaEntryPointsBaseDir = "src", lambdaFormat, lambdaOutdir = "out", lambdaSourceMaps, stackName }) => {
|
|
2105
|
+
if (!lambdaEntryPoints.length) return {};
|
|
2106
|
+
log.info(logPrefix$11, "Deploying Lambda code...");
|
|
2107
|
+
for (const entryPoint of lambdaEntryPoints) {
|
|
2108
|
+
const entryPointPath = path.resolve(lambdaEntryPointsBaseDir, entryPoint);
|
|
2109
|
+
if (!fs.existsSync(entryPointPath)) throw new Error(`Entry point ${entryPointPath} does not exist.`);
|
|
2110
|
+
}
|
|
2111
|
+
await buildLambdaCode({
|
|
2112
|
+
lambdaExternal,
|
|
2113
|
+
lambdaEntryPoints,
|
|
2114
|
+
lambdaEntryPointsBaseDir,
|
|
2115
|
+
lambdaFormat,
|
|
2116
|
+
lambdaOutdir,
|
|
2117
|
+
lambdaSourceMaps
|
|
2118
|
+
});
|
|
2119
|
+
const { bucket, key, versionId } = await uploadCodeToS3({
|
|
2120
|
+
stackName,
|
|
2121
|
+
lambdaOutdir
|
|
2122
|
+
});
|
|
2123
|
+
if (!lambdaImage) {
|
|
2124
|
+
await deployLambdaLayers({ lambdaExternal });
|
|
2125
|
+
return {
|
|
2126
|
+
bucket,
|
|
2127
|
+
key,
|
|
2128
|
+
versionId
|
|
2129
|
+
};
|
|
2130
|
+
}
|
|
2131
|
+
const { imageUri } = await uploadCodeToECR({
|
|
2132
|
+
bucket,
|
|
2133
|
+
key,
|
|
2134
|
+
versionId,
|
|
2135
|
+
lambdaDockerfile,
|
|
2136
|
+
lambdaExternal
|
|
2137
|
+
});
|
|
2138
|
+
return { imageUri };
|
|
2139
|
+
};
|
|
2140
|
+
//#endregion
|
|
2141
|
+
//#region src/deploy/cicd/getTriggerPipelineObjectKey.ts
|
|
2142
|
+
/**
|
|
2143
|
+
* The file with this key inside the source S3 key of main and tag pipelines
|
|
2144
|
+
* will trigger those pipelines.
|
|
2145
|
+
*/
|
|
2146
|
+
const getTriggerPipelinesObjectKey = ({ prefix, pipeline }) => {
|
|
2147
|
+
return `${prefix}/${pipeline}.zip`;
|
|
2148
|
+
};
|
|
2149
|
+
//#endregion
|
|
2150
|
+
//#region src/deploy/cicd/cicd.template.ts
|
|
2151
|
+
const API_LOGICAL_ID = "ApiV1ServerlessApi";
|
|
2152
|
+
const CODE_BUILD_PROJECT_LOGS_LOGICAL_ID = "RepositoryImageCodeBuildProjectLogsLogGroup";
|
|
2153
|
+
const CODE_BUILD_PROJECT_SERVICE_ROLE_LOGICAL_ID = "RepositoryImageCodeBuildProjectIAMRole";
|
|
2154
|
+
const ECR_REPOSITORY_LOGICAL_ID = "RepositoryECRRepository";
|
|
2155
|
+
const FUNCTION_IAM_ROLE_LOGICAL_ID = "ApiV1ServerlessFunctionIAMRole";
|
|
2156
|
+
const ECS_TASK_REPORT_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID = "EcsTaskReportHandler";
|
|
2157
|
+
const PROCESS_ENV_REPOSITORY_IMAGE_CODE_BUILD_PROJECT_NAME = "REPOSITORY_IMAGE_CODE_BUILD_PROJECT_NAME";
|
|
2158
|
+
const REPOSITORY_ECS_TASK_CONTAINER_NAME = "RepositoryECSTaskContainerName";
|
|
2159
|
+
const REPOSITORY_ECS_TASK_DEFINITION_LOGICAL_ID = "RepositoryECSTaskDefinition";
|
|
2160
|
+
const REPOSITORY_IMAGE_CODE_BUILD_PROJECT_LOGICAL_ID = "RepositoryImageCodeBuildProject";
|
|
2161
|
+
const REPOSITORY_TASKS_ECS_CLUSTER_LOGICAL_ID = "RepositoryTasksECSCluster";
|
|
2162
|
+
const REPOSITORY_TASKS_ECS_CLUSTER_LOGS_LOG_GROUP_LOGICAL_ID = "RepositoryTasksECSClusterLogsLogGroup";
|
|
2163
|
+
const REPOSITORY_TASKS_ECS_TASK_DEFINITION_EXECUTION_ROLE_LOGICAL_ID = "RepositoryTasksECSTaskDefinitionExecutionRoleIAMRole";
|
|
2164
|
+
const REPOSITORY_TASKS_ECS_TASK_DEFINITION_TASK_ROLE_LOGICAL_ID = "RepositoryTasksECSTaskDefinitionTaskRoleIAMRole";
|
|
2165
|
+
const PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID = "PipelinesArtifactStoreS3Bucket";
|
|
2166
|
+
const PIPELINES_ROLE_LOGICAL_ID = "PipelinesMainIAMRole";
|
|
2167
|
+
const PIPELINES_MAIN_LOGICAL_ID = "PipelinesMainCodePipeline";
|
|
2168
|
+
const PIPELINES_TAG_LOGICAL_ID = "PipelinesTagCodePipeline";
|
|
2169
|
+
const PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID = "PipelinesHandlerLambdaFunction";
|
|
2170
|
+
const IMAGE_UPDATER_SCHEDULE_SERVERLESS_FUNCTION_LOGICAL_ID = "ImageUpdaterScheduleServerlessFunction";
|
|
2171
|
+
/**
|
|
2172
|
+
* An [AWS CodeBuild](https://aws.amazon.com/codebuild/) project is created
|
|
2173
|
+
* to build (create and update) repository images. It uses a
|
|
2174
|
+
* [BUILD\_GENERAL1\_SMALL environment compute type](https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html)
|
|
2175
|
+
* with Linux as operational system to build the image.
|
|
2176
|
+
*/
|
|
2177
|
+
const getRepositoryImageBuilder = () => {
|
|
2178
|
+
return {
|
|
2179
|
+
Type: "AWS::CodeBuild::Project",
|
|
2180
|
+
Properties: {
|
|
2181
|
+
Artifacts: { Type: "NO_ARTIFACTS" },
|
|
2182
|
+
Cache: {
|
|
2183
|
+
Location: "LOCAL",
|
|
2184
|
+
Modes: ["LOCAL_DOCKER_LAYER_CACHE"],
|
|
2185
|
+
Type: "LOCAL"
|
|
2186
|
+
},
|
|
2187
|
+
Description: "Create repository image.",
|
|
2188
|
+
Environment: {
|
|
2189
|
+
ComputeType: "BUILD_GENERAL1_SMALL",
|
|
2190
|
+
EnvironmentVariables: [
|
|
2191
|
+
{
|
|
2192
|
+
Name: "AWS_ACCOUNT_ID",
|
|
2193
|
+
Value: { Ref: "AWS::AccountId" }
|
|
2194
|
+
},
|
|
2195
|
+
{
|
|
2196
|
+
Name: "AWS_REGION",
|
|
2197
|
+
Value: { Ref: "AWS::Region" }
|
|
2198
|
+
},
|
|
2199
|
+
{
|
|
2200
|
+
Name: "DOCKERFILE",
|
|
2201
|
+
Value: { "Fn::Sub": [
|
|
2202
|
+
"FROM public.ecr.aws/ubuntu/ubuntu:20.04_stable",
|
|
2203
|
+
"ENV DEBIAN_FRONTEND noninteractive",
|
|
2204
|
+
"RUN apt-get update --fix-missing",
|
|
2205
|
+
"RUN apt-get install -y curl",
|
|
2206
|
+
"RUN apt-get install -y git",
|
|
2207
|
+
"RUN apt-get install -y jq",
|
|
2208
|
+
`RUN curl -fsSL https://deb.nodesource.com/setup_${getNodeVersion({ runtime: DEFAULT_NODE_RUNTIME })}.x | bash -`,
|
|
2209
|
+
"RUN apt-get install -y nodejs",
|
|
2210
|
+
"RUN apt-get clean",
|
|
2211
|
+
"RUN npm install -g yarn",
|
|
2212
|
+
"RUN yarn global add carlin",
|
|
2213
|
+
"RUN git config --global user.name carlin",
|
|
2214
|
+
"RUN git config --global user.email carlin@ttoss.dev",
|
|
2215
|
+
"RUN mkdir /root/.ssh/",
|
|
2216
|
+
"COPY ./id_rsa /root/.ssh/id_rsa",
|
|
2217
|
+
"RUN chmod 600 /root/.ssh/id_rsa",
|
|
2218
|
+
"RUN touch /root/.ssh/known_hosts",
|
|
2219
|
+
"RUN ssh-keyscan github.com >> /root/.ssh/known_hosts",
|
|
2220
|
+
"COPY . /home",
|
|
2221
|
+
"WORKDIR /home/repository",
|
|
2222
|
+
"RUN mkdir -p /home/yarn-cache",
|
|
2223
|
+
"RUN yarn config set cache-folder /home/yarn-cache",
|
|
2224
|
+
"RUN yarn install",
|
|
2225
|
+
"RUN git checkout -- yarn.lock"
|
|
2226
|
+
].join("\n") }
|
|
2227
|
+
},
|
|
2228
|
+
{
|
|
2229
|
+
Name: "IMAGE_TAG",
|
|
2230
|
+
Value: "latest"
|
|
2231
|
+
},
|
|
2232
|
+
{
|
|
2233
|
+
Name: "REPOSITORY_ECR_REPOSITORY",
|
|
2234
|
+
Value: { Ref: ECR_REPOSITORY_LOGICAL_ID }
|
|
2235
|
+
},
|
|
2236
|
+
{
|
|
2237
|
+
Name: "SSH_KEY",
|
|
2238
|
+
Value: { Ref: "SSHKey" }
|
|
2239
|
+
},
|
|
2240
|
+
{
|
|
2241
|
+
Name: "SSH_URL",
|
|
2242
|
+
Value: { Ref: "SSHUrl" }
|
|
2243
|
+
}
|
|
2244
|
+
],
|
|
2245
|
+
Image: "aws/codebuild/standard:3.0",
|
|
2246
|
+
ImagePullCredentialsType: "CODEBUILD",
|
|
2247
|
+
/**
|
|
2248
|
+
* Enables running the Docker daemon inside a Docker container. Set to
|
|
2249
|
+
* true only if the build project is used to build Docker images.
|
|
2250
|
+
* Otherwise, a build that attempts to interact with the Docker daemon
|
|
2251
|
+
* fails. The default setting is false."
|
|
2252
|
+
* https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-environment.html#cfn-codebuild-project-environment-privilegedmode
|
|
2253
|
+
*/
|
|
2254
|
+
PrivilegedMode: true,
|
|
2255
|
+
Type: "LINUX_CONTAINER"
|
|
2256
|
+
},
|
|
2257
|
+
LogsConfig: { CloudWatchLogs: {
|
|
2258
|
+
Status: "ENABLED",
|
|
2259
|
+
GroupName: { Ref: CODE_BUILD_PROJECT_LOGS_LOGICAL_ID }
|
|
2260
|
+
} },
|
|
2261
|
+
ServiceRole: { "Fn::GetAtt": [CODE_BUILD_PROJECT_SERVICE_ROLE_LOGICAL_ID, "Arn"] },
|
|
2262
|
+
Source: {
|
|
2263
|
+
BuildSpec: dump({
|
|
2264
|
+
version: "0.2",
|
|
2265
|
+
phases: {
|
|
2266
|
+
install: { commands: [
|
|
2267
|
+
"echo install started on `date`",
|
|
2268
|
+
`echo "$SSH_KEY" > ~/.ssh/id_rsa`,
|
|
2269
|
+
"chmod 600 ~/.ssh/id_rsa",
|
|
2270
|
+
"rm -rf repository",
|
|
2271
|
+
"git clone $SSH_URL repository",
|
|
2272
|
+
"cd repository",
|
|
2273
|
+
"ls"
|
|
2274
|
+
] },
|
|
2275
|
+
pre_build: { commands: ["echo pre_build started on `date`"] },
|
|
2276
|
+
build: { commands: [
|
|
2277
|
+
"echo build started on `date`",
|
|
1849
2278
|
"$(aws ecr get-login --no-include-email --region $AWS_REGION)",
|
|
1850
2279
|
"echo Building the repository image...",
|
|
1851
2280
|
"cd ../",
|
|
@@ -2206,457 +2635,192 @@ const getCicdTemplate = ({ pipelines = [], cpu = ECS_TASK_DEFAULT_CPU, memory =
|
|
|
2206
2635
|
Value: te.value
|
|
2207
2636
|
};
|
|
2208
2637
|
})
|
|
2209
|
-
],
|
|
2210
|
-
Image: { "Fn::Sub": ["${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${RepositoryECR}:latest", { RepositoryECR: { Ref: ECR_REPOSITORY_LOGICAL_ID } }] },
|
|
2211
|
-
LogConfiguration: {
|
|
2212
|
-
LogDriver: "awslogs",
|
|
2213
|
-
Options: {
|
|
2214
|
-
"awslogs-group": { Ref: REPOSITORY_TASKS_ECS_CLUSTER_LOGS_LOG_GROUP_LOGICAL_ID },
|
|
2215
|
-
"awslogs-region": { Ref: "AWS::Region" },
|
|
2216
|
-
"awslogs-stream-prefix": "ecs"
|
|
2217
|
-
}
|
|
2218
|
-
},
|
|
2219
|
-
Name: REPOSITORY_ECS_TASK_CONTAINER_NAME
|
|
2220
|
-
}],
|
|
2221
|
-
Cpu: cpu,
|
|
2222
|
-
ExecutionRoleArn: { "Fn::GetAtt": [REPOSITORY_TASKS_ECS_TASK_DEFINITION_EXECUTION_ROLE_LOGICAL_ID, "Arn"] },
|
|
2223
|
-
Memory: memory,
|
|
2224
|
-
NetworkMode: "awsvpc",
|
|
2225
|
-
RequiresCompatibilities: ["FARGATE"],
|
|
2226
|
-
TaskRoleArn: { "Fn::GetAtt": [REPOSITORY_TASKS_ECS_TASK_DEFINITION_TASK_ROLE_LOGICAL_ID, "Arn"] }
|
|
2227
|
-
}
|
|
2228
|
-
};
|
|
2229
|
-
})();
|
|
2230
|
-
/**
|
|
2231
|
-
* Pipelines
|
|
2232
|
-
*/
|
|
2233
|
-
if (pipelines.includes("main") || pipelines.includes("tag")) {
|
|
2234
|
-
resources[PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID] = {
|
|
2235
|
-
Type: "AWS::S3::Bucket",
|
|
2236
|
-
Properties: { LifecycleConfiguration: { Rules: [{
|
|
2237
|
-
/**
|
|
2238
|
-
* We won't use the artifacts forever.
|
|
2239
|
-
*/
|
|
2240
|
-
ExpirationInDays: 7,
|
|
2241
|
-
Status: "Enabled"
|
|
2242
|
-
}] } }
|
|
2243
|
-
};
|
|
2244
|
-
resources[PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID] = {
|
|
2245
|
-
Type: "AWS::Lambda::Function",
|
|
2246
|
-
Properties: {
|
|
2247
|
-
Code: {
|
|
2248
|
-
S3Bucket: s3.bucket,
|
|
2249
|
-
S3Key: s3.key,
|
|
2250
|
-
S3ObjectVersion: s3.versionId
|
|
2251
|
-
},
|
|
2252
|
-
Environment: { Variables: { ...executeEcsTaskVariables } },
|
|
2253
|
-
Handler: "index.pipelinesHandler",
|
|
2254
|
-
MemorySize: 128,
|
|
2255
|
-
Role: { "Fn::GetAtt": [FUNCTION_IAM_ROLE_LOGICAL_ID, "Arn"] },
|
|
2256
|
-
Runtime: DEFAULT_NODE_RUNTIME,
|
|
2257
|
-
Timeout: 60
|
|
2258
|
-
}
|
|
2259
|
-
};
|
|
2260
|
-
resources[PIPELINES_ROLE_LOGICAL_ID] = {
|
|
2261
|
-
Type: "AWS::IAM::Role",
|
|
2262
|
-
Properties: {
|
|
2263
|
-
AssumeRolePolicyDocument: {
|
|
2264
|
-
Version: "2012-10-17",
|
|
2265
|
-
Statement: [{
|
|
2266
|
-
Effect: "Allow",
|
|
2267
|
-
Principal: { Service: "codepipeline.amazonaws.com" },
|
|
2268
|
-
Action: "sts:AssumeRole"
|
|
2269
|
-
}]
|
|
2270
|
-
},
|
|
2271
|
-
ManagedPolicyArns: [],
|
|
2272
|
-
Path: getIamPath(),
|
|
2273
|
-
Policies: [{
|
|
2274
|
-
PolicyName: `${PIPELINES_ROLE_LOGICAL_ID}Policy`,
|
|
2275
|
-
PolicyDocument: {
|
|
2276
|
-
Version: "2012-10-17",
|
|
2277
|
-
Statement: [
|
|
2278
|
-
{
|
|
2279
|
-
Effect: "Allow",
|
|
2280
|
-
Action: "lambda:InvokeFunction",
|
|
2281
|
-
Resource: [{ "Fn::GetAtt": [PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID, "Arn"] }]
|
|
2282
|
-
},
|
|
2283
|
-
{
|
|
2284
|
-
Effect: "Allow",
|
|
2285
|
-
Action: "s3:*",
|
|
2286
|
-
Resource: [{ "Fn::GetAtt": [PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID, "Arn"] }, { "Fn::Sub": `arn:aws:s3:::\${${PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID}}/*` }]
|
|
2287
|
-
},
|
|
2288
|
-
{
|
|
2289
|
-
Effect: "Allow",
|
|
2290
|
-
Action: "s3:*",
|
|
2291
|
-
Resource: { "Fn::Sub": [`arn:aws:s3:::\${BucketName}/${triggerPipelinesObjectKeyPrefix}*`, { BucketName: { "Fn::ImportValue": BASE_STACK_BUCKET_NAME_EXPORTED_NAME } }] }
|
|
2292
|
-
},
|
|
2293
|
-
{
|
|
2294
|
-
Effect: "Allow",
|
|
2295
|
-
Action: ["s3:Get*", "s3:List*"],
|
|
2296
|
-
Resource: { "Fn::Sub": [`arn:aws:s3:::\${BucketName}`, { BucketName: { "Fn::ImportValue": BASE_STACK_BUCKET_NAME_EXPORTED_NAME } }] }
|
|
2297
|
-
}
|
|
2298
|
-
]
|
|
2299
|
-
}
|
|
2300
|
-
}]
|
|
2301
|
-
}
|
|
2302
|
-
};
|
|
2303
|
-
const getCodePipelinePipeline = (pipeline) => {
|
|
2304
|
-
const pipelinePascalCase = pascalCase(pipeline);
|
|
2305
|
-
const pipelineS3SourceOutputName = `Pipeline${pipelinePascalCase}S3SourceOutput`;
|
|
2306
|
-
return {
|
|
2307
|
-
Type: "AWS::CodePipeline::Pipeline",
|
|
2308
|
-
Properties: {
|
|
2309
|
-
ArtifactStore: {
|
|
2310
|
-
Location: { Ref: PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID },
|
|
2311
|
-
Type: "S3"
|
|
2312
|
-
},
|
|
2313
|
-
RestartExecutionOnUpdate: false,
|
|
2314
|
-
RoleArn: { "Fn::GetAtt": [PIPELINES_ROLE_LOGICAL_ID, "Arn"] },
|
|
2315
|
-
Stages: [{
|
|
2316
|
-
Actions: [{
|
|
2317
|
-
ActionTypeId: {
|
|
2318
|
-
Category: "Source",
|
|
2319
|
-
Owner: "AWS",
|
|
2320
|
-
Provider: "S3",
|
|
2321
|
-
Version: 1
|
|
2322
|
-
},
|
|
2323
|
-
Configuration: {
|
|
2324
|
-
S3Bucket: { "Fn::ImportValue": BASE_STACK_BUCKET_NAME_EXPORTED_NAME },
|
|
2325
|
-
S3ObjectKey: getTriggerPipelinesObjectKey({
|
|
2326
|
-
prefix: triggerPipelinesObjectKeyPrefix,
|
|
2327
|
-
pipeline
|
|
2328
|
-
})
|
|
2329
|
-
},
|
|
2330
|
-
Name: `Pipeline${pipelinePascalCase}S3SourceAction`,
|
|
2331
|
-
OutputArtifacts: [{ Name: pipelineS3SourceOutputName }]
|
|
2332
|
-
}],
|
|
2333
|
-
Name: `Pipeline${pipelinePascalCase}S3SourceStage`
|
|
2334
|
-
}, {
|
|
2335
|
-
Actions: [{
|
|
2336
|
-
ActionTypeId: {
|
|
2337
|
-
Category: "Invoke",
|
|
2338
|
-
Owner: "AWS",
|
|
2339
|
-
Provider: "Lambda",
|
|
2340
|
-
Version: 1
|
|
2341
|
-
},
|
|
2342
|
-
Configuration: {
|
|
2343
|
-
FunctionName: { Ref: PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID },
|
|
2344
|
-
UserParameters: (() => {
|
|
2345
|
-
return pipeline;
|
|
2346
|
-
})()
|
|
2347
|
-
},
|
|
2348
|
-
InputArtifacts: [{ Name: pipelineS3SourceOutputName }],
|
|
2349
|
-
Name: `Pipeline${pipelinePascalCase}RunECSTasksAction`
|
|
2350
|
-
}, {
|
|
2351
|
-
ActionTypeId: {
|
|
2352
|
-
Category: "Approval",
|
|
2353
|
-
Owner: "AWS",
|
|
2354
|
-
Provider: "Manual",
|
|
2355
|
-
Version: 1
|
|
2356
|
-
},
|
|
2357
|
-
Name: PIPELINE_ECS_TASK_EXECUTION_MANUAL_APPROVAL_ACTION_NAME
|
|
2358
|
-
}],
|
|
2359
|
-
Name: PIPELINE_ECS_TASK_EXECUTION_STAGE_NAME
|
|
2360
|
-
}]
|
|
2361
|
-
}
|
|
2362
|
-
};
|
|
2363
|
-
};
|
|
2364
|
-
if (pipelines.includes("main")) resources[PIPELINES_MAIN_LOGICAL_ID] = getCodePipelinePipeline("main");
|
|
2365
|
-
if (pipelines.includes("tag")) resources[PIPELINES_TAG_LOGICAL_ID] = getCodePipelinePipeline("tag");
|
|
2366
|
-
}
|
|
2367
|
-
return {
|
|
2368
|
-
AWSTemplateFormatVersion: "2010-09-09",
|
|
2369
|
-
Transform: "AWS::Serverless-2016-10-31",
|
|
2370
|
-
Resources: resources,
|
|
2371
|
-
Parameters: {
|
|
2372
|
-
SSHKey: {
|
|
2373
|
-
NoEcho: true,
|
|
2374
|
-
Type: "String"
|
|
2375
|
-
},
|
|
2376
|
-
SSHUrl: { Type: "String" }
|
|
2377
|
-
},
|
|
2378
|
-
Outputs: {
|
|
2379
|
-
[REPOSITORY_IMAGE_CODE_BUILD_PROJECT_LOGICAL_ID]: { Value: { Ref: REPOSITORY_IMAGE_CODE_BUILD_PROJECT_LOGICAL_ID } },
|
|
2380
|
-
ApiV1Endpoint: {
|
|
2381
|
-
Description: "CICD API v1 stage endpoint.",
|
|
2382
|
-
Value: { "Fn::Sub": `https://\${${API_LOGICAL_ID}}.execute-api.\${AWS::Region}.amazonaws.com/v1/` }
|
|
2638
|
+
],
|
|
2639
|
+
Image: { "Fn::Sub": ["${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${RepositoryECR}:latest", { RepositoryECR: { Ref: ECR_REPOSITORY_LOGICAL_ID } }] },
|
|
2640
|
+
LogConfiguration: {
|
|
2641
|
+
LogDriver: "awslogs",
|
|
2642
|
+
Options: {
|
|
2643
|
+
"awslogs-group": { Ref: REPOSITORY_TASKS_ECS_CLUSTER_LOGS_LOG_GROUP_LOGICAL_ID },
|
|
2644
|
+
"awslogs-region": { Ref: "AWS::Region" },
|
|
2645
|
+
"awslogs-stream-prefix": "ecs"
|
|
2646
|
+
}
|
|
2647
|
+
},
|
|
2648
|
+
Name: REPOSITORY_ECS_TASK_CONTAINER_NAME
|
|
2649
|
+
}],
|
|
2650
|
+
Cpu: cpu,
|
|
2651
|
+
ExecutionRoleArn: { "Fn::GetAtt": [REPOSITORY_TASKS_ECS_TASK_DEFINITION_EXECUTION_ROLE_LOGICAL_ID, "Arn"] },
|
|
2652
|
+
Memory: memory,
|
|
2653
|
+
NetworkMode: "awsvpc",
|
|
2654
|
+
RequiresCompatibilities: ["FARGATE"],
|
|
2655
|
+
TaskRoleArn: { "Fn::GetAtt": [REPOSITORY_TASKS_ECS_TASK_DEFINITION_TASK_ROLE_LOGICAL_ID, "Arn"] }
|
|
2383
2656
|
}
|
|
2384
|
-
}
|
|
2385
|
-
};
|
|
2386
|
-
};
|
|
2387
|
-
//#endregion
|
|
2388
|
-
//#region src/deploy/lambda/buildLambdaCode.ts
|
|
2389
|
-
const logPrefix$15 = "lambda";
|
|
2390
|
-
/**
|
|
2391
|
-
* Carlin builds the Lambda code using esbuild. It can build the code as ESM or
|
|
2392
|
-
* CJS format. When building as ESM, it will split the code into multiple files,
|
|
2393
|
-
* which reduces the payload size when deploying the Lambda function. The file
|
|
2394
|
-
* extension of the output files will be `.mjs` for ESM and `.cjs` for CJS.
|
|
2395
|
-
*/
|
|
2396
|
-
const buildLambdaCode = async ({ lambdaEntryPoints, lambdaEntryPointsBaseDir = ".", lambdaExternal = [], lambdaFormat = "esm", lambdaOutdir, lambdaSourceMaps = false }) => {
|
|
2397
|
-
log.info(logPrefix$15, "Building Lambda single file...");
|
|
2657
|
+
};
|
|
2658
|
+
})();
|
|
2398
2659
|
/**
|
|
2399
|
-
*
|
|
2400
|
-
* new ones.
|
|
2660
|
+
* Pipelines
|
|
2401
2661
|
*/
|
|
2402
|
-
if (
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
...builtinModules,
|
|
2413
|
-
...lambdaExternal
|
|
2414
|
-
],
|
|
2415
|
-
/**
|
|
2416
|
-
* Some packages as `graphql` are not compatible with ESM yet.
|
|
2417
|
-
* https://github.com/graphql/graphql-js/issues/3603
|
|
2418
|
-
*/
|
|
2419
|
-
format: lambdaFormat,
|
|
2420
|
-
/**
|
|
2421
|
-
* https://esbuild.github.io/api/#minify
|
|
2422
|
-
*/
|
|
2423
|
-
minifySyntax: true,
|
|
2424
|
-
platform: "node",
|
|
2425
|
-
/**
|
|
2426
|
-
* Node applies the map only with `--enable-source-maps`, which
|
|
2427
|
-
* `addSourceMapsNodeOptions` sets on every function. Without
|
|
2428
|
-
* `sourcesContent`: Node maps a frame from names and lines alone, and the
|
|
2429
|
-
* source text would only grow the zip.
|
|
2430
|
-
*/
|
|
2431
|
-
...lambdaSourceMaps ? {
|
|
2432
|
-
sourcemap: "linked",
|
|
2433
|
-
sourcesContent: false
|
|
2434
|
-
} : {},
|
|
2435
|
-
splitting: lambdaFormat === "esm",
|
|
2436
|
-
outbase: path.join(process.cwd(), lambdaEntryPointsBaseDir),
|
|
2437
|
-
outdir: path.join(process.cwd(), lambdaOutdir),
|
|
2438
|
-
outExtension: { ".js": lambdaFormat === "esm" ? ".mjs" : ".cjs" },
|
|
2439
|
-
target: typescriptConfig.target,
|
|
2440
|
-
treeShaking: true
|
|
2441
|
-
});
|
|
2442
|
-
if (errors.length > 0) throw errors;
|
|
2443
|
-
};
|
|
2444
|
-
//#endregion
|
|
2445
|
-
//#region src/deploy/lambdaLayer/getPackageLambdaLayerStackName.ts
|
|
2446
|
-
const lambdaLayerStackNamePrefix = `LambdaLayer`;
|
|
2447
|
-
const getPackageLambdaLayerStackName = (packageName) => {
|
|
2448
|
-
const [scopedName, version] = packageName.split("@").filter((part) => {
|
|
2449
|
-
return !!part;
|
|
2450
|
-
});
|
|
2451
|
-
return [
|
|
2452
|
-
lambdaLayerStackNamePrefix,
|
|
2453
|
-
pascalCase(scopedName),
|
|
2454
|
-
version.replace(/[^0-9.]/g, "").replace(/\./g, "-")
|
|
2455
|
-
].join("-");
|
|
2456
|
-
};
|
|
2457
|
-
//#endregion
|
|
2458
|
-
//#region src/deploy/lambdaLayer/deployLambdaLayer.ts
|
|
2459
|
-
const logPrefix$14 = "lambda-layer";
|
|
2460
|
-
const createLambdaLayerZipFile = async ({ codeBuildProjectName, packageName }) => {
|
|
2461
|
-
log.info(logPrefix$14, `Creating zip file for package ${packageName}...`);
|
|
2462
|
-
const { build } = await new AWS.CodeBuild().startBuild({
|
|
2463
|
-
environmentVariablesOverride: [{
|
|
2464
|
-
name: "PACKAGE_NAME",
|
|
2465
|
-
value: packageName
|
|
2466
|
-
}],
|
|
2467
|
-
projectName: codeBuildProjectName
|
|
2468
|
-
}).promise();
|
|
2469
|
-
if (!build?.id) throw new Error("Cannot start build.");
|
|
2470
|
-
const result = await waitCodeBuildFinish({
|
|
2471
|
-
buildId: build.id,
|
|
2472
|
-
name: packageName
|
|
2473
|
-
});
|
|
2474
|
-
if (result.artifacts?.location) {
|
|
2475
|
-
const location = result.artifacts.location.split("/");
|
|
2476
|
-
const bucket = location.shift()?.replace("arn:aws:s3:::", "");
|
|
2477
|
-
if (!bucket) throw new Error("Cannot retrieve bucket name.");
|
|
2478
|
-
return {
|
|
2479
|
-
bucket,
|
|
2480
|
-
key: location.join("/")
|
|
2662
|
+
if (pipelines.includes("main") || pipelines.includes("tag")) {
|
|
2663
|
+
resources[PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID] = {
|
|
2664
|
+
Type: "AWS::S3::Bucket",
|
|
2665
|
+
Properties: { LifecycleConfiguration: { Rules: [{
|
|
2666
|
+
/**
|
|
2667
|
+
* We won't use the artifacts forever.
|
|
2668
|
+
*/
|
|
2669
|
+
ExpirationInDays: 7,
|
|
2670
|
+
Status: "Enabled"
|
|
2671
|
+
}] } }
|
|
2481
2672
|
};
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
};
|
|
2485
|
-
/**
|
|
2486
|
-
* The CloudFormation template created to deploy a Lambda Layer.
|
|
2487
|
-
*
|
|
2488
|
-
* - The Layer name is the same as the Stack name.
|
|
2489
|
-
*/
|
|
2490
|
-
const getLambdaLayerTemplate = ({ bucket, key, packageName, runtime }) => {
|
|
2491
|
-
const description = packageName.substring(0, 256);
|
|
2492
|
-
return {
|
|
2493
|
-
AWSTemplateFormatVersion: "2010-09-09",
|
|
2494
|
-
Resources: { LambdaLayer: {
|
|
2495
|
-
Type: "AWS::Lambda::LayerVersion",
|
|
2673
|
+
resources[PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID] = {
|
|
2674
|
+
Type: "AWS::Lambda::Function",
|
|
2496
2675
|
Properties: {
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2676
|
+
Code: {
|
|
2677
|
+
S3Bucket: s3.bucket,
|
|
2678
|
+
S3Key: s3.key,
|
|
2679
|
+
S3ObjectVersion: s3.versionId
|
|
2501
2680
|
},
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
Value: { Ref: "LambdaLayer" },
|
|
2509
|
-
Export: { Name: { Ref: "AWS::StackName" } }
|
|
2510
|
-
} }
|
|
2511
|
-
};
|
|
2512
|
-
};
|
|
2513
|
-
const getPackagesThatAreNotDeployed = async ({ packages }) => {
|
|
2514
|
-
return (await Promise.all(packages.map(async (packageName) => {
|
|
2515
|
-
return await doesStackExist({ stackName: getPackageLambdaLayerStackName(packageName) }) ? "" : packageName;
|
|
2516
|
-
}))).filter((packageName) => {
|
|
2517
|
-
return !!packageName;
|
|
2518
|
-
});
|
|
2519
|
-
};
|
|
2520
|
-
const deployLambdaLayer = async ({ packages, deployIfExists = true, runtime }) => {
|
|
2521
|
-
try {
|
|
2522
|
-
const packagesToBeDeployed = deployIfExists ? packages : await getPackagesThatAreNotDeployed({ packages });
|
|
2523
|
-
if (packagesToBeDeployed.length === 0) return;
|
|
2524
|
-
const codeBuildProjectName = await getBaseStackResource("BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME");
|
|
2525
|
-
if (!codeBuildProjectName) throw new Error("Cannot deploy lambda-layer because AWS CodeBuild project doesn't exist.");
|
|
2526
|
-
const deployLambdaLayerSinglePackage = async (packageName) => {
|
|
2527
|
-
try {
|
|
2528
|
-
const { bucket, key } = await createLambdaLayerZipFile({
|
|
2529
|
-
codeBuildProjectName,
|
|
2530
|
-
packageName
|
|
2531
|
-
});
|
|
2532
|
-
await deploy({
|
|
2533
|
-
template: getLambdaLayerTemplate({
|
|
2534
|
-
packageName,
|
|
2535
|
-
bucket,
|
|
2536
|
-
key,
|
|
2537
|
-
runtime
|
|
2538
|
-
}),
|
|
2539
|
-
terminationProtection: true,
|
|
2540
|
-
params: { StackName: getPackageLambdaLayerStackName(packageName) }
|
|
2541
|
-
});
|
|
2542
|
-
} catch (error) {
|
|
2543
|
-
handleDeployError({
|
|
2544
|
-
error,
|
|
2545
|
-
logPrefix: logPrefix$14
|
|
2546
|
-
});
|
|
2681
|
+
Environment: { Variables: { ...executeEcsTaskVariables } },
|
|
2682
|
+
Handler: "index.pipelinesHandler",
|
|
2683
|
+
MemorySize: 128,
|
|
2684
|
+
Role: { "Fn::GetAtt": [FUNCTION_IAM_ROLE_LOGICAL_ID, "Arn"] },
|
|
2685
|
+
Runtime: DEFAULT_NODE_RUNTIME,
|
|
2686
|
+
Timeout: 60
|
|
2547
2687
|
}
|
|
2548
2688
|
};
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
}
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2689
|
+
resources[PIPELINES_ROLE_LOGICAL_ID] = {
|
|
2690
|
+
Type: "AWS::IAM::Role",
|
|
2691
|
+
Properties: {
|
|
2692
|
+
AssumeRolePolicyDocument: {
|
|
2693
|
+
Version: "2012-10-17",
|
|
2694
|
+
Statement: [{
|
|
2695
|
+
Effect: "Allow",
|
|
2696
|
+
Principal: { Service: "codepipeline.amazonaws.com" },
|
|
2697
|
+
Action: "sts:AssumeRole"
|
|
2698
|
+
}]
|
|
2699
|
+
},
|
|
2700
|
+
ManagedPolicyArns: [],
|
|
2701
|
+
Path: getIamPath(),
|
|
2702
|
+
Policies: [{
|
|
2703
|
+
PolicyName: `${PIPELINES_ROLE_LOGICAL_ID}Policy`,
|
|
2704
|
+
PolicyDocument: {
|
|
2705
|
+
Version: "2012-10-17",
|
|
2706
|
+
Statement: [
|
|
2707
|
+
{
|
|
2708
|
+
Effect: "Allow",
|
|
2709
|
+
Action: "lambda:InvokeFunction",
|
|
2710
|
+
Resource: [{ "Fn::GetAtt": [PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID, "Arn"] }]
|
|
2711
|
+
},
|
|
2712
|
+
{
|
|
2713
|
+
Effect: "Allow",
|
|
2714
|
+
Action: "s3:*",
|
|
2715
|
+
Resource: [{ "Fn::GetAtt": [PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID, "Arn"] }, { "Fn::Sub": `arn:aws:s3:::\${${PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID}}/*` }]
|
|
2716
|
+
},
|
|
2717
|
+
{
|
|
2718
|
+
Effect: "Allow",
|
|
2719
|
+
Action: "s3:*",
|
|
2720
|
+
Resource: { "Fn::Sub": [`arn:aws:s3:::\${BucketName}/${triggerPipelinesObjectKeyPrefix}*`, { BucketName: { "Fn::ImportValue": BASE_STACK_BUCKET_NAME_EXPORTED_NAME } }] }
|
|
2721
|
+
},
|
|
2722
|
+
{
|
|
2723
|
+
Effect: "Allow",
|
|
2724
|
+
Action: ["s3:Get*", "s3:List*"],
|
|
2725
|
+
Resource: { "Fn::Sub": [`arn:aws:s3:::\${BucketName}`, { BucketName: { "Fn::ImportValue": BASE_STACK_BUCKET_NAME_EXPORTED_NAME } }] }
|
|
2726
|
+
}
|
|
2727
|
+
]
|
|
2728
|
+
}
|
|
2729
|
+
}]
|
|
2579
2730
|
}
|
|
2580
|
-
}),
|
|
2581
|
-
deployIfExists: false
|
|
2582
|
-
});
|
|
2583
|
-
};
|
|
2584
|
-
//#endregion
|
|
2585
|
-
//#region src/deploy/lambda/uploadCodeToECR.ts
|
|
2586
|
-
new AWS.CodeBuild({ region: AWS_DEFAULT_REGION });
|
|
2587
|
-
const uploadCodeToECR = async ({ bucket, key, lambdaExternal, lambdaDockerfile }) => {
|
|
2588
|
-
throw new Error("uploadCodeToECR not finished yet.");
|
|
2589
|
-
};
|
|
2590
|
-
//#endregion
|
|
2591
|
-
//#region src/deploy/lambda/uploadCodeToS3.ts
|
|
2592
|
-
const logPrefix$12 = "lambda";
|
|
2593
|
-
const zipFileName = "lambda.zip";
|
|
2594
|
-
const uploadCodeToS3 = async ({ stackName, lambdaOutdir }) => {
|
|
2595
|
-
log.info(logPrefix$12, `Uploading code to S3...`);
|
|
2596
|
-
const zip = new AdmZip();
|
|
2597
|
-
const zipFile = `${lambdaOutdir}/${zipFileName}`;
|
|
2598
|
-
/**
|
|
2599
|
-
* Check if the zip file already exists and delete it before creating a new.
|
|
2600
|
-
*/
|
|
2601
|
-
if (fs.existsSync(zipFile)) await fs.promises.rm(zipFile);
|
|
2602
|
-
/**
|
|
2603
|
-
* Zip entire directory.
|
|
2604
|
-
*/
|
|
2605
|
-
zip.addLocalFolder(lambdaOutdir);
|
|
2606
|
-
zip.writeZip(`${lambdaOutdir}/${zipFileName}`);
|
|
2607
|
-
return uploadFileToS3({
|
|
2608
|
-
bucket: await getBaseStackResource("BASE_STACK_BUCKET_LOGICAL_NAME"),
|
|
2609
|
-
contentType: "application/zip",
|
|
2610
|
-
key: `lambdas/${stackName}/${zipFileName}`,
|
|
2611
|
-
file: zip.toBuffer()
|
|
2612
|
-
});
|
|
2613
|
-
};
|
|
2614
|
-
//#endregion
|
|
2615
|
-
//#region src/deploy/lambda/deployLambdaCode.ts
|
|
2616
|
-
const logPrefix$11 = "lambda";
|
|
2617
|
-
const deployLambdaCode = async ({ lambdaDockerfile, lambdaExternal = [], lambdaImage, lambdaEntryPoints, lambdaEntryPointsBaseDir = "src", lambdaFormat, lambdaOutdir = "out", lambdaSourceMaps, stackName }) => {
|
|
2618
|
-
if (!lambdaEntryPoints.length) return {};
|
|
2619
|
-
log.info(logPrefix$11, "Deploying Lambda code...");
|
|
2620
|
-
for (const entryPoint of lambdaEntryPoints) {
|
|
2621
|
-
const entryPointPath = path.resolve(lambdaEntryPointsBaseDir, entryPoint);
|
|
2622
|
-
if (!fs.existsSync(entryPointPath)) throw new Error(`Entry point ${entryPointPath} does not exist.`);
|
|
2623
|
-
}
|
|
2624
|
-
await buildLambdaCode({
|
|
2625
|
-
lambdaExternal,
|
|
2626
|
-
lambdaEntryPoints,
|
|
2627
|
-
lambdaEntryPointsBaseDir,
|
|
2628
|
-
lambdaFormat,
|
|
2629
|
-
lambdaOutdir,
|
|
2630
|
-
lambdaSourceMaps
|
|
2631
|
-
});
|
|
2632
|
-
const { bucket, key, versionId } = await uploadCodeToS3({
|
|
2633
|
-
stackName,
|
|
2634
|
-
lambdaOutdir
|
|
2635
|
-
});
|
|
2636
|
-
if (!lambdaImage) {
|
|
2637
|
-
await deployLambdaLayers({ lambdaExternal });
|
|
2638
|
-
return {
|
|
2639
|
-
bucket,
|
|
2640
|
-
key,
|
|
2641
|
-
versionId
|
|
2642
2731
|
};
|
|
2732
|
+
const getCodePipelinePipeline = (pipeline) => {
|
|
2733
|
+
const pipelinePascalCase = pascalCase(pipeline);
|
|
2734
|
+
const pipelineS3SourceOutputName = `Pipeline${pipelinePascalCase}S3SourceOutput`;
|
|
2735
|
+
return {
|
|
2736
|
+
Type: "AWS::CodePipeline::Pipeline",
|
|
2737
|
+
Properties: {
|
|
2738
|
+
ArtifactStore: {
|
|
2739
|
+
Location: { Ref: PIPELINES_ARTIFACT_STORE_S3_BUCKET_LOGICAL_ID },
|
|
2740
|
+
Type: "S3"
|
|
2741
|
+
},
|
|
2742
|
+
RestartExecutionOnUpdate: false,
|
|
2743
|
+
RoleArn: { "Fn::GetAtt": [PIPELINES_ROLE_LOGICAL_ID, "Arn"] },
|
|
2744
|
+
Stages: [{
|
|
2745
|
+
Actions: [{
|
|
2746
|
+
ActionTypeId: {
|
|
2747
|
+
Category: "Source",
|
|
2748
|
+
Owner: "AWS",
|
|
2749
|
+
Provider: "S3",
|
|
2750
|
+
Version: 1
|
|
2751
|
+
},
|
|
2752
|
+
Configuration: {
|
|
2753
|
+
S3Bucket: { "Fn::ImportValue": BASE_STACK_BUCKET_NAME_EXPORTED_NAME },
|
|
2754
|
+
S3ObjectKey: getTriggerPipelinesObjectKey({
|
|
2755
|
+
prefix: triggerPipelinesObjectKeyPrefix,
|
|
2756
|
+
pipeline
|
|
2757
|
+
})
|
|
2758
|
+
},
|
|
2759
|
+
Name: `Pipeline${pipelinePascalCase}S3SourceAction`,
|
|
2760
|
+
OutputArtifacts: [{ Name: pipelineS3SourceOutputName }]
|
|
2761
|
+
}],
|
|
2762
|
+
Name: `Pipeline${pipelinePascalCase}S3SourceStage`
|
|
2763
|
+
}, {
|
|
2764
|
+
Actions: [{
|
|
2765
|
+
ActionTypeId: {
|
|
2766
|
+
Category: "Invoke",
|
|
2767
|
+
Owner: "AWS",
|
|
2768
|
+
Provider: "Lambda",
|
|
2769
|
+
Version: 1
|
|
2770
|
+
},
|
|
2771
|
+
Configuration: {
|
|
2772
|
+
FunctionName: { Ref: PIPELINES_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID },
|
|
2773
|
+
UserParameters: (() => {
|
|
2774
|
+
return pipeline;
|
|
2775
|
+
})()
|
|
2776
|
+
},
|
|
2777
|
+
InputArtifacts: [{ Name: pipelineS3SourceOutputName }],
|
|
2778
|
+
Name: `Pipeline${pipelinePascalCase}RunECSTasksAction`
|
|
2779
|
+
}, {
|
|
2780
|
+
ActionTypeId: {
|
|
2781
|
+
Category: "Approval",
|
|
2782
|
+
Owner: "AWS",
|
|
2783
|
+
Provider: "Manual",
|
|
2784
|
+
Version: 1
|
|
2785
|
+
},
|
|
2786
|
+
Name: PIPELINE_ECS_TASK_EXECUTION_MANUAL_APPROVAL_ACTION_NAME
|
|
2787
|
+
}],
|
|
2788
|
+
Name: PIPELINE_ECS_TASK_EXECUTION_STAGE_NAME
|
|
2789
|
+
}]
|
|
2790
|
+
}
|
|
2791
|
+
};
|
|
2792
|
+
};
|
|
2793
|
+
if (pipelines.includes("main")) resources[PIPELINES_MAIN_LOGICAL_ID] = getCodePipelinePipeline("main");
|
|
2794
|
+
if (pipelines.includes("tag")) resources[PIPELINES_TAG_LOGICAL_ID] = getCodePipelinePipeline("tag");
|
|
2643
2795
|
}
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2796
|
+
return {
|
|
2797
|
+
AWSTemplateFormatVersion: "2010-09-09",
|
|
2798
|
+
Transform: "AWS::Serverless-2016-10-31",
|
|
2799
|
+
Resources: resources,
|
|
2800
|
+
Parameters: {
|
|
2801
|
+
SSHKey: {
|
|
2802
|
+
NoEcho: true,
|
|
2803
|
+
Type: "String"
|
|
2804
|
+
},
|
|
2805
|
+
SSHUrl: { Type: "String" }
|
|
2806
|
+
},
|
|
2807
|
+
Outputs: {
|
|
2808
|
+
[REPOSITORY_IMAGE_CODE_BUILD_PROJECT_LOGICAL_ID]: { Value: { Ref: REPOSITORY_IMAGE_CODE_BUILD_PROJECT_LOGICAL_ID } },
|
|
2809
|
+
ApiV1Endpoint: {
|
|
2810
|
+
Description: "CICD API v1 stage endpoint.",
|
|
2811
|
+
Value: { "Fn::Sub": `https://\${${API_LOGICAL_ID}}.execute-api.\${AWS::Region}.amazonaws.com/v1/` }
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2814
|
+
};
|
|
2652
2815
|
};
|
|
2653
2816
|
//#endregion
|
|
2654
2817
|
//#region src/deploy/cicd/getCicdStackName.ts
|
|
2655
2818
|
const getCicdStackName = () => {
|
|
2819
|
+
const project = getProjectName();
|
|
2656
2820
|
return pascalCase([
|
|
2657
2821
|
NAME,
|
|
2658
2822
|
"Cicd",
|
|
2659
|
-
|
|
2823
|
+
project
|
|
2660
2824
|
].join(" "));
|
|
2661
2825
|
};
|
|
2662
2826
|
//#endregion
|
|
@@ -2666,19 +2830,20 @@ const getLambdaInput = (extension) => {
|
|
|
2666
2830
|
return path$1.resolve(__dirname, `lambdas/index.${extension}`);
|
|
2667
2831
|
};
|
|
2668
2832
|
const deployCicdLambdas = async ({ stackName }) => {
|
|
2833
|
+
const lambdaInput = (() => {
|
|
2834
|
+
/**
|
|
2835
|
+
* This case happens when carlin command is executed when the package is
|
|
2836
|
+
* built.
|
|
2837
|
+
*/
|
|
2838
|
+
if (fs$1.existsSync(getLambdaInput("js"))) return getLambdaInput("js");
|
|
2839
|
+
/**
|
|
2840
|
+
* The package isn't built.
|
|
2841
|
+
*/
|
|
2842
|
+
if (fs$1.existsSync(getLambdaInput("ts"))) return getLambdaInput("ts");
|
|
2843
|
+
throw new Error("Cannot read CICD lambdas file.");
|
|
2844
|
+
})();
|
|
2669
2845
|
const s3 = await deployLambdaCode({
|
|
2670
|
-
lambdaEntryPoints: [
|
|
2671
|
-
/**
|
|
2672
|
-
* This case happens when carlin command is executed when the package is
|
|
2673
|
-
* built.
|
|
2674
|
-
*/
|
|
2675
|
-
if (fs$2.existsSync(getLambdaInput("js"))) return getLambdaInput("js");
|
|
2676
|
-
/**
|
|
2677
|
-
* The package isn't built.
|
|
2678
|
-
*/
|
|
2679
|
-
if (fs$2.existsSync(getLambdaInput("ts"))) return getLambdaInput("ts");
|
|
2680
|
-
throw new Error("Cannot read CICD lambdas file.");
|
|
2681
|
-
})()],
|
|
2846
|
+
lambdaEntryPoints: [lambdaInput],
|
|
2682
2847
|
lambdaExternal: [],
|
|
2683
2848
|
/**
|
|
2684
2849
|
* Needs stackName to define the S3 key.
|
|
@@ -2742,7 +2907,7 @@ const deployCicd = async ({ cpu, memory, pipelines, updateRepository, slackWebho
|
|
|
2742
2907
|
* Created to allow mocking.
|
|
2743
2908
|
*/
|
|
2744
2909
|
const readSSHKey = (dir) => {
|
|
2745
|
-
return fs$
|
|
2910
|
+
return fs$1.readFileSync(dir, "utf-8");
|
|
2746
2911
|
};
|
|
2747
2912
|
//#endregion
|
|
2748
2913
|
//#region src/deploy/cicd/command.ts
|
|
@@ -2864,17 +3029,40 @@ const cloudFormationTypes = [
|
|
|
2864
3029
|
}
|
|
2865
3030
|
}
|
|
2866
3031
|
];
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
3032
|
+
/**
|
|
3033
|
+
* The tags are load-only: dumping writes the intrinsic functions as plain
|
|
3034
|
+
* mappings (`Ref: SomeRef`), which CloudFormation also accepts.
|
|
3035
|
+
*/
|
|
3036
|
+
const getYamlTag = ({ tag, options }) => {
|
|
3037
|
+
if (options.kind === "scalar") return defineScalarTag(tag, {
|
|
3038
|
+
resolve: (source) => {
|
|
3039
|
+
return options.construct(source);
|
|
3040
|
+
},
|
|
3041
|
+
identify: () => {
|
|
3042
|
+
return false;
|
|
3043
|
+
}
|
|
3044
|
+
});
|
|
3045
|
+
return defineSequenceTag(tag, {
|
|
3046
|
+
create: () => {
|
|
3047
|
+
return [];
|
|
3048
|
+
},
|
|
3049
|
+
addItem: (carrier, item) => {
|
|
3050
|
+
carrier.push(item);
|
|
3051
|
+
},
|
|
3052
|
+
finalize: (carrier) => {
|
|
3053
|
+
return options.construct(carrier);
|
|
3054
|
+
},
|
|
3055
|
+
identify: () => {
|
|
3056
|
+
return false;
|
|
3057
|
+
}
|
|
2870
3058
|
});
|
|
2871
3059
|
};
|
|
2872
3060
|
/**
|
|
2873
3061
|
* Transform CloudFormation directives in objects. For example, transform
|
|
2874
|
-
* !Ref Something in { Ref: Something }.
|
|
3062
|
+
* !Ref Something in { Ref: Something }. Merge keys (`<<`) are supported.
|
|
2875
3063
|
*/
|
|
2876
3064
|
const getSchema = (tagAndTypeArr = []) => {
|
|
2877
|
-
return
|
|
3065
|
+
return CORE_SCHEMA.withTags(mergeTag, [...tagAndTypeArr, ...cloudFormationTypes].map(getYamlTag));
|
|
2878
3066
|
};
|
|
2879
3067
|
/**
|
|
2880
3068
|
* Transform YAML string in JSON object.
|
|
@@ -2884,7 +3072,7 @@ const getSchema = (tagAndTypeArr = []) => {
|
|
|
2884
3072
|
* @returns JSON template.
|
|
2885
3073
|
*/
|
|
2886
3074
|
const loadCloudFormationTemplate = (template, tagAndTypeArr = []) => {
|
|
2887
|
-
return
|
|
3075
|
+
return load(template, { schema: getSchema(tagAndTypeArr) });
|
|
2888
3076
|
};
|
|
2889
3077
|
//#endregion
|
|
2890
3078
|
//#region ../cloudformation/src/readCloudFormationYamlTemplate.ts
|
|
@@ -2894,7 +3082,7 @@ const getTypes = () => {
|
|
|
2894
3082
|
options: {
|
|
2895
3083
|
kind: "scalar",
|
|
2896
3084
|
construct: (filePath) => {
|
|
2897
|
-
return fs$
|
|
3085
|
+
return fs$1.readFileSync(path$1.resolve(process.cwd(), filePath)).toString();
|
|
2898
3086
|
}
|
|
2899
3087
|
}
|
|
2900
3088
|
}];
|
|
@@ -2904,7 +3092,8 @@ const getTypes = () => {
|
|
|
2904
3092
|
* @param param0
|
|
2905
3093
|
*/
|
|
2906
3094
|
const readCloudFormationYamlTemplate = ({ templatePath }) => {
|
|
2907
|
-
const
|
|
3095
|
+
const template = fs$1.readFileSync(templatePath).toString();
|
|
3096
|
+
const parsed = loadCloudFormationTemplate(template, getTypes());
|
|
2908
3097
|
if (!parsed || typeof parsed === "string") throw new Error("Cannot parse CloudFormation template.");
|
|
2909
3098
|
return parsed;
|
|
2910
3099
|
};
|
|
@@ -2922,7 +3111,7 @@ const defaultTemplatePaths$1 = [
|
|
|
2922
3111
|
const findAndReadCloudFormationTemplate = async ({ templatePath: defaultTemplatePath, options = {} }) => {
|
|
2923
3112
|
const templatePath = defaultTemplatePath || defaultTemplatePaths$1.reduce((acc, cur) => {
|
|
2924
3113
|
if (acc) return acc;
|
|
2925
|
-
return fs$
|
|
3114
|
+
return fs$1.existsSync(path$1.resolve(process.cwd(), cur)) ? cur : acc;
|
|
2926
3115
|
}, "");
|
|
2927
3116
|
if (!templatePath) throw new Error("Cannot find a CloudFormation template.");
|
|
2928
3117
|
const extension = templatePath?.split(".").pop();
|
|
@@ -2932,8 +3121,9 @@ const findAndReadCloudFormationTemplate = async ({ templatePath: defaultTemplate
|
|
|
2932
3121
|
* "Error message: "unknown tag !<!Ref> at line 21, column 34:\n"
|
|
2933
3122
|
*/
|
|
2934
3123
|
if (["yaml", "yml"].includes(extension)) return readCloudFormationYamlTemplate({ templatePath });
|
|
3124
|
+
const configFilePath = path$1.resolve(process.cwd(), templatePath);
|
|
2935
3125
|
return readConfigFile({
|
|
2936
|
-
configFilePath
|
|
3126
|
+
configFilePath,
|
|
2937
3127
|
options
|
|
2938
3128
|
});
|
|
2939
3129
|
};
|
|
@@ -2974,8 +3164,6 @@ const getLambdaEntryPointsFromTemplate = (template) => {
|
|
|
2974
3164
|
//#endregion
|
|
2975
3165
|
//#region src/deploy/cloudformation.ts
|
|
2976
3166
|
const logPrefix$8 = "cloudformation";
|
|
2977
|
-
log.addLevel("event", 1e4, { fg: "yellow" });
|
|
2978
|
-
log.addLevel("output", 1e4, { fg: "blue" });
|
|
2979
3167
|
[
|
|
2980
3168
|
"ts",
|
|
2981
3169
|
"js",
|
|
@@ -3165,47 +3353,6 @@ const deployCloudFormation = async (cliOptions) => {
|
|
|
3165
3353
|
});
|
|
3166
3354
|
}
|
|
3167
3355
|
};
|
|
3168
|
-
const emptyStackBuckets = async ({ stackName }) => {
|
|
3169
|
-
const buckets = [];
|
|
3170
|
-
await (async ({ nextToken }) => {
|
|
3171
|
-
const { StackResourceSummaries } = await cloudFormationV2().listStackResources({
|
|
3172
|
-
StackName: stackName,
|
|
3173
|
-
NextToken: nextToken
|
|
3174
|
-
}).promise();
|
|
3175
|
-
for (const { ResourceType, PhysicalResourceId } of StackResourceSummaries || []) if (ResourceType === "AWS::S3::Bucket" && PhysicalResourceId) buckets.push(PhysicalResourceId);
|
|
3176
|
-
})({});
|
|
3177
|
-
return Promise.all(buckets.map((bucket) => {
|
|
3178
|
-
return emptyS3Directory({ bucket });
|
|
3179
|
-
}));
|
|
3180
|
-
};
|
|
3181
|
-
/**
|
|
3182
|
-
* 1. Check if `environment` is defined. If defined, do nothing. It doesn't
|
|
3183
|
-
* destroy stacks with defined `environment`.
|
|
3184
|
-
* 1. Check if termination protection is disabled.
|
|
3185
|
-
* 1. Empty all buckets in the stack (if any).
|
|
3186
|
-
* 1. Delete the stack.
|
|
3187
|
-
*/
|
|
3188
|
-
const destroy = async ({ stackName }) => {
|
|
3189
|
-
const environment = getEnvironment();
|
|
3190
|
-
if (environment) {
|
|
3191
|
-
log.info(logPrefix$8, `Cannot destroy stack when environment (${environment}) is defined.`);
|
|
3192
|
-
return;
|
|
3193
|
-
}
|
|
3194
|
-
if (!await doesStackExist({ stackName })) {
|
|
3195
|
-
log.info(logPrefix$8, `Stack ${stackName} doesn't exist.`);
|
|
3196
|
-
return;
|
|
3197
|
-
}
|
|
3198
|
-
if (!await canDestroyStack({ stackName })) {
|
|
3199
|
-
const message = `Stack ${stackName} cannot be destroyed while TerminationProtection is enabled.`;
|
|
3200
|
-
throw new Error(message);
|
|
3201
|
-
}
|
|
3202
|
-
try {
|
|
3203
|
-
await emptyStackBuckets({ stackName });
|
|
3204
|
-
} catch (error) {
|
|
3205
|
-
log.warn(logPrefix$8, `Failed to empty buckets for stack ${stackName}: ${error?.message || error}. Proceeding with stack deletion.`);
|
|
3206
|
-
}
|
|
3207
|
-
await deleteStack({ stackName });
|
|
3208
|
-
};
|
|
3209
3356
|
const destroyCloudFormation = async ({ stackName: defaultStackName } = {}) => {
|
|
3210
3357
|
try {
|
|
3211
3358
|
log.info(logPrefix$8, "CAUTION! Starting CloudFormation destroy...");
|
|
@@ -3261,7 +3408,7 @@ const deployLambdaLayerCommand = {
|
|
|
3261
3408
|
*/
|
|
3262
3409
|
const readDockerfile = (dockerfilePath) => {
|
|
3263
3410
|
try {
|
|
3264
|
-
return fs$
|
|
3411
|
+
return fs$1.readFileSync(path$1.join(process.cwd(), dockerfilePath), "utf8");
|
|
3265
3412
|
} catch {
|
|
3266
3413
|
return "";
|
|
3267
3414
|
}
|
|
@@ -3284,11 +3431,11 @@ const readAllDeployFiles = async () => {
|
|
|
3284
3431
|
});
|
|
3285
3432
|
const results = [];
|
|
3286
3433
|
for (const file of files) try {
|
|
3287
|
-
const raw = await fs$
|
|
3434
|
+
const raw = await fs$1.promises.readFile(file, "utf-8");
|
|
3288
3435
|
const content = JSON.parse(raw);
|
|
3289
3436
|
if (content.stackName && content.outputs) results.push(content);
|
|
3290
3437
|
} catch {
|
|
3291
|
-
log.warn(logPrefix$6, `Could not read deploy file: ${path$
|
|
3438
|
+
log.warn(logPrefix$6, `Could not read deploy file: ${path$1.basename(file)}`);
|
|
3292
3439
|
}
|
|
3293
3440
|
return results.sort((a, b) => {
|
|
3294
3441
|
return a.packageName.localeCompare(b.packageName);
|
|
@@ -3453,13 +3600,16 @@ const invalidateCloudFront = async ({ outputs }) => {
|
|
|
3453
3600
|
}
|
|
3454
3601
|
}
|
|
3455
3602
|
};
|
|
3456
|
-
|
|
3603
|
+
/**
|
|
3604
|
+
* CloudFront is a global service whose API is signed in us-east-1.
|
|
3605
|
+
*/
|
|
3606
|
+
const cloudFront = new CloudFrontClient({ region: CLOUDFRONT_REGION });
|
|
3457
3607
|
try {
|
|
3458
|
-
await cloudFront.
|
|
3608
|
+
await cloudFront.send(new CreateInvalidationCommand(params));
|
|
3459
3609
|
log.info(logPrefix$5, `CloudFront Distribution ID ${distributionId} invalidated with success.`);
|
|
3460
|
-
} catch (
|
|
3610
|
+
} catch (error) {
|
|
3461
3611
|
log.error(logPrefix$5, `Error while trying to invalidate CloudFront distribution ${distributionId}.`);
|
|
3462
|
-
log.error(logPrefix$5,
|
|
3612
|
+
log.error(logPrefix$5, error);
|
|
3463
3613
|
}
|
|
3464
3614
|
} else log.info(logPrefix$5, `Cannot invalidate because distribution does not exist.`);
|
|
3465
3615
|
};
|
|
@@ -3646,7 +3796,7 @@ const getResponseHeadersPolicyConfig = ({ responseHeaders }) => {
|
|
|
3646
3796
|
*
|
|
3647
3797
|
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-function-quotas.html
|
|
3648
3798
|
*/
|
|
3649
|
-
const MAX_FUNCTION_SIZE_BYTES =
|
|
3799
|
+
const MAX_FUNCTION_SIZE_BYTES = 10240;
|
|
3650
3800
|
/**
|
|
3651
3801
|
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/writing-function-code.html
|
|
3652
3802
|
*/
|
|
@@ -3817,8 +3967,8 @@ const CLOUDFRONT_CACHE_POLICY_LOGICAL_ID = "CachePolicy";
|
|
|
3817
3967
|
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html
|
|
3818
3968
|
*/
|
|
3819
3969
|
const CACHE_POLICY_CONFIG = {
|
|
3820
|
-
DefaultTTL:
|
|
3821
|
-
MaxTTL:
|
|
3970
|
+
DefaultTTL: 86400,
|
|
3971
|
+
MaxTTL: 31536e3,
|
|
3822
3972
|
MinTTL: 0,
|
|
3823
3973
|
ParametersInCacheKeyAndForwardedToOrigin: {
|
|
3824
3974
|
CookiesConfig: { CookieBehavior: "none" },
|
|
@@ -3955,7 +4105,7 @@ const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, redirectToT
|
|
|
3955
4105
|
Comment: { "Fn::Sub": ["CloudFront Distribution for ${Project} project.", { Project: { Ref: "Project" } }] },
|
|
3956
4106
|
CustomErrorResponses: [403, 404].map((errorCode) => {
|
|
3957
4107
|
if (spa) return {
|
|
3958
|
-
ErrorCachingMinTTL:
|
|
4108
|
+
ErrorCachingMinTTL: 86400,
|
|
3959
4109
|
ErrorCode: errorCode,
|
|
3960
4110
|
ResponseCode: 200,
|
|
3961
4111
|
ResponsePagePath: "/index.html"
|
|
@@ -4466,7 +4616,7 @@ const deployStaticAppCommand = {
|
|
|
4466
4616
|
if (!cloudfront) throw new Error("The viewer-request-function-code option requires the cloudfront option.");
|
|
4467
4617
|
return true;
|
|
4468
4618
|
}).middleware(() => {
|
|
4469
|
-
|
|
4619
|
+
setEnvVar("REGION", CLOUDFRONT_REGION);
|
|
4470
4620
|
});
|
|
4471
4621
|
},
|
|
4472
4622
|
handler: ({ destroy, ...rest }) => {
|
|
@@ -4564,73 +4714,106 @@ const generateSSHCommandWithPwd = ({ userName, host, password, port }) => {
|
|
|
4564
4714
|
//#endregion
|
|
4565
4715
|
//#region src/deploy/vm/deployVM.ts
|
|
4566
4716
|
const logPrefix$3 = "deploy-vm";
|
|
4717
|
+
/**
|
|
4718
|
+
* SSH requires private keys to have restricted permissions (400 or 600 only).
|
|
4719
|
+
* Fix them when `fixPermissions` is set, otherwise throw.
|
|
4720
|
+
*/
|
|
4721
|
+
const ensureKeyPermissions = ({ keyPath, fixPermissions }) => {
|
|
4722
|
+
const permissions = statSync(keyPath).mode & 511;
|
|
4723
|
+
if (permissions === 256 || permissions === 384) {
|
|
4724
|
+
log.info(logPrefix$3, `SSH key permissions OK: ${permissions.toString(8)}`);
|
|
4725
|
+
return;
|
|
4726
|
+
}
|
|
4727
|
+
const permissionStr = permissions.toString(8);
|
|
4728
|
+
if (fixPermissions) {
|
|
4729
|
+
log.info(logPrefix$3, `Fixing SSH key permissions: ${keyPath} (${permissionStr} → 400)`);
|
|
4730
|
+
chmodSync(keyPath, 256);
|
|
4731
|
+
log.info(logPrefix$3, `Permissions set to 400 (read-only by owner)`);
|
|
4732
|
+
return;
|
|
4733
|
+
}
|
|
4734
|
+
log.error(logPrefix$3, `SSH key permissions too open: ${permissionStr} (octal)`);
|
|
4735
|
+
log.error(logPrefix$3, `SSH requires permissions 400 or 600`);
|
|
4736
|
+
log.error(logPrefix$3, `Fix manually: chmod 400 ${keyPath}`);
|
|
4737
|
+
log.error(logPrefix$3, `Or run with: --fix-permissions`);
|
|
4738
|
+
throw new Error(`Invalid SSH key permissions: ${permissionStr}. Expected 400 or 600.`);
|
|
4739
|
+
};
|
|
4740
|
+
/**
|
|
4741
|
+
* Check and fix SSH key permissions. Only invalid permissions stop the
|
|
4742
|
+
* deployment; failing to read them is a warning.
|
|
4743
|
+
*/
|
|
4744
|
+
const checkKeyPermissions = ({ keyPath, fixPermissions }) => {
|
|
4745
|
+
try {
|
|
4746
|
+
ensureKeyPermissions({
|
|
4747
|
+
keyPath,
|
|
4748
|
+
fixPermissions
|
|
4749
|
+
});
|
|
4750
|
+
} catch (error) {
|
|
4751
|
+
if (!(error instanceof Error)) {
|
|
4752
|
+
log.warn(logPrefix$3, "Warning: Could not check key permissions: Unknown error");
|
|
4753
|
+
return;
|
|
4754
|
+
}
|
|
4755
|
+
if (error.message.includes("Invalid SSH key permissions")) throw error;
|
|
4756
|
+
log.warn(logPrefix$3, `Warning: Could not check key permissions: ${error.message}`);
|
|
4757
|
+
}
|
|
4758
|
+
};
|
|
4759
|
+
const getSSHCommand = ({ userName, host, keyPath, password, port }) => {
|
|
4760
|
+
if (keyPath) return {
|
|
4761
|
+
sshCommand: generateSSHCommand({
|
|
4762
|
+
userName,
|
|
4763
|
+
host,
|
|
4764
|
+
keyPath,
|
|
4765
|
+
port
|
|
4766
|
+
}),
|
|
4767
|
+
sshPassword: void 0
|
|
4768
|
+
};
|
|
4769
|
+
if (!password) throw new Error("Password authentication selected but no password was provided.");
|
|
4770
|
+
const result = generateSSHCommandWithPwd({
|
|
4771
|
+
userName,
|
|
4772
|
+
host,
|
|
4773
|
+
password,
|
|
4774
|
+
port
|
|
4775
|
+
});
|
|
4776
|
+
return {
|
|
4777
|
+
sshCommand: result.command,
|
|
4778
|
+
sshPassword: result.password
|
|
4779
|
+
};
|
|
4780
|
+
};
|
|
4781
|
+
const validateStdin = (stdin) => {
|
|
4782
|
+
if (!stdin) {
|
|
4783
|
+
log.error(logPrefix$3, "SSH process stdin is null or undefined");
|
|
4784
|
+
return false;
|
|
4785
|
+
}
|
|
4786
|
+
if (stdin.destroyed) {
|
|
4787
|
+
log.error(logPrefix$3, "SSH process stdin has been destroyed");
|
|
4788
|
+
return false;
|
|
4789
|
+
}
|
|
4790
|
+
if (!stdin.writable) {
|
|
4791
|
+
log.error(logPrefix$3, "SSH process stdin is not writable");
|
|
4792
|
+
return false;
|
|
4793
|
+
}
|
|
4794
|
+
return true;
|
|
4795
|
+
};
|
|
4567
4796
|
const deployVM = async ({ userName, host, scriptPath, keyPath, password, port, fixPermissions = false }) => {
|
|
4568
4797
|
if (!userName || !host || !scriptPath) throw new Error("Missing required parameters: userName, host, scriptPath");
|
|
4569
4798
|
return new Promise((resolve, reject) => {
|
|
4570
4799
|
if (!keyPath && !password) throw new Error(`Authentication method required. Provide either --key-path for SSH key authentication or --password for password authentication.`);
|
|
4571
4800
|
if (keyPath && !existsSync(keyPath)) throw new Error(`SSH key not found at ${keyPath}`);
|
|
4572
|
-
if (keyPath)
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
if (fixPermissions) {
|
|
4578
|
-
log.info(logPrefix$3, `Fixing SSH key permissions: ${keyPath} (${permissionStr} → 400)`);
|
|
4579
|
-
chmodSync(keyPath, 256);
|
|
4580
|
-
log.info(logPrefix$3, `Permissions set to 400 (read-only by owner)`);
|
|
4581
|
-
} else {
|
|
4582
|
-
log.error(logPrefix$3, `SSH key permissions too open: ${permissionStr} (octal)`);
|
|
4583
|
-
log.error(logPrefix$3, `SSH requires permissions 400 or 600`);
|
|
4584
|
-
log.error(logPrefix$3, `Fix manually: ${fixCommand}`);
|
|
4585
|
-
log.error(logPrefix$3, `Or run with: --fix-permissions`);
|
|
4586
|
-
throw new Error(`Invalid SSH key permissions: ${permissionStr}. Expected 400 or 600.`);
|
|
4587
|
-
}
|
|
4588
|
-
} else log.info(logPrefix$3, `SSH key permissions OK: ${permissions.toString(8)}`);
|
|
4589
|
-
} catch (error) {
|
|
4590
|
-
if (error instanceof Error) {
|
|
4591
|
-
if (error.message.includes("Invalid SSH key permissions")) throw error;
|
|
4592
|
-
log.warn(logPrefix$3, `Warning: Could not check key permissions: ${error.message}`);
|
|
4593
|
-
} else log.warn(logPrefix$3, "Warning: Could not check key permissions: Unknown error");
|
|
4594
|
-
}
|
|
4595
|
-
let sshCommand;
|
|
4596
|
-
let sshPassword;
|
|
4597
|
-
if (keyPath) sshCommand = generateSSHCommand({
|
|
4801
|
+
if (keyPath) checkKeyPermissions({
|
|
4802
|
+
keyPath,
|
|
4803
|
+
fixPermissions
|
|
4804
|
+
});
|
|
4805
|
+
const { sshCommand, sshPassword } = getSSHCommand({
|
|
4598
4806
|
userName,
|
|
4599
4807
|
host,
|
|
4600
4808
|
keyPath,
|
|
4809
|
+
password,
|
|
4601
4810
|
port
|
|
4602
4811
|
});
|
|
4603
|
-
else {
|
|
4604
|
-
if (!password) throw new Error("Password authentication selected but no password was provided.");
|
|
4605
|
-
const result = generateSSHCommandWithPwd({
|
|
4606
|
-
userName,
|
|
4607
|
-
host,
|
|
4608
|
-
password,
|
|
4609
|
-
port
|
|
4610
|
-
});
|
|
4611
|
-
sshCommand = result.command;
|
|
4612
|
-
sshPassword = result.password;
|
|
4613
|
-
}
|
|
4614
4812
|
const sshProcess = spawn(sshCommand[0], sshCommand.slice(1), { stdio: [
|
|
4615
4813
|
"pipe",
|
|
4616
4814
|
"inherit",
|
|
4617
4815
|
"inherit"
|
|
4618
4816
|
] });
|
|
4619
|
-
const validateStdin = (stdin) => {
|
|
4620
|
-
if (!stdin) {
|
|
4621
|
-
log.error(logPrefix$3, "SSH process stdin is null or undefined");
|
|
4622
|
-
return false;
|
|
4623
|
-
}
|
|
4624
|
-
if (stdin.destroyed) {
|
|
4625
|
-
log.error(logPrefix$3, "SSH process stdin has been destroyed");
|
|
4626
|
-
return false;
|
|
4627
|
-
}
|
|
4628
|
-
if (!stdin.writable) {
|
|
4629
|
-
log.error(logPrefix$3, "SSH process stdin is not writable");
|
|
4630
|
-
return false;
|
|
4631
|
-
}
|
|
4632
|
-
return true;
|
|
4633
|
-
};
|
|
4634
4817
|
if (!validateStdin(sshProcess.stdin)) {
|
|
4635
4818
|
reject(/* @__PURE__ */ new Error("SSH process stdin is not available or not writable"));
|
|
4636
4819
|
return;
|
|
@@ -4724,7 +4907,8 @@ const reportDeployCommand = {
|
|
|
4724
4907
|
await reportToGitHubPR();
|
|
4725
4908
|
return;
|
|
4726
4909
|
}
|
|
4727
|
-
|
|
4910
|
+
const newStackName = stackName || await getStackName();
|
|
4911
|
+
await printStackOutputsAfterDeploy({ stackName: newStackName });
|
|
4728
4912
|
} catch (error) {
|
|
4729
4913
|
log.info(logPrefix$1, "Cannot report stack. Message: %s", error.message);
|
|
4730
4914
|
}
|
|
@@ -4920,21 +5104,21 @@ const deployCommand = {
|
|
|
4920
5104
|
const logPrefix = "generate-env";
|
|
4921
5105
|
const readEnvFile = async ({ envFileName, envsPath }) => {
|
|
4922
5106
|
try {
|
|
4923
|
-
return await fs$
|
|
5107
|
+
return await fs$1.promises.readFile(path$1.resolve(process.cwd(), envsPath, envFileName), "utf8");
|
|
4924
5108
|
} catch {
|
|
4925
5109
|
return;
|
|
4926
5110
|
}
|
|
4927
5111
|
};
|
|
4928
5112
|
const writeEnvFile = async ({ envFileName, content }) => {
|
|
4929
|
-
return fs$
|
|
5113
|
+
return fs$1.promises.writeFile(path$1.resolve(process.cwd(), envFileName), content);
|
|
4930
5114
|
};
|
|
4931
5115
|
const readDeployOutputLines = async ({ envFromDeployOutputs }) => {
|
|
4932
5116
|
const lines = [];
|
|
4933
5117
|
for (const { dir, variables } of envFromDeployOutputs) {
|
|
4934
|
-
const latestDeployPath = path$
|
|
5118
|
+
const latestDeployPath = path$1.resolve(process.cwd(), dir, ".carlin", LATEST_DEPLOY_OUTPUTS_FILENAME);
|
|
4935
5119
|
let latestDeploy;
|
|
4936
5120
|
try {
|
|
4937
|
-
const raw = await fs$
|
|
5121
|
+
const raw = await fs$1.promises.readFile(latestDeployPath, "utf8");
|
|
4938
5122
|
latestDeploy = JSON.parse(raw);
|
|
4939
5123
|
} catch {
|
|
4940
5124
|
log.warn(logPrefix, "Could not read latest-deploy.json from %s. Skipping.", latestDeployPath);
|
|
@@ -4981,8 +5165,9 @@ const generateEnv = async ({ defaultEnvironment, envFromDeployOutputs, path: env
|
|
|
4981
5165
|
const key = trimmed.split("=")[0].trim();
|
|
4982
5166
|
return !deployOutputKeys.has(key);
|
|
4983
5167
|
}).join("\n");
|
|
5168
|
+
const content = deployOutputLines.length > 0 ? `${filteredEnvFile}\n${deployOutputLines.join("\n")}\n` : envFile;
|
|
4984
5169
|
await writeEnvFile({
|
|
4985
|
-
content
|
|
5170
|
+
content,
|
|
4986
5171
|
envFileName: ".env"
|
|
4987
5172
|
});
|
|
4988
5173
|
log.info(logPrefix, "Generate env file %s from %s successfully.", ".env", envFileName);
|
|
@@ -5149,7 +5334,7 @@ const readConfigFiles = () => {
|
|
|
5149
5334
|
options: getConfigFileOptions()
|
|
5150
5335
|
}) || {};
|
|
5151
5336
|
});
|
|
5152
|
-
return
|
|
5337
|
+
return deepmerge.all(configs.reverse());
|
|
5153
5338
|
};
|
|
5154
5339
|
/**
|
|
5155
5340
|
* Load the appropriate .env file. If an environment is specified (e.g. `-e
|
|
@@ -5161,8 +5346,11 @@ const readConfigFiles = () => {
|
|
|
5161
5346
|
const loadDotEnv = () => {
|
|
5162
5347
|
const { environment } = getConfigFileOptions();
|
|
5163
5348
|
if (environment) {
|
|
5164
|
-
if (dotenv.config({
|
|
5165
|
-
|
|
5349
|
+
if (dotenv.config({
|
|
5350
|
+
path: path.resolve(process.cwd(), `.env.${environment}`),
|
|
5351
|
+
quiet: true
|
|
5352
|
+
}).error) dotenv.config({ quiet: true });
|
|
5353
|
+
} else dotenv.config({ quiet: true });
|
|
5166
5354
|
};
|
|
5167
5355
|
const syncEnvironmentOption = (argv) => {
|
|
5168
5356
|
const finalEnvironment = argv.environment || process.env.ENVIRONMENT;
|
|
@@ -5198,7 +5386,6 @@ const cli = () => {
|
|
|
5198
5386
|
return yargs(hideBin(process.argv)).strictCommands().scriptName(NAME).env(getEnv()).options(addGroupToOptions(options, "Common Options")).middleware(syncEnvironmentOption).middleware(handleEnvironments).middleware(({ environment }) => {
|
|
5199
5387
|
if (!["string", "undefined"].includes(typeof environment)) throw new Error(`environment type is invalid. The value: ${JSON.stringify(environment)}`);
|
|
5200
5388
|
}).middleware(({ region }) => {
|
|
5201
|
-
AWS.config.region = region;
|
|
5202
5389
|
setEnvVar("REGION", region);
|
|
5203
5390
|
}).pkgConf(getPkgConfig()).config(getConfig()).config("config", (configFilePath) => {
|
|
5204
5391
|
return readConfigFileSync({
|