Đây là hàm đầu tiên, với các nhiệm vụ:
Vì nhiều lý do:
ExtractEmailLambda
, nodejs Nodejs 22.x. Chọn role đã tạo trước đó ExtractEmailFunctionRole
và chọn Create functionUpload thành công
Tiếp theo, chúng ta cần thêm biến môi trường
Key | Value | Description |
---|---|---|
QUEUE_NAME | sqs-generate-email-content-queue | Queue này dùng để chứa email tới và gọi function tạo nội dung |
BUCKET_NAME | ai-powered-email-auto-replies | Tên bucket để lấy nội dung email, vì SES không emit nội dung email mà chỉ emit key path (messageId) của email trên S3 |
Chọn Save
Configuration | Value | Description |
---|---|---|
function_name | ExtractEmailLambda | The unique name identifier for the Lambda function |
role | ExtractEmailFunctionRole | IAM role that defines the permissions and access policies for the function |
runtime | nodejs20.x | The execution environment and version for the Lambda function (Node.js 20) |
handler | index.handler | The entry point for the function |
memory_size | 128 | Amount of memory allocated to the function in MB (128MB) |
timeout | 30 | Maximum execution time allowed for the function in seconds |
// Helper function to extract email data from SES event
const extractEmailData = async (event: SESEvent) => {
const sesRecord = event.Records[0].ses;
const messageId = sesRecord.mail.messageId;
const emailData = {
sender: sesRecord.mail.source,
key: messageId,
};
// Get email content from S3
const getObjectCommand = new GetObjectCommand({
Bucket: env.bucketName,
Key: `received-email/${messageId}`
});
const response = await s3Client.send(getObjectCommand);
const emailContent = await response.Body?.transformToString();
// Parsing MIME type to JSON object
const { text, subject } = await simpleParser(emailContent);
return {
...emailData,
subject,
content: text || "No email data"
};
};
const body = await extractEmailData(event);
const command = new GetQueueUrlCommand({ // GetQueueUrlRequest
QueueName: env.queueName, // required
});
const queueUrlResponse = await sqsClient.send(command);
const params = {
QueueUrl: queueUrlResponse.QueueUrl,
MessageBody: JSON.stringify(body),
};
await sqsClient.send(new SendMessageCommand(params));
Full source code
import { Handler, SESEvent } from 'aws-lambda';
import { GetQueueUrlCommand, SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { Logger } from '@aws-lambda-powertools/logger';
import * as mailparser from 'mailparser';
require('dotenv').config();
const simpleParser = mailparser.simpleParser;
const logger = new Logger();
const env = {
queueName: process.env.QUEUE_NAME || "",
bucketName: process.env.BUCKET_NAME || ""
}
const sqsClient = new SQSClient();
const s3Client = new S3Client();
// Helper function to extract email data from SES event
const extractEmailData = async (event: SESEvent) => {
const sesRecord = event.Records[0].ses;
const messageId = sesRecord.mail.messageId;
const emailData = {
sender: sesRecord.mail.source,
key: messageId,
};
// logger.info("Email Data: ", JSON.stringify(emailData, null, 2));
// Get email content from S3
const getObjectCommand = new GetObjectCommand({
Bucket: env.bucketName,
Key: `received-email/${messageId}`
});
const response = await s3Client.send(getObjectCommand);
const emailContent = await response.Body?.transformToString();
const { text, subject } = await simpleParser(emailContent);
return {
...emailData,
subject,
content: text || "No email data"
};
};
export const handler: Handler = async (event: SESEvent): Promise<void> => {
try {
const body = await extractEmailData(event);
// logger.info({
// message: "Extracted email data",
// body: body
// });
const command = new GetQueueUrlCommand({ // GetQueueUrlRequest
QueueName: env.queueName, // required
});
const queueUrlResponse = await sqsClient.send(command);
const params = {
QueueUrl: queueUrlResponse.QueueUrl,
MessageBody: JSON.stringify(body),
};
await sqsClient.send(new SendMessageCommand(params));
}
catch (error) {
logger.error(error as string);
}
};
TriggerEmailIncoming
và chọn Create rule setExtractAndSaveToS3
và chọn NextExtractEmailLambda
và thêm action khác là thêm vào S3received-email
. Chúng ta cũng cần thêm policies để SES có thể thêm nội dung email vào S3, choose Create IAM RoleSESSaveEmailToS3Role
và Create roleSESSaveEmailToS3Policy
> SESSaveEmailToS3Policy > Add permissions