I'm trying to send the url obtained by calling S3's getSigned url using aws ses from my lambda as shown below:
exports.handler = async (event) => {
......
.....
var signedUrl = await s3.getSignedUrl(
"getObject",
parameters,
(error, url) => {
if (error) {
console.log("Error writing to S3", error);
} else {
console.log("Pre-signed url: " + url);
var eparam = {
Destination: {
CcAddresses: ["abc@abc.com"],
ToAddresses: ["xyz@xyz.com"],
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: url // sending url as part of the email
},
Text: {
Charset: "UTF-8",
Data: "TEXT_FORMAT_BODY",
},
},
Subject: {
Charset: "UTF-8",
Data: "Test email",
},
},
Source: "abc@abc.com" /* required */,
ReplyToAddresses: [
"xyz@xyz.com",
],
};
console.log("compiling email... parameters ready", eparam);
var sendPromise = new AWS.SES().sendEmail(eparam).promise();
sendPromise
.then(function (data) {
console.log("Success!! ", data.MessageId);
})
.catch(function (err) {
console.error(err, err.stack);
});
}
}
);
};
The problem is that I'm unable to send the email as the code does not enter either the '.then(function (data)' block of the sendPromise nor does it catch any error in the ' .catch(function (err) ' part. Is there something that I'm doing wrong here? When I tried sending the email outside the callback part of the getSignedUrl method, I was able to send the email (without the url of course!) but since we need to embedd the url in the body of the email we must be able to do so in the callback. Any way we can do it ?
You should avoid mixing the use of callbacks and promises as you can run into issues like this. Try converting getSignedUrl
to this: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrlPromise-property
Also make sure you are returning a promise from your lambda handler: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
If you don't return a promise from your async handler the lambda function may finish before your promise is resolved.