AWS: API, Dynamo and Lambda

Overview

Mirroring our previous efforts in Express, today, we will be wiring up a completely serverless, let fully functional, CRUD-Enabled API.

Class Outline

Learning Objectives

Students will be able to

Describe and Define

Execute

Notes

Creating a serverless API: Checklist

Creating a Dynamo DB Table at AWS

  1. Open the DynamoDB Dashboard
  2. Choose Create Table
  3. Name your table
  4. Choose a field name to use as primary key
    • Generally, “id”, and you’ll need to supply this when you add records

Working with Dynamo from Node

When writing code that connects to a Dynamo Database, you’ll need to know your AWS credentials and install dynamoose as a dependency

https://dynamoosejs.com/getting_started/Introduction

Create a Schema with Dynamoose

This is just like Mongoose!

'use strict';

const dynamoose = require('dynamoose');

const friendsSchema = new dynamoose.Schema({
  'id': String,
  'name': String,
  'phone': String,
});

module.exports = dynamoose.model('friends', friendsSchema);

Write your Lambda Function (or any JS) to use your schema…

Be sure your Lambda function has full permissions for API Gateway, DynamoDB, and Cloudwatch

The actul schema and CRUD ops are very similar Mongoose and MongoDB

const contentModel = require('./friends.schema.js');

async function findRecord(id) {
  const content = await contentModel.query("id").eq(id).exec();
  console.log(content[0]);
}

async function saveRecord(name, phone) {
  const id = uuid();
  const record = new contentModel({ id, name, phone });
  const data = await record.save();
  console.log(data);
}

Create API Endpoints

  1. At API Gateway, create a new HTTP API
  2. Identify “Integrations” which is one or more of your Lambda functions (above)
  3. Once created, define a route endpoint for each REST method
  4. Connect each endpoint to the correct lambda

As your routes are invoked by users, those lambda’s will fire, with the event receiving any POST or QUERY data