Function To Table Integration

Use Case

Often times we need a way to capture and store the values of a submitted web form. Newsletter signup or contact forms are perfect examples.

In this guide we create a website with a simple form that stores a submitted user’s name and email address into a DynamoDB table.

Prerequisites

How To

Stack Setup

  1. Create a new Stackery stack. Select a Git provider. 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 (Mac) / Backspace (Windows) 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 AWS = require('aws-sdk')
const dynamodb = new AWS.DynamoDB();

module.exports = function handler(message, context, callback) {
  const signup = JSON.parse(message.body);
  const ports = JSON.parse(process.env.STACKERY_PORTS)

  // Copy the specific values we want to save into the Table record
  const params = {
    Item: {
      "email": {
        S: signup.email
      }, 
      "name": {
        S: signup.name
      }, 
    },
    ReturnConsumedCapacity: "TOTAL", 
    TableName: ports[0][0].tableName
  }
  // Put the new record into the Table  
  return dynamodb.putItem(params).promise()
    // Send a 204 No Content response back to the client
    .then(() => { callback(null, {
      statusCode: 204,
      headers: {},
      body: JSON.stringify({}) 
    })});
}

The above code inserts the signup into the Table then responds with a 204 No Content HTTP response.

Deploy

Deploy via Stackery CLI

Deploy via Stackery Web UI:

  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.