Serverless E-mail Forwarder using Lambda

Architecture Diagram

By now I’m sure you’re sick of hearing about serverless this and serverless that. Well, here’s some more.

I recently came across a nifty GitHub project to use AWS Lambda to forward e-mails for a domain to a single email address (arithmetric/aws-lambda-ses-forwarder).

I’ve had an old domain lying around that I didn’t use for email, so I thought I’d set it up and see how it went, I’ve written up some instructions below:

Setup Lambda Function

My config looks a little bit like this:

var defaultConfig = {
  fromEmail: "noreply@mail.skunkw0rks.io",
  subjectPrefix: "",
  emailBucket: "bucket_name",
  emailKeyPrefix: "incoming/",
  forwardMapping: {
    "@skunkw0rks.io": [
      "different_email@example.com"
    ]
  }
};

It’ll forward all emails from @skunkworks.io to the super legit different_email@example.com.

{
  "Version": "2012-10-17",
  "Statement": [
     {
        "Effect": "Allow",
        "Action": [
           "logs:CreateLogGroup",
           "logs:CreateLogStream",
           "logs:PutLogEvents"
        ],
        "Resource": "arn:aws:logs:*:*:*"
     },
     {
        "Effect": "Allow",
        "Action": "ses:SendRawEmail",
        "Resource": "*"
     },
     {
        "Effect": "Allow",
        "Action": [
           "s3:GetObject",
           "s3:PutObject"
        ],
        "Resource": "arn:aws:s3:::bucket_name/*"
     }
  ]
}

Configure SES

Configure S3

{
  "Version": "2012-10-17",
  "Statement": [
     {
        "Sid": "GiveSESPermissionToWriteEmail",
        "Effect": "Allow",
        "Principal": {
           "Service": "ses.amazonaws.com"
        },
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::S3-BUCKET-NAME/*",
        "Condition": {
           "StringEquals": {
              "aws:Referer": "AWS-ACCOUNT-ID"
           }
        }
     }
  ]
}

Testing

This should be everything you need to get up and running, send a test email to the domain and check for Invocation Errors in Lambda.

comments powered by Disqus