Email Form With AWS Lambda

What is AWS Lambda / Serverless

Lambda allows you to execute a block of code without provisioning servers and only pay for the execution time. There is no charge when the code isn’t running. Compute capacity scales automatically. From a few requests per day to thousands per second.

Lambda is better for low traffic volume because its cheaper and easier to manage than EC2 server instances. You are only paying per call, not
For higher traffic volume Lambda scales up automatically. Autoscaling with EC2 is possible, but not as easy.

For a detailed reference see the AWS Lambda Developer Guide.

Why Process Contact Form Data with Lambda

  • No servers to manage
  • Easy to deploy
  • Sub Second Billing
  • No monthly fees
  • Never pay for idle servers
  • Infinitely and Continuous Scaling
  • Allows the rest of your basic site to be static

Other Uses of AWS Lambda

  • Process real-time data
  • Mobile backends and other Backend Services
  • Stored procedures for AWS DynamoDB
  • Process uploads to S3 buckets
  • Workflows with AWS Step Functions

Downsides of AWS Lambda

  • Yet another thing to manage
  • Not very common today
  • Development Environment not as good

What you need

  • AWS Account
  • Some Knowledge of Javascript

What We'll Setup

  • AWS SES
  • AWS Lambda
  • API Gateway

SES

SES

Create and verify an email address to use for receiving email.

  1. Go to SES from AWS Console
  2. Click Email Addresses
  3. Click “Verify a New Email Address”
  4. Type in the email address you want to send from.
  5. Click “Verify This Email Address”
  6. Check your email and click the link in the Verification Request email.

AWS Lambda and Claudia.JS

Claudia.js is a nice AWS specific abstraction layer for API Gateway and AWS Lambda. It will automatically setup API Gateway and Lambda for your node.js application.

mkdir email
cd email
npm init
npm install claudia -g

Enter your AWS Config to ~/.aws/credentials

[claudia]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
var ApiBuilder = require('claudia-api-builder'),
    AWS = require('aws-sdk'),
    api = new ApiBuilder(),
module.exports = api;
var ses = new aws.SES();
api.post('/email', function(request) {
  'use strict';
  
  var message = "message: " + request.post.message;
  var email = {
    Destination: { ToAddresses: ['your@email.com'] },
    Message: { 
      Body: { 
        Html: { Data: message },
        Text: { Data: message }
      },
      Subject: { Data: 'Form Submission from YourWebsite.com' }
      },
      Source: 'your@email.com'
    };
    return SES.sendEmail(email).promise()
      .then(function (data) {
        // On Success Redirect to Homepage
        return new api.ApiResponse('OK', {'X-Version': '202', 'Content-Type': 'text/plain', 'Location': 'http://homepage.com/thank-you'}, 302);
      })
      .catch(function (err) {
        // Console.log messages are visible in AWS Cloudwatch
        console.log('Error sending mail: ' + err)
        return { 'status': 'ERROR' }
      })
});

HTML Form

<html>
<body>
  <form action="https://rand.execute-api.us-east-1.amazonaws.com/latest/form" method="post">
    <div class="form-group">
      <label for="contacts-message">Message</label>
      <textarea class="form-control" rows="5" id="contact-message" name="message"></textarea>
    </div>
  </form>
</body>
</html>

Deploy your application to AWS

claudia create --region us-east-1

If you need to update your deployed code run

claudia update