Skip to content

Commit 7fd0c6c

Browse files
authored
Merge pull request #147 from mylesboone/node18support
AWS Lambda Node 20.x Support
2 parents f0e76a3 + e6b8674 commit 7fd0c6c

File tree

6 files changed

+55
-44
lines changed

6 files changed

+55
-44
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ the email forwarding mapping from original destinations to new destination.
5959
2. In AWS Lambda, add a new function and skip selecting a blueprint.
6060

6161
- Name the function "SesForwarder" and optionally give it a description. Ensure
62-
Runtime is set to Node.js 16.x. (Node.js 18.x can be used if the AWS SDK v2
63-
module is also installed.)
62+
Runtime is set to Node.js 20.x.
6463

6564
- For the Lambda function code, either copy and paste the contents of
6665
`index.js` into the inline code editor or zip the contents of the repository

index.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

3-
var AWS = require('aws-sdk');
3+
const { S3Client, CopyObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
4+
const { SESv2Client, SendEmailCommand } = require("@aws-sdk/client-sesv2");
45

56
console.log("AWS Lambda SES Forwarder // @arithmetric // Version 5.1.0");
67

@@ -160,19 +161,19 @@ exports.fetchMessage = function(data) {
160161
data.config.emailKeyPrefix + data.email.messageId
161162
});
162163
return new Promise(function(resolve, reject) {
163-
data.s3.copyObject({
164+
data.s3.send(new CopyObjectCommand({
164165
Bucket: data.config.emailBucket,
165166
CopySource: data.config.emailBucket + '/' + data.config.emailKeyPrefix +
166167
data.email.messageId,
167168
Key: data.config.emailKeyPrefix + data.email.messageId,
168169
ACL: 'private',
169170
ContentType: 'text/plain',
170171
StorageClass: 'STANDARD'
171-
}, function(err) {
172+
}), function(err) {
172173
if (err) {
173174
data.log({
174175
level: "error",
175-
message: "copyObject() returned error:",
176+
message: "CopyObjectCommand() returned error:",
176177
error: err,
177178
stack: err.stack
178179
});
@@ -181,21 +182,21 @@ exports.fetchMessage = function(data) {
181182
}
182183

183184
// Load the raw email from S3
184-
data.s3.getObject({
185+
data.s3.send(new GetObjectCommand({
185186
Bucket: data.config.emailBucket,
186187
Key: data.config.emailKeyPrefix + data.email.messageId
187-
}, function(err, result) {
188+
}), async function(err, result) {
188189
if (err) {
189190
data.log({
190191
level: "error",
191-
message: "getObject() returned error:",
192+
message: "GetObjectCommand() returned error:",
192193
error: err,
193194
stack: err.stack
194195
});
195196
return reject(
196197
new Error("Error: Failed to load message body from S3."));
197198
}
198-
data.emailData = result.Body.toString();
199+
data.emailData = await result.Body.transformToString();
199200
return resolve(data);
200201
});
201202
});
@@ -285,19 +286,16 @@ exports.processMessage = function(data) {
285286
};
286287

287288
/**
288-
* Send email using the SES sendRawEmail command.
289+
* Send email using the SESv2 SendEmailCommand command.
289290
*
290291
* @param {object} data - Data bundle with context, email, etc.
291292
*
292293
* @return {object} - Promise resolved with data.
293294
*/
294295
exports.sendMessage = function(data) {
295296
var params = {
296-
Destinations: data.recipients,
297-
Source: data.originalRecipient,
298-
RawMessage: {
299-
Data: data.emailData
300-
}
297+
Destination: { ToAddresses: data.recipients },
298+
Content: { Raw: { Data: Buffer.from(data.emailData) } },
301299
};
302300
data.log({
303301
level: "info",
@@ -306,19 +304,19 @@ exports.sendMessage = function(data) {
306304
data.recipients.join(", ") + "."
307305
});
308306
return new Promise(function(resolve, reject) {
309-
data.ses.sendRawEmail(params, function(err, result) {
307+
data.ses.send(new SendEmailCommand(params), function(err, result) {
310308
if (err) {
311309
data.log({
312310
level: "error",
313-
message: "sendRawEmail() returned error.",
311+
message: "SendEmailCommand() returned error.",
314312
error: err,
315313
stack: err.stack
316314
});
317315
return reject(new Error('Error: Email sending failed.'));
318316
}
319317
data.log({
320318
level: "info",
321-
message: "sendRawEmail() successful.",
319+
message: "SendEmailCommand() successful.",
322320
result: result
323321
});
324322
resolve(data);
@@ -351,9 +349,9 @@ exports.handler = function(event, context, callback, overrides) {
351349
context: context,
352350
config: overrides && overrides.config ? overrides.config : defaultConfig,
353351
log: overrides && overrides.log ? overrides.log : console.log,
354-
ses: overrides && overrides.ses ? overrides.ses : new AWS.SES(),
352+
ses: overrides && overrides.ses ? overrides.ses : new SESv2Client(),
355353
s3: overrides && overrides.s3 ?
356-
overrides.s3 : new AWS.S3({signatureVersion: 'v4'})
354+
overrides.s3 : new S3Client({signatureVersion: 'v4'})
357355
};
358356
Promise.series(steps, data)
359357
.then(function(data) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"test": "nyc --statements 100 -- mocha -- --check-leaks --timeout 3000"
1010
},
1111
"dependencies": {
12-
"aws-sdk": "~2.1083.0"
12+
"@aws-sdk/client-s3": "^3.188.0",
13+
"@aws-sdk/client-ses": "^3.188.0"
1314
},
1415
"devDependencies": {
1516
"coveralls": "^3.0.7",

test/fetchMessage.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
var assert = require("assert");
55

6+
const {GetObjectCommand, CopyObjectCommand} = require('@aws-sdk/client-s3');
7+
68
var index = require("../index");
79

810
describe('index.js', function() {
@@ -25,11 +27,17 @@ describe('index.js', function() {
2527
},
2628
log: console.log,
2729
s3: {
28-
copyObject: function(options, callback) {
29-
callback(null);
30-
},
31-
getObject: function(options, callback) {
32-
callback(null, {Body: "email data"});
30+
send: function(options, callback) {
31+
if (options instanceof CopyObjectCommand)
32+
callback(null);
33+
else if (options instanceof GetObjectCommand)
34+
callback(null, {
35+
Body: {
36+
transformToString: function() {
37+
return "email data";
38+
}
39+
}
40+
});
3341
}
3442
}
3543
};
@@ -55,10 +63,7 @@ describe('index.js', function() {
5563
},
5664
log: console.log,
5765
s3: {
58-
copyObject: function(options, callback) {
59-
callback(true);
60-
},
61-
getObject: function(options, callback) {
66+
send: function(options, callback) {
6267
callback(true);
6368
}
6469
}
@@ -83,11 +88,11 @@ describe('index.js', function() {
8388
},
8489
log: console.log,
8590
s3: {
86-
copyObject: function(options, callback) {
87-
callback(null);
88-
},
89-
getObject: function(options, callback) {
90-
callback(true);
91+
send: function(options, callback) {
92+
if (options instanceof CopyObjectCommand)
93+
callback(null);
94+
else if (options instanceof GetObjectCommand)
95+
callback(true);
9196
}
9297
}
9398
};

test/handler.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
var assert = require("assert");
55
var fs = require("fs");
66

7+
const {GetObjectCommand, CopyObjectCommand} = require('@aws-sdk/client-s3');
8+
79
var index = require("../index");
810

911
describe('index.js', function() {
@@ -16,15 +18,21 @@ describe('index.js', function() {
1618
};
1719
var overrides = {
1820
s3: {
19-
copyObject: function(options, callback) {
20-
callback(null);
21-
},
22-
getObject: function(options, callback) {
23-
callback(null, {Body: "email data"});
21+
send: function(options, callback) {
22+
if (options instanceof CopyObjectCommand)
23+
callback(null);
24+
else if (options instanceof GetObjectCommand)
25+
callback(null, {
26+
Body: {
27+
transformToString: function() {
28+
return "email data";
29+
}
30+
}
31+
});
2432
}
2533
},
2634
ses: {
27-
sendRawEmail: function(options, callback) {
35+
send: function(options, callback) {
2836
callback(null, {status: "ok"});
2937
}
3038
},

test/sendMessage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('index.js', function() {
2020
context: {},
2121
log: console.log,
2222
ses: {
23-
sendRawEmail: function(options, callback) {
23+
send: function(options, callback) {
2424
callback(null, {status: "ok"});
2525
}
2626
}
@@ -45,7 +45,7 @@ describe('index.js', function() {
4545
context: {},
4646
log: console.log,
4747
ses: {
48-
sendRawEmail: function(options, callback) {
48+
send: function(options, callback) {
4949
callback(true);
5050
}
5151
}

0 commit comments

Comments
 (0)