Function To Table Integration

Use Case

Often times we need a simple way to capture the values of a submitted form. Newsletter signup or contact forms are perfect examples. Imagine we have a website with a simple form that sends a request to an API service with the user’s name and email address. For our simple needs we just want to dump the signups into a NoSQL table. Later on when we are ready to publish a newsletter we can query the table for all the records to send the emails. This guide covers how to create a simple API service to ingest the newsletter signups.

Prerequisites

How To

Stack Setup

  1. Create a new Stackery stack. Select a Git provider (AWS is easiest if you just want to get going quickly). In the next screen name the stack “newsletter-sign-up-tutorial” (or another name of your choice).
  2. Delete all the nodes in the new canvas by selecting them and pressing the Delete key on your keyboard.
  3. Add a new Rest Api node.
    1. Name the node Newsletter Signup, set the Method to POST, and set the Endpoint to /signup.
  4. Add a new Function node.
    1. Name the node Signup Handler, but leave all the settings as they are.
    2. Drag a wire from the Rest Api node to the Function node.
  5. Add a new Table node.
    1. Name the node Signups, and set the Hash Key to email.
    2. Drag a wire from the Function node to the Table node.

Signup Handler Function

This stack will invoke the function once each time the /signup endpoint is hit with a POST request. The API expects the signup details to be passed in the body of the request:

{
  "email": "samantha@gmail.com",
  "name": "Samantha Gladstone"
}

Let’s add some code so the Function can save new signups to the database. Open the Function node and paste the following code in:

const stackery = require('stackery')

module.exports = function handler(message) {
  const signup = JSON.parse(message.body);

  // Copy the specific values we want to save into the Table record
  const record = {email: signup.email, name: signup.name};

  // Put the new record into the Table  
  return stackery.output({action: 'put', record})
    // Respond with an empty object to send a 204 No Content response back to the client
    .then(() => ({}))
}

The above code inserts the signup into the Table then responds with a 204 No Content HTTP response. The stackery module is packaged with all Node.js functions automatically. The stackery.output() function sends messages to nodes connected to the output port of the Function node. Here, we output a message to put a record into the connected Table node. You can view all the possible the Table message formats for performing queries in our Table docs.

Deploy

  1. Click the Save button in the left sidebar of the Stackery dashboard.
  2. Click the Prepare Deployment button just below the Save button.
  3. Wait for the deployment to be prepared. This usually takes about one minute.
  4. Click the Deploy button. This will open the AWS CloudFormation. Click the “Execute” button in the top right to deploy the new stack.
  5. Back in the Stackery dashboard, wait for the deployment to be deployed. The deployment in the left sidebar will move from the Prepared Deployment section to the Current Deployment section once it has been fully deployed. The initial deployment takes around 10-15 minutes. Subsequent deployments take about a minute.
  6. Get the domain of the API. In the Stackery dashboard, select the current deployment if it is not already selected. In the canvas area on the right, select the Rest Api node. In the properties panel on the right find and copy the Domain Name.

Test

Now that the api is running let’s test it out! You can run the following cURL command to hit the endpoint:

$ curl https://<your api domain>/signup -d '{"email": "samantha@gmail.com", "name": "Samantha Gladstone"}'

You can now log into the AWS DynamoDB Console to find your table and list the records inside it!

Try Stackery For Free

Gain control and visibility of your serverless operations from architecture design to application deployment and infrastructure monitoring.