Technical Articles & Tutorials

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: ['[email protected]'] },
Message: { 
  Body: { 
    Html: { Data: message },
    Text: { Data: message }
  },
  Subject: { Data: 'Form Submission from YourWebsite.com' }
  },
  Source: '[email protected]'
};
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

About

Why fear those copying you, if you are doing good they will do the same to the world.

Archives

  1. AI & Automation
  2. AI Filtering for Web Content
  3. Web Fundamentals & Infrastructure
  4. Reclaiming Connection: Decentralized Social Networks
  5. Web Economics & Discovery
  6. The Broken Discovery Machine
  7. Evolution of Web Links
  8. Code & Frameworks
  9. Breaking the Tech Debt Avoidance Loop
  10. Evolution of Scaling & High Availability
  11. Evolution of Configuration & Environment
  12. Evolution of API Support
  13. Evolution of Browser & Client Support
  14. Evolution of Deployment & DevOps
  15. Evolution of Real-time Capabilities
  16. The Visual Basic Gap in Web Development
  17. Evolution of Testing & Monitoring
  18. Evolution of Internationalization & Localization
  19. Evolution of Form Processing
  20. Evolution of Security
  21. Evolution of Caching
  22. Evolution of Data Management
  23. Evolution of Response Generation
  24. Evolution of Request Routing & Handling
  25. Evolution of Session & State Management
  26. Web Framework Responsibilities
  27. Evolution of Internet Clients
  28. Evolution of Web Deployment
  29. The Missing Architectural Layer in Web Development
  30. Development Velocity Gap: WordPress vs. Modern Frameworks
  31. Data & Storage
  32. Evolution of Web Data Storage
  33. Information Management
  34. Manual Bookkeeping with a Columnar Pad
  35. Managing Tasks Effectively: A Complete System
  36. Managing Appointments: Designing a Calendar System
  37. Building a Personal Knowledge Base
  38. Contact Management in the Digital Age
  39. Project Management for Individuals
  40. The Art of Response: Communicating with Purpose
  41. Strategic Deferral: Purposeful Postponement
  42. The Art of Delegation: Amplifying Impact
  43. Taking Action: Guide to Decisive Execution
  44. The Art of Deletion: Digital Decluttering
  45. Digital Filing: A Clutter-Free Life
  46. Managing Incoming Information
  47. Cloud & Infrastructure
  48. Moving from Cloud to Self-Hosted Infrastructure
  49. AWS Lightsail versus EC2
  50. WordPress on AWS Lightsail
  51. Migrating from Heroku to Dokku
  52. Storage & Media
  53. Vultr Object Storage on Django Wagtail
  54. Live Video Streaming with Nginx
  55. YI 4k Live Streaming
  56. Tools & Connectivity
  57. Multi Connection VPN
  58. Email Forms with AWS Lambda
  59. Static Sites with Hexo

Optimize Your Website!

Is your WordPress site running slowly? I offer a comprehensive service that includes needs assessments and performance optimizations. Get your site running at its best!

Check Out My Fiverr Gig!

Elsewhere

  1. YouTube
  2. Twitter
  3. GitHub