Creating a Contact form using Amazon Web Services

I recently wanted to add a simple contact form to my static website. When users submit the form, I wanted to receive their message via email. Amazon S3 hosts the site. I thought of three options to handle the dynamic behavior of parsing the form and sending me an email:

  1. Use a VPS to parse the form and send the email.
  2. Use a third party service.
  3. Use Amazon API Gateway and Amazon Lambda

Option 1 seemed like overkill. Option 2 and 3 both appealed to me. I eventually settled on using Amazon’s services due to their low cost. The remainder of this post contains my notes about setting up the services. See the Amazon documentation for a more in depth look at each service.

Lambda Function

Create a new Lambda function with a NodeJs runtime. Use code like the following, modified from lithostec:

'use strict';
var AWS = require('aws-sdk');
var ses = new AWS.SES({apiVersion: '2010-12-01'});

function validateEmail(email) {
  var tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\.?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$/;
  if (!email) return false;
  if(email.length>254) return false;
  var valid = tester.test(email);
  if(!valid) return false;
  var parts = email.split("@");
  if(parts[0].length>64) return false;
  var domainParts = parts[1].split(".");
  if(domainParts.some(function(part) { return part.length>63; })) return false;
  return true;

exports.handler = (event, context, callback) => {
  console.log('Received event:', JSON.stringify(event, null, 2));
  console.log('Received context:', JSON.stringify(context, null, 2));
  if (! {'We are sorry, an error has occurred. Please try again later.'); return; }
  if (! {'Email is required.'); return; }
  if (! || === '') {'You must provide a message.'); return; }
  if (! || === '') {'You must provide your name.'); return; }
  var email = unescape(;
  if (!validateEmail(email)) {'You must provide a valid email address.'); return; }
  var messageParts = [];
  var replyTo = + " <" + email + ">";
  messageParts.push("Message: " +;
  var subject = "Contact Form Email - " + email;
  var params = {
    Destination: { ToAddresses: [ 'Your Name ' ] },
    Message: {
      Body: { Text: { Data: messageParts.join("\r\n"), Charset: 'UTF-8' } },
      Subject: { Data: subject, Charset: 'UTF-8' }
    Source: "Contact Form ",
    ReplyToAddresses: [ replyTo ]
  ses.sendEmail(params, function(err, data) {
    if (err) {
      console.log(err, err.stack);;
    } else {
      context.succeed('Thank you for contacting us!');

Your form’s data will be in the JSON. This assumes you have Amazon SES setup.

Amazon API Gateway

Create a new API or use an existing one. Create a resource to handle the form submission. I named my resource “/submit-contact-form”. Add a POST method. Select the new method and navigate to the Integration Request section.

Set the Integration Type to Lambda Function and set Lambda Function to your previously created function.

Expand Body Mapping Templates. Select “When there are no templates defined”. Add a Content-Type of “application/x-www-form-urlencoded”. Set the template to the following code courtesy of Marcus’s Stackoverflow answer:

    "data": {
        #foreach( $token in $input.path('$').split('&') )
            #set( $keyVal = $token.split('=') )
            #set( $keyValSize = $keyVal.size() )
            #if( $keyValSize >= 1 )
                #set( $key = $util.urlDecode($keyVal[0]) )
                #if( $keyValSize >= 2 )
                    #set( $val = $util.urlDecode($keyVal[1]) )
                    #set( $val = '' )
                "$key": "$val"#if($foreach.hasNext),#end

Enable CORS unless you are using your own custom domain name for your API. Deploy the API.

Contact Form

For the HTML side, I had the Submit button of the contact form execute an Ajax request to the API with a function similar to this:

function submitContactForm() {
	type: "POST",
	url: '',
	data: $('#contact-form').serialize(),
	success: function(message) {
	    if (message.errorMessage) {
	    } else {
	error: function(message) {
	dataType: 'json',
	crossDomain: true
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora