• 281-717-6671
  • hello@quellhorst.com

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

What you need

  • AWS Account
  • Some Knowledge of Javascript

What We’ll Setup

  • AWS SES
  • AWS Lambda
  • API Gateway

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.

Setting up Claudia.js

1
2
3
4
mkdir email
cd email
npm init
npm install claudia -g

Enter your AWS Config to ~/.aws/credentials

1
2
3
[claudia]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

1
2
3
4
5
6
7
8
9
10
<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

1
claudia create --region us-east-1

If you need to update your deployed code run

1
claudia update



Featured Posts