UNPKG

@goldstack/utils-aws-lambda

Version:

Utilities for deploying AWS Lambda functions

75 lines 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deployFunction = void 0; const utils_aws_cli_1 = require("@goldstack/utils-aws-cli"); const utils_log_1 = require("@goldstack/utils-log"); const utils_sh_1 = require("@goldstack/utils-sh"); const path_1 = require("path"); const deployFunction = async (params) => { const targetArchive = params.targetArchiveName || `lambda-${Date.now()}.zip`; await (0, utils_sh_1.rmSafe)(targetArchive); // debug( // `[${params.functionName}] Preparing Zip from ${params.lambdaPackageDir}` // ); await (0, utils_sh_1.zip)({ directory: params.lambdaPackageDir, target: targetArchive, mode: 511, }); let fixedTargetArchive = targetArchive; if (!(0, utils_aws_cli_1.hasAwsCli)()) { // When running in Docker via AWS CLI, we need to use paths relative to the mounted /app directory (0, utils_log_1.debug)(`Original target archive path: ${targetArchive}`); // If it's an absolute path, make it relative to the working directory if ((0, path_1.isAbsolute)(fixedTargetArchive)) { fixedTargetArchive = (0, path_1.relative)(process.cwd(), fixedTargetArchive); } // Normalize to forward slashes and escape special characters fixedTargetArchive = fixedTargetArchive.split(path_1.sep).join('/'); fixedTargetArchive = fixedTargetArchive.replace(/\$/g, '\\$'); (0, utils_log_1.debug)(`Fixed target archive path: ${fixedTargetArchive}`); } else { // When running without Docker, we need to ensure correct paths for Windows const isWin = process.platform === 'win32'; if (!isWin) { fixedTargetArchive = fixedTargetArchive.replace(/\$/g, '\\$'); } else { fixedTargetArchive = fixedTargetArchive.replaceAll('\\', '\\\\'); } } const deployResult = await (0, utils_aws_cli_1.awsCli)({ credentials: params.awsCredentials, region: params.region, options: { silent: true, }, command: `lambda update-function-code --function-name ${params.functionName} --zip-file fileb://${fixedTargetArchive}`, }); if (!params.targetArchiveName) { await (0, utils_sh_1.rmSafe)(targetArchive); } // wait until lambda becomes active let counter = 0; let state = ''; while (counter < 20 && state !== 'Active') { const res = await (0, utils_aws_cli_1.awsCli)({ credentials: params.awsCredentials, region: params.region, options: { silent: true, }, command: `lambda get-function --function-name ${params.functionName}`, }); const data = JSON.parse(res); state = data.Configuration.State; counter++; } if (counter >= 20) { throw new Error(`Function was still in state '${state}' after deployment`); } return JSON.parse(deployResult); }; exports.deployFunction = deployFunction; //# sourceMappingURL=deployFunction.js.map