Tạo Send Email Function

Đây là function cuối cùng, với các nhiệm vụ:

  • Nhận tin nhắn SQS và gửi phản hồi cho người dùng
  • Lưu phản hồi đó vào S3.

Tại sao tôi cần function này?

  • Bởi vì tôi muốn lưu việc gửi phản hồi email, debug agent email của tôi dựa trên email phản hồi.
  • Dễ dàng hơn để debug

Các bước thực hiện

Bạn có thể xem lại mục 8.1, 8.2 để upload source code

Bạn có thể download tại index.zip hoặc bạn có thể build tại đây tại đây. Sau khi đã có thì chọn Code > Upload from > .zip file

Tiếp theo là thêm biến môi trường

lambda14

Chúng ta sẽ có 2 giá trị

KeyValueDescription
SOURCE_EMAILsupport@YOUR_DOMAINĐây là email mà bạn muốn trả lời (sender)
BUCKET_NAMEai-powered-email-auto-repliesS3 bucket để lưu nội dung phản hồi

Chọn Save

lambda15

Cập nhật biến môi trường thành công

lambda16

Thông tin chi tiết

ConfigurationValueDescription
function_nameSendEmailLambdaThe unique name identifier for the Lambda function
roleSendEmailFunctionRoleIAM role that defines the permissions and access policies for the function
runtimenodejs20.xThe execution environment and version for the Lambda function (Node.js 20)
handlerindex.handlerThe entry point for the function
memory_size128Amount of memory allocated to the function in MB (128MB)
timeout30Maximum execution time allowed for the function in seconds
  1. Send email via SES
const data: MessageData = JSON.parse(sqsData.body);
const params: SendEmailCommandInput = {
    Destination: {
        ToAddresses: [data.sender],
    },
    Message: {
        Body: {
            Text: { Data: data.text },
        },
        Subject: { Data: data.subject },
    },
    Source: env.sourceEmail,
};
const command = new SendEmailCommand(params);
await ses.send(command);
  1. Save response to S3
await s3.send(
    new PutObjectCommand({
        Bucket: env.bucketName,
        Key: `generated-email/${content.key}.json`,
        Body: content.data,
        ContentType: 'application/json'
    })
);

Full source code

import { SESClient, SendEmailCommand, SendEmailCommandInput } from "@aws-sdk/client-ses";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { Handler, SQSEvent } from 'aws-lambda';
import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

require('dotenv').config();

type MessageData = {
    messageId: string;
    sender: string;
    subject: string;
    text: string;
};

type S3Data = {
    key: string;
    data: string;
};

const env = {
    sourceEmail: process.env.SOURCE_EMAIL || "",
    bucketName: process.env.BUCKET_NAME || ""
}

const ses = new SESClient();
const s3 = new S3Client();

export const handler: Handler = async (event: SQSEvent): Promise<void> => {
    try {
        const content = await sendEmailToCustomer(event);
        if (content) {
            await saveResponseToS3(content);
        }
    }
    catch (error) {
        logger.error(error as string);
    }
};

const sendEmailToCustomer = async (event: SQSEvent): Promise<S3Data | null> => {
    // logger.info('Sending email to customer', event.Records.toString());
    const sqsData = event.Records[0];
    // logger.info('Body', sqsData.body);
    if (sqsData) {
        const data: MessageData = JSON.parse(sqsData.body);
        const params: SendEmailCommandInput = {
            Destination: {
                ToAddresses: [data.sender],
            },
            Message: {
                Body: {
                    Text: { Data: data.text },
                },
                Subject: { Data: data.subject },
            },
            Source: env.sourceEmail,
        };
        const command = new SendEmailCommand(params);
        await ses.send(command);
        return { key: data.messageId, data: JSON.stringify(params) };
    }
    return null;
}

const saveResponseToS3 = async (content: S3Data) => {
    // logger.info('Saving response to S3', content);
    await s3.send(
        new PutObjectCommand({
            Bucket: env.bucketName,
            Key: `generated-email/${content.key}.json`,
            Body: content.data,
            ContentType: 'application/json'
        })
    );
}