我有一组 15-20 个脚本在带有 Node.js 16.x 的 AWS Lambda 上运行,这些脚本执行各种操作,包括使用 copy、headObject、putObject 和 getObject 等函数与 S3 交互。此外,我的脚本还利用 Step Functions、Glue、SNS 和 Athena 等 AWS 服务。我不能坚持使用 16.x,因为它的截止日期是 2024 年 6 月中旬。
我需要专家指导,了解使这些脚本与 Node.js 18.x 兼容所需的最小更改。虽然如果他们也可以使用 Node.js 20.x 将是有益的,但这不是目前的优先事项。
其中一个脚本如下所示:
// Script to read, modify, and write CSV files to S3 after removing quotes.
// Requires AWS-SDK v2 and utilizes AWS Step Functions.
const AWS = require('aws-sdk');
const sf = new AWS.StepFunctions();
const s3 = new AWS.S3();
const rl = require('readline');
const stream = require('stream');
// Randomized AWS credentials
AWS.config.update({
region: {mention-region-here}
});
function extractBucketName(path) {
var parts = path.split("/", 3);
return parts[2];
}
function extractBucketKey(path) {
var s = path.replace(/^s3:\/\//, "");
var pos = s.indexOf("/");
var key = s.substr(pos + 1);
return key;
}
exports.handler = function(event, context, callback) {
// Main logic to read from S3, process, and write back to S3.
if ((typeof event.inputFile != "undefined") && (typeof event.outputFile != "undefined")) {
var paramsInput = {
Bucket: extractBucketName(event.inputFile),
Key: extractBucketKey(event.inputFile)
};
// Create read stream from S3
var readStream = s3.getObject(paramsInput).createReadStream();
// Pass the S3 read stream into the readline interface to break into lines
var lineReader = rl.createInterface({
input: readStream,
terminal: false
});
// Create a write stream to upload processed data back to S3
const writeStream = new stream.PassThrough();
var paramsOutput = {
Bucket: extractBucketName(event.outputFile),
Key: extractBucketKey(event.outputFile),
Body: writeStream
};
s3.upload(paramsOutput).promise();
// Process each line from the input file
lineReader.on('line', function(line) {
// Perform processing on line (remove quotes)
line = line.replace(/['"]+/g, '');
// Write processed line to the write stream
writeStream.write(`${line}\n`);
}).on('close', function() {
// Close the write stream after processing is complete
writeStream.end();
callback(null);
// Example code to send success token to AWS Step Functions
var taskParams = {
output: JSON.stringify('Write a success message'),
taskToken: event.token
};
sf.sendTaskSuccess(taskParams, function(err, data) {
if (err) {
console.error(err.message);
callback(err.message);
return;
}
console.log(data);
callback(null);
});
});
}
};
我已经阅读了 Amazon 的文章和文档,但我仍然不清楚从 Node.js 16.x 到 18.x 的过渡哪些需要修改,哪些不需要修改。我想避免尝试错误的方法,以免我陷入错误噩梦。对于平稳过渡所需的任何变革,来自社区的任何专家建议将不胜感激。谢谢!
在开发中将 NodeJs 从 16.x 升级到 18.x 后,我尝试了以下文章: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/ 和其他文章,但不幸的是,这并没有提供我需要的所有信息。 另外,我读过这个问题: 切换到 18.x 后,Lambda 函数无法加载 AWS sdk
但这仅适用于 s3,并不能完全帮助
从 16 升级到 18/20 的部分问题是 aws-sdk 不再捆绑在环境中,并且已升级到 v3
直到 Node.js 16,Lambda 的 Node.js 运行时都包含 AWS SDK JavaScript 版本 2。此后已被 AWS 取代 SDK for JavaScript 第 3 版,于 2020 年 12 月发布。 从 Node.js 18 开始,一直到 Node.js 20,Lambda Node.js 运行时已升级 AWS 开发工具包的版本 JavaScript 包含在从 v2 到 v3 的运行时中。客户升级 来自使用随附 AWS 开发工具包的 Node.js 16 或更早版本的运行时 JavaScript v2 应该升级他们的代码以使用 v3 SDK。
参考:https://aws.amazon.com/blogs/compute/node-js-20-x-runtime-now-available-in-aws-lambda/
对您有利的可能是自动迁移/codemod 脚本,详细信息请参阅 https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/migration.html#migration-to-v3
例如,您可以在 example.ts 上运行 aws-sdk v2-to-v3 转换,如下所示。
$ npx aws-sdk-js-codemod -t v2-to-v3 example.ts
迁移的结果还应该减少运行应用程序所需的开销,来自上面的迁移链接
aws-sdk 软件包为您的应用程序增加了约 40 MB 的空间。更换 var AWS = require("aws-sdk") 并导入 {DynamoDB} from “@aws-sdk/client-dynamodb”将该开销减少到大约 3 MB。 将导入限制为仅限 DynamoDB 客户端,并且 ListTablesCommand 命令将开销减少到不到 100 KB。