Startup Tutorial – Signup Emails with AWS Lambda and DynamoDB

The blog explains how to setup signup email backend with AWS Lambda microservice and Amazon DynamoDB. It’s a good tutorial for amazon web services developer who want to get started with microservices & programming  amazon web services.

Emails processed by the AWS Lambda function will be stored in DynamoDB.

Perfect for startups. How to invoke your Lambda via rest is mentioned at the end of the article. You can create your signup landing page with this use case and get started for free to begin with. If you have not setup AWS Free Tier, please read below article.

*Note: This is to help you get started and doesn’t necessarily reflect the best way of doing signup emails 

What is AWS Lambda and DynamoDB?

AWS Lambda is as microservice solution by Amazon. You literally upload a node.js file (or a node.js or Java 8 binary) and you let it run in the cloud. A great thing about Amazon Lambda is that it’s very cost efficient. You only pay for how long AWS Lambda runs for instead of constantly paying for an EC2 instance.

Amazon DynamoDB is a NoSQL database service provided by Amazon. The cloud service platform is easy to setup and start playing with.

Setup Amazon DynamoDB

This part is easy. Inside your AWS Console (after signing up to AWS) select Services from the top and find DynamoDB. For region you can just choose US East N.Virginia as it’s the cheapest. This link takes you there directly.

  • Click create table.
  • Call your table whatever you want, a reasonable name would be SignupsTable.

Now here comes some learning.

  • Amazon DynamoDB has a primary unique key.
  • This unique key can just be a hash, or a hash with a range.
  • A hash with a range consists of 2 values, one value to hash, and the other to be “the range”.
  • The range attribute can be queried upon.
  • We probably want to query our emails by date added and probably by email name..

Since we can’t query by a hash, we need to select hash and range for the primary index. Just put something like type for the hash value. This will be a constant value for all emails added. Usually the hash is a unique identifier for an entry, but for us our table is simple so we don’t really need to make use of this functionality. More on this later.

For the range value write email and make it a string. This means we can query our database by emails.

Programming Amazon Web Services : DynamoDB Create Table

Programming Amazon Web Services : DynamoDB Create Table

Now click continue and we’ll add another index, date. This way we can filter our results by date they were added.

  • Select Local Secondary Index for the index type and type date for the Index Range Key (this means that this index is tied to this hash).
  • Click Add Index To Table and continue.
Programming Amazon Web Services : Add Index to Amazon DynamoDB

Programming Amazon Web Services : Add Index to Amazon DynamoDB

Continue making everything with the defaults and we’re done. Wooo!

To test this out go to your DynamoDB table by selecting it from here and clicking explore. While the table is Creating we’ll setup our Lambda function.

Setup AWS Lambda MicroService

Create a new Lambda microservice by selecting AWS Lambda from the Services tab in the console, or go here (link to US East N.Virginia again).

Click Create a AWS Lambda Function and available select simple-mobile-backend. If you can’t find it, it’s okay. I’ll paste the relevant code here, but it’d be useful for you to have a look of some dummy AWS Lambda functions.

For the function name type anything, SignupEmailLambda is a sensible name. Feel free to type any description.

Create a new role by selecting Basic with DynamoDB from the role table and just following that through with the defaults. This will allow your AWS Lambda function to have permissions to call DynamoDB. Once this is created and you’re taken back to the AWS Lambda creating screen, select the role you just created (the default name is something like lambda_dynamo).

For the timeout select 5 seconds (this is the maximum time a Lambda runs before being terminated). 5 seconds should be enough to write to a DynamoDB database assuming no network stuff goes horribly wrong.

Leave everything else as defaults and continue. Click Create Function on the following page.

AWS Lambda MicroService Function

AWS Lambda MicroService Function

Lambda learning time:

  • AWS Lambda microservices are triggered in response to something. This could be an http request or a stream event. For this example we’ll only use http request responses.
  • Lambdas take an event (which for http requests is the data) and a context (which for http submits an http response).

Code and Testing Lambda Function

Now lets code up our Lambda. You should be taken to the AWS Lambda coding page. Lets configure the sample event by clicking Configure Sample Event from the actions drop down next to test near the top of the page.

Programming Amazon Web Services : AWS Lambda Configure Sample Event

Programming Amazon Web Services : AWS Lambda Configure Sample Event


Lets setup a simple JSON message with an email with just an email key and a dummy email value like so (you can change the value to be whatever you want). This is the simulated JSON payload for your AWS Lambda function microservice.

Now click Submit. It’s okay things might fail when it tries to run the Lambda. No worries for now.

The JSON payload will be attached to the default event object passed into the Lambda function.

Copy the code below to your AWS Lambda function section where you enter code..

So basically, you initialize a Lambda with the following line. AWS then calls this exports.handler function with the event and the context from which your Lambda was triggered.

To put the item in the table we need an object with the TableName set to our table name and and Item which contains the object we wanna store.

Note: For simplicity I left out error checking such as examining if the email is valid, If it already exists in the database, whether the dynamo.putItem call returns an error etc.

Lets Run This!

Now simply click Save and Test. Assuming your database is up this should work. If not your database could still be setting up . Check your database status here (assuming it’s on US East N.Virginia).

Click on your SignupEmailTable and click Explore Table up the top.

Because we configured a range key for the date and email, you could do queries on your table (once it gets larger). Just click Query, set the type as email and you can search emails and dates by ascending or descending. Leave the range key value blank if you want to search all.


If you want to set this up as an http endpoint follow this tutorial by AWS. If you encounter “Cross Origin Resource Header” errors follow this link (this will happen if you try access the lambda function from your browser)

Programming amazon web services is fun. You learn a lot about cloud computing. Hopefully this will get you on a good start with AWS Lambda and Amazon DynamoDB. You can later integrate sending emails from the AWS Lambda to MailChimp, AWS SES or another email sending service.

For getting started with Amazon Web Services, I recommend below article:

What do you think? Will you create your signup landing page with above use case. Let me know in comment section.

If you like my article please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to with your email.