AWS Workmail, AWS SES, AWS S3 and AWS Lambda Function to automate
Combine Aws Workmail, AWS SES and AWS Lambda Function
I have done a project using AWS Workmail, AWS SES and AWS Lambda Function to automate a task which requires manually download a file from attachment whenever receiving an email. The workflow is: emails will be sent to AWS Workmail email address. AWS SES email receiving rule set can place all those emails to a linked S3 bucket. Once we receive the workmail in the linked S3 bucket, that linked bucket with this lambda function will be triggered to extract the attachment from the email and place it to another separated s3 bucket according to the sender-name/email-subject/year/month/day as S3 folder structure
Set up AWS Workmail
- Go to Workmail service on AWS and click add organization as shown below. You can name it as anything. My test one has named it as nine-data-testing.
- Click inside the organisation. Create a user. You can name it data so our email address will be email@example.com. My test one is cqiu as shown below.
Set up AWS SES
- Go to AWS SES service. Under Email Receiving section in the left menu, click Rule Sets and then click Create Rule.
Set up AWS S3
- Create a bucket name “datascience-email-sources” with below permission policy :
Back to AWS Workmail to add S3 rule
- Add Recipient using the aws workmail we just created and under Actions section, add an S3 rule with bucket datascience-email-sources we just created.
Set up AWS Lamdbda function
- Create lambda function workemail-attachment-extract with below triggers and permission to resources :
The S3 trigger setting is as below. Monitoring all objectCreated Events.
The policies for the lambda role I set up in IAM are below for your reference.
Below is the lambda function code. I have also open source this project in my github https://github.com/chrisqiqiu/aws-lambda-function-nine/tree/master/workmail-ses-s3-attachment
Create the zip file based on the lamda function :
pip install -r requirements.txt -t ./
chmod 777 lambda_function.py
zip -r9 ../lambda_function.zip .
- Upload the zip file
lambda_function.zipas shown below
- Please set the Timeout to 5 minutes instead of 3 seconds :