carlin 1.31.11 → 1.31.13

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.
Files changed (74) hide show
  1. package/dist/index.js +4477 -4
  2. package/package.json +10 -10
  3. package/dist/cli.js +0 -246
  4. package/dist/config.js +0 -11
  5. package/dist/deploy/addDefaults.cloudformation.js +0 -151
  6. package/dist/deploy/baseStack/command.js +0 -9
  7. package/dist/deploy/baseStack/config.js +0 -30
  8. package/dist/deploy/baseStack/deployBaseStack.js +0 -62
  9. package/dist/deploy/baseStack/getBaseStackResource.js +0 -27
  10. package/dist/deploy/baseStack/getBucketTemplate.js +0 -46
  11. package/dist/deploy/baseStack/getLambdaImageBuilderTemplate.js +0 -188
  12. package/dist/deploy/baseStack/getLambdaLayerBuilderTemplate.js +0 -142
  13. package/dist/deploy/baseStack/getVpcTemplate.js +0 -169
  14. package/dist/deploy/cicd/cicd.template.js +0 -938
  15. package/dist/deploy/cicd/command.js +0 -31
  16. package/dist/deploy/cicd/command.options.js +0 -79
  17. package/dist/deploy/cicd/config.js +0 -8
  18. package/dist/deploy/cicd/deployCicd.js +0 -121
  19. package/dist/deploy/cicd/ecsTaskReportCommand.js +0 -55
  20. package/dist/deploy/cicd/getCicdStackName.js +0 -11
  21. package/dist/deploy/cicd/getTriggerPipelineObjectKey.js +0 -11
  22. package/dist/deploy/cicd/lambdas/cicdApiV1.handler.js +0 -124
  23. package/dist/deploy/cicd/lambdas/ecsTaskReport.handler.js +0 -126
  24. package/dist/deploy/cicd/lambdas/executeTasks.js +0 -67
  25. package/dist/deploy/cicd/lambdas/getProcessEnvVariable.js +0 -10
  26. package/dist/deploy/cicd/lambdas/githubWebhooksApiV1.handler.js +0 -148
  27. package/dist/deploy/cicd/lambdas/imageUpdaterSchedule.handler.js +0 -44
  28. package/dist/deploy/cicd/lambdas/index.js +0 -13
  29. package/dist/deploy/cicd/lambdas/pipelines.handler.js +0 -160
  30. package/dist/deploy/cicd/lambdas/putApprovalResultManualTask.js +0 -51
  31. package/dist/deploy/cicd/lambdas/shConditionalCommands.js +0 -30
  32. package/dist/deploy/cicd/pipelines.js +0 -86
  33. package/dist/deploy/cicd/readSSHKey.js +0 -34
  34. package/dist/deploy/cloudformation.core.js +0 -379
  35. package/dist/deploy/cloudformation.js +0 -189
  36. package/dist/deploy/command.js +0 -205
  37. package/dist/deploy/lambda/buildLambdaSingleFile.js +0 -67
  38. package/dist/deploy/lambda/deployLambdaCode.js +0 -43
  39. package/dist/deploy/lambda/deployLambdaLayers.js +0 -36
  40. package/dist/deploy/lambda/uploadCodeToECR.js +0 -53
  41. package/dist/deploy/lambda/uploadCodeToS3.js +0 -33
  42. package/dist/deploy/lambdaLayer/command.js +0 -50
  43. package/dist/deploy/lambdaLayer/deployLambdaLayer.js +0 -139
  44. package/dist/deploy/lambdaLayer/getPackageLambdaLayerStackName.js +0 -21
  45. package/dist/deploy/readDockerfile.js +0 -40
  46. package/dist/deploy/s3.js +0 -210
  47. package/dist/deploy/stackName.js +0 -85
  48. package/dist/deploy/staticApp/command.js +0 -86
  49. package/dist/deploy/staticApp/deployStaticApp.js +0 -65
  50. package/dist/deploy/staticApp/findDefaultBuildFolder.js +0 -44
  51. package/dist/deploy/staticApp/getStaticAppBucket.js +0 -19
  52. package/dist/deploy/staticApp/invalidateCloudFront.js +0 -44
  53. package/dist/deploy/staticApp/removeOldVersions.js +0 -56
  54. package/dist/deploy/staticApp/staticApp.template.js +0 -371
  55. package/dist/deploy/staticApp/uploadBuiltAppToS3.js +0 -28
  56. package/dist/deploy/utils.js +0 -31
  57. package/dist/deploy/vercel/command.js +0 -31
  58. package/dist/deploy/vercel/deployVercel.js +0 -59
  59. package/dist/generateEnv/generateEnv.js +0 -64
  60. package/dist/generateEnv/generateEnvCommand.js +0 -29
  61. package/dist/utils/addGroupToOptions.js +0 -11
  62. package/dist/utils/cloudFormationTemplate.js +0 -142
  63. package/dist/utils/codeBuild.js +0 -52
  64. package/dist/utils/environmentVariables.js +0 -16
  65. package/dist/utils/exec.js +0 -26
  66. package/dist/utils/formatCode.js +0 -34
  67. package/dist/utils/getAwsAccountId.js +0 -10
  68. package/dist/utils/getCurrentBranch.js +0 -35
  69. package/dist/utils/getEnvironment.js +0 -8
  70. package/dist/utils/getIamPath.js +0 -6
  71. package/dist/utils/getProjectName.js +0 -35
  72. package/dist/utils/index.js +0 -31
  73. package/dist/utils/packageJson.js +0 -32
  74. package/dist/utils/spawn.js +0 -34
