Mirroring our previous efforts in Express, today, we will be wiring up a completely serverless, let fully functional, CRUD-Enabled API.
- AWS API Gateway
- How to trigger Lambda Functions in response to an API request
- The differences between HTTP and REST APIs at AWS
- The differences between DynamoDB and Mongo
- Creation of a DynamoDB Table
- Creation of a Lambda function that can operate on a DynamoDB Table
- Usage of Dynamoose in a NodeJS Lambda Function
Creating a serverless API: Checklist
- IAM User role with access to Lambda and DynamoDB Full Access
- Dynamo DB Table Created
- Lambda function(s) that use Dynamoose to attach to the table
- Created with the correct IAM Role (Step 1)
- API Endpoints that all the appropriate functions for each action type
- Open the DynamoDB Dashboard
- Choose
Create Table
- Name your table
- Choose a field name to use as primary key
- Generally, "id", and you'll need to supply this when you add records
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
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);
Again, this is very similar to Mongoose and Mongo
const contentModel = require('./curriculum.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);
}
- At API Gateway, create a new HTTP API
- Once created, define a route endpoint for each REST method
- Connect each endpoint to a lambda
As your routes are invoked by users, those lambda's will fire, with the event
receiving any POST or QUERY data