Serverless feedback form using S3, Lambda, DynamoDB, SES, and API Gateway

Serverless Forms using Lambda

The walkthrough below provides the front end code for a serverless contact or feedback form as well as the necessary steps in AWS to setup an API Gateway, IAM roles, Lambda function, and a DynamoDB Table. Code for both the front and back end are on github:
https://github.com/kyle138/feedbackform.

The code in this repo is split into two folders, [S3] and [lambda].
[S3]:
All files and folders within the S3 folder will need to go within an S3 bucket configured for web hosting. This serves the front end HTML as well as the clientside javascript files required for collecting the form information and submitting the data to the API Gateway.
[Lambda]:
The lambda folder contains a single lambdafunc.js file. The contents of this file will be copied into your lambda function from within the AWS console.

SES

  1. Verify a New Email Address (email address you control where feedback will be sent)
  2. Make note of the verified email addresses for use in the Role in the Lambda section.

DynamoDB

  1. Create Table
  2. Name it ‘feedback’
  3. PrimaryKey ‘datetime’ (string)
  4. Make note of the ARN for the role in the next section.

Lambda

  1. Create Lambda function (use blueprint ‘microservice-http-endpoint’)
  2. Name the function ‘feedback’, make sure runtime is node.js
  3. Leave the code as-is for now, we’ll update that later
  4. Role: Create a new role based off of ‘*Basic with DynamoDB’
    1. Name the role ‘lambda_dynamodb_feedback’
    2. Edit the policy with the contents of lambda/lambda_dynamodb_role.json
      1. Edit the json resource with the ARN of your dynamoDB
      2. Edit the json conditions with the verified email address(es)
  5. Set API endpoint type to API Gateway
  6. Select the feedback gateway created in the API Gateway section
  7. Select POST for Method
  8. Select Open for Security
  9. Review and [Create Function]
  10. Now replace the lambda code with the contents of the lambda/lambdafunc.js > [Save and Test]
  11. Choose sample event template ‘Hello World’ and use the following:
  12. 
          {
           "name": "Test Name",
           "email": "email@test.com",
           "subject": "Test Subject",
           "message": "This is the test message."
          }
              
  13. You should receive “True”, also the test items should be in the DynamoDB table and in the CloudWatch Logs and you should receive the test via email.

API Gateway

  1. Create API gateway
  2. Select Resource / > [Create Resource]
  3. Name the resource ‘feedback’ with the same resource path (autopopulated)
  4. Select the new feedback resource > [Create Method] > Select ‘POST’
  5. Select ‘Lambda Function’ for Integration type and choose a Lambda Region
    1. Make note of the region as the lambda function and dynamoDB will use the same
  6. Select the Lambda Function field and begin typing ‘feedback’, your function created below should be offered as a suggestion.
  7. [Enable CORS] (keep defaults and [Enable])
  8. [Deploy API]
    1. Select New Stage and name it ‘prod’
    2. Make note of the Invoke URL for use in the front-end feedback.js file that is placed in S3.
  9. Select Resources > POST > TEST. Use the sample event from the previous step as the Request Body and click [Test]

S3

  1. Create an S3 bucket
  2. Name it exactly the same as the domain you will be directing to it (eg: feedback.mydomain.com)
  3. Configure it to serve as a website according to the following:
    1. http://docs.aws.amazon.com/gettingstarted/latest/swh/website-hosting-intro.html
  4. Place the API Gateway URL from the previous step in the feedback.js
  5. Upload S3 files to new bucket.

Credits

By no means did I come up with all of this by myself. I simply integrated the ideas from the three links found below::

Bootstrap Jquery form source:
http://myprogrammingblog.com/2013/08/27/how-to-make-a-contact-form-with-bootstrap-3-jqueryphphtml5jqbootstrapvalidation/

S3 Static Website Hosting:
http://docs.aws.amazon.com/gettingstarted/latest/swh/website-hosting-intro.html

Lambda/DynamoDB/API Gateway walk through:
http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html