@@ -1,205 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.deployCommand = exports.examples = exports.options = void 0;
7
- const utils_1 = require("../utils");
8
- const command_1 = require("./baseStack/command");
9
- const command_2 = require("./cicd/command");
10
- const cloudformation_1 = require("./cloudformation");
11
- const command_3 = require("./lambdaLayer/command");
12
- const command_4 = require("./staticApp/command");
13
- const command_5 = require("./vercel/command");
14
- const stackName_1 = require("./stackName");
15
- const cloudformation_core_1 = require("./cloudformation.core");
16
- const readDockerfile_1 = require("./readDockerfile");
17
- const npmlog_1 = __importDefault(require("npmlog"));
18
- const logPrefix = 'deploy';
19
- const checkAwsAccountId = async (awsAccountId) => {
20
- try {
21
- const currentAwsAccountId = await (0, utils_1.getAwsAccountId)();
22
- if (String(awsAccountId) !== String(currentAwsAccountId)) {
23
- throw new Error(`AWS account id does not match. Current is "${currentAwsAccountId}" but the defined in configuration files is "${awsAccountId}".`);
24
- }
25
- }
26
- catch (error) {
27
- if (error.code === 'CredentialsError') {
28
- /**
29
- * No credentials found.
30
- */
31
- return;
32
- }
33
- npmlog_1.default.error(logPrefix, error.message);
34
- process.exit();
35
- }
36
- };
37
- const describeDeployCommand = {
38
- command: 'describe',
39
- describe: 'Print the outputs of the deployment.',
40
- handler: async ({ stackName }) => {
41
- try {
42
- const newStackName = stackName || (await (0, stackName_1.getStackName)());
43
- await (0, cloudformation_core_1.printStackOutputsAfterDeploy)({ stackName: newStackName });
44
- }
45
- catch (error) {
46
- npmlog_1.default.info(logPrefix, 'Cannot describe stack. Message: %s', error.message);
47
- }
48
- },
49
- };
50
- exports.options = {
51
- 'aws-account-id': {
52
- description: 'AWS account id associated with the deployment.',
53
- type: 'string',
54
- },
55
- destroy: {
56
- default: false,
57
- description: 'Destroy the deployment. You cannot destroy a deploy with "environment" is defined.',
58
- type: 'boolean',
59
- },
60
- 'lambda-dockerfile': {
61
- coerce: (arg) => {
62
- return (0, readDockerfile_1.readDockerfile)(arg);
63
- },
64
- default: 'Dockerfile',
65
- describe: 'Instructions to create the Lambda image.',
66
- type: 'string',
67
- },
68
- 'lambda-image': {
69
- default: false,
70
- describe: 'A Lambda image will be created instead using S3.',
71
- type: 'boolean',
72
- },
73
- 'lambda-externals': {
74
- default: [],
75
- describe: 'Lambda external packages.',
76
- type: 'array',
77
- },
78
- 'lambda-input': {
79
- default: 'src/lambda.ts',
80
- describe: 'Lambda input file. This file export all handlers used by the Lambda Functions.',
81
- type: 'string',
82
- },
83
- /**
84
- * This option has the format:
85
- *
86
- * ```ts
87
- * {
88
- * key: string,
89
- * value: string,
90
- * usePreviousValue: boolean,
91
- * resolvedValue: string
92
- * }[]
93
- * ```
94
- */
95
- parameters: {
96
- alias: 'p',
97
- default: [],
98
- describe: 'A list of parameters that will be passed to CloudFormation Parameters when deploying.',
99
- },
100
- 'skip-deploy': {
101
- alias: 'skip',
102
- default: false,
103
- describe: 'Skip deploy.',
104
- type: 'boolean',
105
- },
106
- 'stack-name': {
107
- describe: 'CloudFormation Stack name.',
108
- type: 'string',
109
- },
110
- 'template-path': {
111
- alias: 't',
112
- type: 'string',
113
- },
114
- };
115
- exports.examples = [
116
- [
117
- 'carlin deploy -t src/cloudformation.template1.yml',
118
- 'Change the CloudFormation template path.',
119
- ],
120
- ['carlin deploy -e Production', 'Set environment.'],
121
- [
122
- 'carlin deploy --lambda-input src/lambda/index.ts --lambda-externals momentjs',
123
- "Lambda exists. Don't bundle momentjs.",
124
- ],
125
- [
126
- 'carlin deploy --destroy --stack-name StackToBeDeleted',
127
- 'Destroy a specific stack.',
128
- ],
129
- ];
130
- exports.deployCommand = {
131
- command: 'deploy [deploy]',
132
- describe: 'Deploy cloud resources.',
133
- builder: (yargsBuilder) => {
134
- yargsBuilder
135
- .example(exports.examples)
136
- .options((0, utils_1.addGroupToOptions)(exports.options, 'Deploy Options'))
137
- /**
138
- * Set stack name.
139
- */
140
- .middleware(({ stackName }) => {
141
- if (stackName) {
142
- (0, stackName_1.setPreDefinedStackName)(stackName);
143
- }
144
- })
145
- /**
146
- * Set lambdaImage if lambdaDockerfile exists.
147
- */
148
- .middleware((argv) => {
149
- if (argv.lambdaDockerfile) {
150
- Object.assign(argv, {
151
- lambdaImage: true,
152
- });
153
- }
154
- })
155
- /**
156
- * Check AWS account id.
157
- */
158
- .middleware(async ({ environments, environment, awsAccountId: defaultAwsAccountId, }) => {
159
- const envAwsAccountId = (() => {
160
- return environments && environment && environments[environment]
161
- ? environments[environment].awsAccountId
162
- : undefined;
163
- })();
164
- if (envAwsAccountId) {
165
- await checkAwsAccountId(envAwsAccountId);
166
- }
167
- if (defaultAwsAccountId) {
168
- await checkAwsAccountId(defaultAwsAccountId);
169
- }
170
- })
171
- .middleware(({ skipDeploy }) => {
172
- if (skipDeploy) {
173
- npmlog_1.default.warn(logPrefix, "Skip deploy flag is true, then the deploy command wasn't executed.");
174
- process.exit(0);
175
- }
176
- });
177
- const commands = [
178
- command_3.deployLambdaLayerCommand,
179
- describeDeployCommand,
180
- command_1.deployBaseStackCommand,
181
- command_4.deployStaticAppCommand,
182
- command_2.deployCicdCommand,
183
- command_5.deployVercelCommand,
184
- ];
185
- yargsBuilder.positional('deploy', {
186
- choices: commands.map(({ command }) => {
187
- return command;
188
- }),
189
- describe: 'Type of deployment.',
190
- type: 'string',
191
- });
192
- commands.forEach((command) => {
193
- return yargsBuilder.command(command);
194
- });
195
- return yargsBuilder;
196
- },
197
- handler: ({ destroy, ...rest }) => {
198
- if (destroy) {
199
- (0, cloudformation_1.destroyCloudFormation)();
200
- }
201
- else {
202
- (0, cloudformation_1.deployCloudFormation)(rest);
203
- }
204
- },
205
- };
@@ -1,67 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.buildLambdaSingleFile = void 0;
30
- const esbuild = __importStar(require("esbuild"));
31
- const builtin_modules_1 = __importDefault(require("builtin-modules"));
32
- const npmlog_1 = __importDefault(require("npmlog"));
33
- const path_1 = __importDefault(require("path"));
34
- const logPrefix = 'lambda';
35
- const outFolder = 'dist';
36
- const outFile = 'index.js';
37
- const buildLambdaSingleFile = async ({ lambdaExternals, lambdaInput, }) => {
38
- npmlog_1.default.info(logPrefix, 'Building Lambda single file...');
39
- const { errors } = esbuild.buildSync({
40
- banner: {
41
- js: '// Powered by carlin (https://ttoss.dev/docs/carlin/)',
42
- },
43
- bundle: true,
44
- entryPoints: [path_1.default.resolve(process.cwd(), lambdaInput)],
45
- external: [
46
- /**
47
- * Only AWS SDK v3 on Node.js 18.x or higher.
48
- * https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/
49
- */
50
- '@aws-sdk/*',
51
- ...builtin_modules_1.default,
52
- ...lambdaExternals,
53
- ],
54
- /**
55
- * https://esbuild.github.io/api/#minify
56
- */
57
- minifySyntax: true,
58
- platform: 'node',
59
- outfile: path_1.default.join(process.cwd(), outFolder, outFile),
60
- target: 'node20',
61
- treeShaking: true,
62
- });
63
- if (errors.length > 0) {
64
- throw errors;
65
- }
66
- };
67
- exports.buildLambdaSingleFile = buildLambdaSingleFile;
@@ -1,43 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.deployLambdaCode = void 0;
7
- const buildLambdaSingleFile_1 = require("./buildLambdaSingleFile");
8
- const deployLambdaLayers_1 = require("./deployLambdaLayers");
9
- const uploadCodeToECR_1 = require("./uploadCodeToECR");
10
- const uploadCodeToS3_1 = require("./uploadCodeToS3");
11
- const fs_1 = __importDefault(require("fs"));
12
- const npmlog_1 = __importDefault(require("npmlog"));
13
- const logPrefix = 'lambda';
14
- /**
15
- * 1. Build Lambda code using esbuild. The build process will create a single
16
- * file with all your code.
17
- * 1. The build will ignore packages on `lambda-externals` option.
18
- * 1. Zip the output file.
19
- * 1. Upload the zipped code to base stack bucket.
20
- * 1. Add the code bucket, key and version to the CloudFormation template as
21
- * parameters.
22
- */
23
- const deployLambdaCode = async ({ lambdaDockerfile, lambdaExternals, lambdaImage, lambdaInput, stackName, }) => {
24
- if (!fs_1.default.existsSync(lambdaInput)) {
25
- return undefined;
26
- }
27
- npmlog_1.default.info(logPrefix, 'Deploying Lambda code...');
28
- await (0, buildLambdaSingleFile_1.buildLambdaSingleFile)({ lambdaExternals, lambdaInput });
29
- const { bucket, key, versionId } = await (0, uploadCodeToS3_1.uploadCodeToS3)({ stackName });
30
- if (!lambdaImage) {
31
- await (0, deployLambdaLayers_1.deployLambdaLayers)({ lambdaExternals });
32
- return { bucket, key, versionId };
33
- }
34
- const { imageUri } = await (0, uploadCodeToECR_1.uploadCodeToECR)({
35
- bucket,
36
- key,
37
- versionId,
38
- lambdaDockerfile,
39
- lambdaExternals,
40
- });
41
- return { imageUri };
42
- };
43
- exports.deployLambdaCode = deployLambdaCode;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.deployLambdaLayers = void 0;
7
- const deployLambdaLayer_1 = require("../lambdaLayer/deployLambdaLayer");
8
- const npmlog_1 = __importDefault(require("npmlog"));
9
- const path_1 = __importDefault(require("path"));
10
- const logPrefix = 'lambda';
11
- const deployLambdaLayers = async ({ lambdaExternals = [], }) => {
12
- if (lambdaExternals.length === 0) {
13
- return;
14
- }
15
- npmlog_1.default.info(logPrefix, `--lambda-externals [${lambdaExternals.join(', ')}] was found. Creating other layers...`);
16
- const { dependencies = {} } = (() => {
17
- try {
18
- return require(path_1.default.resolve(process.cwd(), 'package.json')) || {};
19
- }
20
- catch (err) {
21
- npmlog_1.default.error(logPrefix, 'Cannot read package.json. Error message: %j', err.message);
22
- return {};
23
- }
24
- })();
25
- const packages = lambdaExternals.map((lambdaExternal) => {
26
- try {
27
- const semver = dependencies[lambdaExternal].replace(/(~|\^)/g, '');
28
- return `${lambdaExternal}@${semver}`;
29
- }
30
- catch {
31
- throw new Error(`Cannot find ${lambdaExternal} on package.json dependencies.`);
32
- }
33
- });
34
- await (0, deployLambdaLayer_1.deployLambdaLayer)({ packages, deployIfExists: false });
35
- };
36
- exports.deployLambdaLayers = deployLambdaLayers;
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uploadCodeToECR = void 0;
4
- const config_1 = require("../../config");
5
- const aws_sdk_1 = require("aws-sdk");
6
- const getBaseStackResource_1 = require("../baseStack/getBaseStackResource");
7
- const utils_1 = require("../../utils");
8
- const codeBuild = new aws_sdk_1.CodeBuild({ region: config_1.AWS_DEFAULT_REGION });
9
- const uploadCodeToECR = async ({ bucket, key, lambdaExternals, lambdaDockerfile, }) => {
10
- const TEMP = 1;
11
- if (TEMP) {
12
- throw new Error('uploadCodeToECR not finished yet.');
13
- }
14
- const lambdaBuilder = await (0, getBaseStackResource_1.getBaseStackResource)('BASE_STACK_LAMBDA_IMAGE_BUILDER_LOGICAL_NAME');
15
- const defaultDockerfile = [
16
- 'FROM public.ecr.aws/lambda/nodejs:14',
17
- // eslint-disable-next-line no-template-curly-in-string
18
- 'COPY . ${LAMBDA_TASK_ROOT}',
19
- 'RUN npm install',
20
- ].join('\n');
21
- const { build } = await codeBuild
22
- .startBuild({
23
- environmentVariablesOverride: [
24
- {
25
- name: 'DOCKERFILE',
26
- value: lambdaDockerfile || defaultDockerfile,
27
- },
28
- {
29
- name: 'LAMBDA_EXTERNALS',
30
- value: lambdaExternals.join(' '),
31
- },
32
- {
33
- name: 'IMAGE_TAG',
34
- value: (0, utils_1.getPackageVersion)(),
35
- },
36
- {
37
- name: 'REPOSITORY_ECR_REPOSITORY',
38
- value: 'testtesteste',
39
- },
40
- ],
41
- projectName: lambdaBuilder,
42
- sourceLocationOverride: `${bucket}/${key}`,
43
- sourceTypeOverride: 'S3',
44
- })
45
- .promise();
46
- if (!build?.id) {
47
- throw new Error('Cannot start build.');
48
- }
49
- await (0, utils_1.waitCodeBuildFinish)({ buildId: build.id, name: 'lambda-builder' });
50
- const imageUri = '178804353523.dkr.ecr.us-east-1.amazonaws.com/testtesteste:0.0.1';
51
- return { imageUri };
52
- };
53
- exports.uploadCodeToECR = uploadCodeToECR;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.uploadCodeToS3 = void 0;
7
- const getBaseStackResource_1 = require("../baseStack/getBaseStackResource");
8
- const s3_1 = require("../s3");
9
- const adm_zip_1 = __importDefault(require("adm-zip"));
10
- const fs_1 = __importDefault(require("fs"));
11
- const npmlog_1 = __importDefault(require("npmlog"));
12
- const path_1 = __importDefault(require("path"));
13
- const logPrefix = 'lambda';
14
- const outFolder = 'dist';
15
- const outFile = 'index.js';
16
- const uploadCodeToS3 = async ({ stackName }) => {
17
- npmlog_1.default.info(logPrefix, `Uploading code to S3...`);
18
- const zip = new adm_zip_1.default();
19
- const code = fs_1.default.readFileSync(path_1.default.join(process.cwd(), outFolder, outFile));
20
- zip.addFile('index.js', code);
21
- if (!fs_1.default.existsSync('dist')) {
22
- fs_1.default.mkdirSync('dist');
23
- }
24
- zip.writeZip('dist/lambda.zip');
25
- const bucketName = await (0, getBaseStackResource_1.getBaseStackResource)('BASE_STACK_BUCKET_LOGICAL_NAME');
26
- return (0, s3_1.uploadFileToS3)({
27
- bucket: bucketName,
28
- contentType: 'application/zip',
29
- key: `lambdas/${stackName}/lambda.zip`,
30
- file: zip.toBuffer(),
31
- });
32
- };
33
- exports.uploadCodeToS3 = uploadCodeToS3;
@@ -1,50 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.deployLambdaLayerCommand = exports.options = void 0;
7
- const npmlog_1 = __importDefault(require("npmlog"));
8
- const config_1 = require("../../config");
9
- const utils_1 = require("../../utils");
10
- const deployLambdaLayer_1 = require("./deployLambdaLayer");
11
- const logPrefix = 'deploy-lambda-layer';
12
- /**
13
- * https://stackoverflow.com/a/64880672/8786986
14
- */
15
- const packageNameRegex = /@[~^]?([\dvx*]+(?:[-.](?:[\dx*]+|alpha|beta))*)/;
16
- exports.options = {
17
- packages: {
18
- array: true,
19
- describe: `NPM packages' names to be deployed as Lambda Layers. It must follow the format: ${packageNameRegex.toString()}.`,
20
- required: true,
21
- type: 'string',
22
- },
23
- };
24
- exports.deployLambdaLayerCommand = {
25
- command: 'lambda-layer',
26
- describe: 'Deploy Lambda Layer.',
27
- builder: (yargs) => yargs
28
- .options((0, utils_1.addGroupToOptions)(exports.options, 'Deploy Lambda Layer Options'))
29
- .check(({ packages }) => {
30
- const invalidPackages = packages
31
- .map((packageName) => {
32
- return packageNameRegex.test(packageName) ? undefined : packageName;
33
- })
34
- .filter((packageName) => !!packageName);
35
- if (invalidPackages.length > 0) {
36
- throw new Error(`Some package names are invalid: ${invalidPackages.join(', ')}. The package must follow the pattern: ${packageNameRegex.toString()}.`);
37
- }
38
- else {
39
- return true;
40
- }
41
- }),
42
- handler: ({ destroy, ...rest }) => {
43
- if (destroy) {
44
- npmlog_1.default.info(logPrefix, `${config_1.NAME} doesn't destroy lambda layers.`);
45
- }
46
- else {
47
- (0, deployLambdaLayer_1.deployLambdaLayer)(rest);
48
- }
49
- },
50
- };
@@ -1,139 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.deployLambdaLayer = exports.getLambdaLayerTemplate = void 0;
7
- const utils_1 = require("../../utils");
8
- const aws_sdk_1 = require("aws-sdk");
9
- const config_1 = require("../../config");
10
- const cloudformation_core_1 = require("../cloudformation.core");
11
- const getBaseStackResource_1 = require("../baseStack/getBaseStackResource");
12
- const getPackageLambdaLayerStackName_1 = require("./getPackageLambdaLayerStackName");
13
- const utils_2 = require("../utils");
14
- const npmlog_1 = __importDefault(require("npmlog"));
15
- const logPrefix = 'lambda-layer';
16
- const createLambdaLayerZipFile = async ({ codeBuildProjectName, packageName, }) => {
17
- npmlog_1.default.info(logPrefix, `Creating zip file for package ${packageName}...`);
18
- const codeBuild = new aws_sdk_1.CodeBuild();
19
- const { build } = await codeBuild
20
- .startBuild({
21
- environmentVariablesOverride: [
22
- {
23
- name: 'PACKAGE_NAME',
24
- value: packageName,
25
- },
26
- ],
27
- projectName: codeBuildProjectName,
28
- })
29
- .promise();
30
- if (!build?.id) {
31
- throw new Error('Cannot start build.');
32
- }
33
- const result = await (0, utils_1.waitCodeBuildFinish)({
34
- buildId: build.id,
35
- name: packageName,
36
- });
37
- if (result.artifacts?.location) {
38
- const location = result.artifacts.location.split('/');
39
- const bucket = location.shift()?.replace('arn:aws:s3:::', '');
40
- if (!bucket) {
41
- throw new Error('Cannot retrieve bucket name.');
42
- }
43
- const key = location.join('/');
44
- return { bucket, key };
45
- }
46
- throw new Error(`Cannot get artifact location for package ${packageName}`);
47
- };
48
- /**
49
- * The CloudFormation template created to deploy a Lambda Layer.
50
- *
51
- * - The Layer name is the same as the Stack name.
52
- */
53
- const getLambdaLayerTemplate = ({ bucket, key, packageName, }) => {
54
- const description = packageName
55
- /**
56
- * Description has limit of 256.
57
- * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-layerversion.html#cfn-lambda-layerversion-description
58
- */
59
- .substring(0, 256);
60
- return {
61
- AWSTemplateFormatVersion: '2010-09-09',
62
- Resources: {
63
- LambdaLayer: {
64
- Type: 'AWS::Lambda::LayerVersion',
65
- Properties: {
66
- CompatibleRuntimes: [config_1.NODE_RUNTIME],
67
- Content: {
68
- S3Bucket: bucket,
69
- S3Key: key,
70
- },
71
- Description: description,
72
- LayerName: { Ref: 'AWS::StackName' },
73
- },
74
- },
75
- },
76
- Outputs: {
77
- LambdaLayerVersion: {
78
- Description: description,
79
- Value: { Ref: 'LambdaLayer' },
80
- Export: {
81
- Name: { Ref: 'AWS::StackName' },
82
- },
83
- },
84
- },
85
- };
86
- };
87
- exports.getLambdaLayerTemplate = getLambdaLayerTemplate;
88
- const getPackagesThatAreNotDeployed = async ({ packages, }) => {
89
- return (await Promise.all(packages.map(async (packageName) => {
90
- const stackName = (0, getPackageLambdaLayerStackName_1.getPackageLambdaLayerStackName)(packageName);
91
- return (await (0, cloudformation_core_1.doesStackExist)({ stackName })) ? '' : packageName;
92
- }))).filter((packageName) => {
93
- return !!packageName;
94
- });
95
- };
96
- const deployLambdaLayer = async ({ packages, deployIfExists = true, }) => {
97
- try {
98
- const packagesToBeDeployed = deployIfExists
99
- ? packages
100
- : await getPackagesThatAreNotDeployed({ packages });
101
- if (packagesToBeDeployed.length === 0) {
102
- return;
103
- }
104
- const codeBuildProjectName = await (0, getBaseStackResource_1.getBaseStackResource)('BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME');
105
- if (!codeBuildProjectName) {
106
- throw new Error("Cannot deploy lambda-layer because AWS CodeBuild project doesn't exist.");
107
- }
108
- const deployLambdaLayerSinglePackage = async (packageName) => {
109
- try {
110
- const { bucket, key } = await createLambdaLayerZipFile({
111
- codeBuildProjectName,
112
- packageName,
113
- });
114
- const lambdaLayerTemplate = (0, exports.getLambdaLayerTemplate)({
115
- packageName,
116
- bucket,
117
- key,
118
- });
119
- await (0, cloudformation_core_1.deploy)({
120
- template: lambdaLayerTemplate,
121
- terminationProtection: true,
122
- params: { StackName: (0, getPackageLambdaLayerStackName_1.getPackageLambdaLayerStackName)(packageName) },
123
- });
124
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
125
- }
126
- catch (error) {
127
- (0, utils_2.handleDeployError)({ error, logPrefix });
128
- }
129
- };
130
- await Promise.all(packagesToBeDeployed.map((packageName) => {
131
- return deployLambdaLayerSinglePackage(packageName);
132
- }));
133
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
134
- }
135
- catch (error) {
136
- (0, utils_2.handleDeployError)({ error, logPrefix });
137
- }
138
- };
139
- exports.deployLambdaLayer = deployLambdaLayer;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPackageLambdaLayerStackName = exports.lambdaLayerStackNamePrefix = void 0;
4
- const change_case_1 = require("change-case");
5
- exports.lambdaLayerStackNamePrefix = `LambdaLayer`;
6
- const getPackageLambdaLayerStackName = (packageName) => {
7
- const [scopedName, version] = packageName.split('@').filter((part) => {
8
- return !!part;
9
- });
10
- return [
11
- exports.lambdaLayerStackNamePrefix,
12
- (0, change_case_1.pascalCase)(scopedName),
13
- version
14
- /**
15
- * Replace everything except alphanumeric characters and dots.
16
- */
17
- .replace(/[^0-9.]/g, '')
18
- .replace(/\./g, '-'),
19
- ].join('-');
20
- };
21
- exports.getPackageLambdaLayerStackName = getPackageLambdaLayerStackName;