openec2 Article Description

Part 6

Configure an SES/S3 Bucket for forwarding email

We will now create a Lambda function, using the previous work, that our SES Receiving rule can use for processing emails in the bucket.

The first example uses Node.js.20x. It will only notify you of an email that has arrived. You can download from the bucket via your Amazon AWS login, or an app like CyberDuck or Command One. (You supply the app your S3 keys, not the SMTP keys, but the S3 keys you made earlier.)

The lesson after this will configure your SES Receiving rule to add the Lambda function, which you can then test by sending an email to your sandbox address, admin@mydomain.com from your verified personal address. Both addresses must be verified while in sandbox mode.

Our lesson after these will show the older Node.js.16x. I don’t know why no one has created the same functionality for 20x. There is some GitHub code around, but it has never worked. The 16x code lets us verify security, then forwards copies to anyone for any address names you choose. Amazon AWS has said this code will be removed, but so far it has not. I may have a lesson that shows a shell script for forwarding the email from .20x as an attachment which is at least something more practical.


Go to the Lambda console (you can search on it), select your email region (e.g. Oregon), and click on Functions, Create function.

Copy and paste this code into the Lambda function – firstly change the values for your own bucket name, domain, and personal email address.

Don’t use any of the template code that is in a new function. Overwrite it all with your code.

// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
const ses = new SESClient({ region: "us-west-2" });

export const handler = async(event) => {
  const command = new SendEmailCommand({
    Destination: {
      ToAddresses: ["YOUR_PERSONAL_EMAIL_ADDRESS"],
    },
    Message: {
      Body: {
        Text: { Data: "An e-mail for YOUR_DOMAIN_NAME has arrived in YOUR_EMAIL_BUCKET. Please login to the S3 bucket to download." },
      },

      Subject: { Data: "New SES YOUR_DOMAIN_NAME e-mail" },
    },
    Source: "YOUR_PERSONAL_EMAIL_ADDRESS",
  });

  try {
    let response = await ses.send(command);
    // process data.
    return response;
  }
  catch (error) {
    // error handling.
  }
  finally {
    // finally.
  }
};

Make sure us-west-2 is the region you use, otherwise, edit it.

Examples:

YOUR_PERSONAL_EMAIL_ADDRESS: me@gmail.com

An e-mail for mydomain.com has arrived in mydomain.com.inbox. Please login to the S3 bucket to download.

New SES mydomain.com e-mail

Next we slightly change the configuration.

Next we add the preceding work to create an email rule in SES.