DEV Community

loading...

blog: Building Serverless Apps using the Serverless Stack Framework

Nabeel Valley
My name is Nabeel Valley, born in 1995 - Pretoria, South Africa I'm passionate about solving challenging problems in beautiful ways
Originally published at nabeelvalley.netlify.app on ・13 min read

Prior to doing any of the below you will require your ~/.aws/credentials file to be configured with the credentials for your AWS account

Serverless Stack Framework

SST Framework is a framework built on top of CDK for working with Lambdas and other CDK constructs

It provides easy CDK setups and a streamlined debug and deploy process and even has integration with the VSCode debugger to debug stacks on AWS

Init Project

To init a new project use the following command:

npx create-serverless-stack@latest my-sst-app --language typescript

Enter fullscreen mode Exit fullscreen mode

Which will create a Serverless Stack applocation using TypeScript

Run the App

You can run the created project in using the config defined in the sst.json file:

{
  "name": "my-sst-app",
  "stage": "dev",
  "region": "us-east-1",
  "lint": true,
  "typeCheck": true
}

Enter fullscreen mode Exit fullscreen mode

Using the following commands command will build then deploy a dev stack and allow you to interact with it via AWS/browser/Postman/etc.

npm run start

Enter fullscreen mode Exit fullscreen mode

Additionally, running using the above command will also start the application with hot reloading enabled so when you save files the corresponding AWS resources will be redeployed so you can continue testing

The Files

The application is structured like a relatively normal Lambda/CDK app with lib which contains the following CDK code:

Stack

lib/index.ts

import MyStack from "./MyStack";
import * as sst from "@serverless-stack/resources";

export default function main(app: sst.App): void {
  // Set default runtime for all functions
  app.setDefaultFunctionProps({
  runtime: "nodejs12.x"
  });

  new MyStack(app, "my-stack");

  // Add more stacks
}

Enter fullscreen mode Exit fullscreen mode

lib/MyStack.ts

import * as sst from "@serverless-stack/resources";

export default class MyStack extends sst.Stack {
  constructor(scope: sst.App, id: string, props?: sst.StackProps) {
  super(scope, id, props);

  // Create the HTTP API
  const api = new sst.Api(this, "Api", {
    routes: {
    "GET /": "src/lambda.handler" },
  });

  // Show API endpoint in output
  this.addOutputs({
    "ApiEndpoint": api.httpApi.apiEndpoint,
  });
  }
}

Enter fullscreen mode Exit fullscreen mode

And src which contains the lambda code:

src/lambda.ts

import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  return {
  statusCode: 200,
  headers: { "Content-Type": "text/plain" },
  body: `Hello, World! Your request was received at ${event.requestContext.time}.`,
  };
};

Enter fullscreen mode Exit fullscreen mode

Add a new Endpoint

Using the defined constructs it's really easy for us to add an additional endpoint:

src/hello.ts

import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  const response = {
    data: 'Hello, World! This is another lambda but with JSON'
  }

  return {
  statusCode: 200,
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(response),
  };
};

Enter fullscreen mode Exit fullscreen mode

And then in the stack we just update the routes:

lib/MyStack.ts

const api = new sst.Api(this, "Api", {
  routes: {
  "GET /": "src/lambda.handler",
  "GET /hello": "src/hello.handler" // new endpoint handler
  },
});

Enter fullscreen mode Exit fullscreen mode

So that the full stack looks like this:

lib/MyStack.ts

import * as sst from "@serverless-stack/resources";

export default class MyStack extends sst.Stack {
  constructor(scope: sst.App, id: string, props?: sst.StackProps) {
  super(scope, id, props);

  // Create the HTTP API
  const api = new sst.Api(this, "Api", {
    routes: {
    "GET /": "src/lambda.handler",
    "GET /hello": "src/hello.handler"
    },
  });

  // Show API endpoint in output
  this.addOutputs({
    "ApiEndpoint": api.httpApi.apiEndpoint,
  });
  }
}

Enter fullscreen mode Exit fullscreen mode

VSCode Debugging

SST supports VSCode Debugging, all that's required is for you to create a .vscode/launch.json filw with the following content:

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug SST Start",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["start"],
      "port": 9229,
      "skipFiles": ["<node_internals>/**"]
    },
    {
      "name": "Debug SST Tests",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/sst",
      "args": ["test", "--runInBand", "--no-cache", "--watchAll=false"],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "env": { "CI": "true" },
      "disableOptimisticBPs": true
    }
  ]
} 

Enter fullscreen mode Exit fullscreen mode

This will then allow you to run Debug SST Start which will configure the AWS resources using the npm start command and connect the debugger to the instance so you can debug your functions locally as well as make use of the automated function deployment

Add a DB

From these docs

We can define our table using the sst.Table class:

const table = new sst.Table(this, "Notes", {
  fields: {
  userId: sst.TableFieldType.STRING,
  noteId: sst.TableFieldType.NUMBER
  },
  primaryIndex: {
  partitionKey: "userId", sortKey: "noteId"
  }
})

Enter fullscreen mode Exit fullscreen mode

Next, we can add some endpoint definitions for the functions we'll create as well as access to the table name via the environment:

const api = new sst.Api(this, "Api", {
  defaultFunctionProps: {
      timeout: 60, // increase timeout so we can debug
    environment: {
      tableName: table.dynamodbTable.tableName,
    },
  },
  routes: {
  // .. other routes
    "GET /notes": "src/notes/getAll.handler", // userId in query
    "GET /notes/{noteId}": "src/notes/get.handler", // userId in query
    "POST /notes": "src/notes/create.handler"
  },
});

Enter fullscreen mode Exit fullscreen mode

And lastly we can grant the permissions to our api to access the table

api.attachPermissions([table])

Enter fullscreen mode Exit fullscreen mode

Adding the above to the MyStack.ts file results in the following:

import * as sst from "@serverless-stack/resources";

export default class MyStack extends sst.Stack {
  constructor(scope: sst.App, id: string, props?: sst.StackProps) {
  super(scope, id, props);

  const table = new sst.Table(this, "Notes", {
    fields: {
    userId: sst.TableFieldType.STRING,
    noteId: sst.TableFieldType.STRING
    },
    primaryIndex: {
    partitionKey: "userId", sortKey: "noteId"
    }
  })

  // Create the HTTP API
  const api = new sst.Api(this, "Api", {
    defaultFunctionProps: {
    timeout: 60, // increase timeout so we can debug
    environment: {
      tableName: table.dynamodbTable.tableName,
    },
    },
    routes: {
    // .. other routes
    "GET /notes": "src/notes/getAll.handler", // userId in query
    "GET /notes/{noteId}": "src/notes/get.handler", // userId in query
    "POST /notes": "src/notes/create.handler"
    },
  });

  api.attachPermissions([table])

  // Show API endpoint in output
  this.addOutputs({
    "ApiEndpoint": api.httpApi.apiEndpoint,
  });
  }
}

Enter fullscreen mode Exit fullscreen mode

Before we go any further, we need to install some dependencies in our app, particularly uuid for generating unique id's for notes, we can install a dependency with:

npm install uuid
npm install aws-sdk

Enter fullscreen mode Exit fullscreen mode

Define Common Structures

We'll also create some general helper functions for returning responses of different types, you can view the details for their files below but these just wrap the response in a status and header as well as stringify the body

src/responses/successResponse.ts

const successResponse = <T>(item: T) => {
  return {
  statusCode: 200,
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(item),
  };
};

export default successResponse;

Enter fullscreen mode Exit fullscreen mode

src/responses/badResuestsResponse.ts

const badRequestResponse = (msg: string) => {
  return {
  statusCode: 400,
  headers: { "Content-Type": "text/plain" },
  body: msg,
  };
}

export default badRequestResponse

Enter fullscreen mode Exit fullscreen mode

src/responses/internalErrorResponse.ts

const internalErrorResponse = (msg: string) => {
  console.error(msg);
  return {
    statusCode: 500,
    headers: { "Content-Type": "text/plain" },
    body: "internal error",
  };
  };

export default internalErrorResponse

Enter fullscreen mode Exit fullscreen mode

And we've also got a Note type which will be the data that gets stored/retreived:

src/notes/Note.ts

type Note = {
  userId: string;
  noteId: string;
  content?: string;
  createdAt: number;
};

export default Note

Enter fullscreen mode Exit fullscreen mode

Access DB

Once we've got a DB table defined as above, we can then access the table to execute different queries

We would create a DB object instance using:

const db = new DynamoDB.DocumentClient();

Enter fullscreen mode Exit fullscreen mode

Create

A create is the simplest one of the database functions for us to implement, this uses the db.put function with the Item to save which is of type Note:

const create = async (tableName: string, item: Note) => {
  await db.put({ TableName: tableName, Item: item }).promise();
};

Enter fullscreen mode Exit fullscreen mode

Get

We can implement a getOne function by using db.get and providing the full Key consisting of the userId and noteId

const getOne = async (tableName: string, noteId: string, userId: string) => {
  const result = await db.get({
    TableName: tableName,
    Key: {
      userId: userId,
      noteId: noteId
    }
  }).promise();

  return result.Item
};

Enter fullscreen mode Exit fullscreen mode

GetAll

We can implement a getByUserId function which will make use of db.query and use the ExpressionAttributeValues to populate the KeyConditionExpression as seen below:

const getByUserId = async (tableName: string, userId: string) => {
  const result = await db.query({
    TableName: tableName,
    KeyConditionExpression: "userId = :userId",
    ExpressionAttributeValues: {
      ":userId": userId,
    },
  }).promise();

  return result.Items
};

Enter fullscreen mode Exit fullscreen mode

Define Lambdas

Now that we know how to write data to Dynamo, we can implement the following files for the endpoints we defined above:

Create

src/notes/create.ts

import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";
import { DynamoDB } from "aws-sdk";
import { v1 } from "uuid";
import internalErrorResponse from "../responses/internalErrorResponse";
import successResponse from "../responses/successResponse";
import badRequestResponse from "../responses/badRequestResponse"
import Note from "./Note";

const db = new DynamoDB.DocumentClient();

const toItem = (data: string, content: string): Note => {
  return {
    userId: data,
    noteId: v1(),
    content: content,
    createdAt: Date.now(),
  };
};

const parseBody = (event: APIGatewayProxyEventV2) => {
  const data = JSON.parse(event.body || "{}");

  return {
    userId: data.userId,
    content: data.content,
  };
};

const isValid = (data: Partial<Note>) =>
  typeof data.userId !== "undefined" && typeof data.content !== "undefined";

const create = async (tableName: string, item: Note) => {
  await db.put({ TableName: tableName, Item: item }).promise();
};

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  if (typeof process.env.tableName === "undefined")
    return internalErrorResponse("tableName is undefined");

  const tableName = process.env.tableName;
  const data = parseBody(event);

  if (!isValid(data)) return badRequestResponse("userId and content are required");

  const item = toItem(data.userId, data.content);
  await create(tableName, item);

  return successResponse(item)
};

Enter fullscreen mode Exit fullscreen mode

Get

src/notes/get.ts

import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";
import { DynamoDB } from "aws-sdk";
import badRequestResponse from "../responses/badRequestResponse";
import internalErrorResponse from "../responses/internalErrorResponse";
import successResponse from "../responses/successResponse";

type RequestParams = {
  noteId?: string;
  userId?: string
};

const db = new DynamoDB.DocumentClient();

const parseBody = (event: APIGatewayProxyEventV2): RequestParams => {
  const pathData = event.pathParameters;
  const queryData = event.queryStringParameters;

  return {
    noteId: pathData?.noteId,
    userId: queryData?.userId
  };
};

const isValid = (data: RequestParams) => typeof data.noteId !== "undefined" && typeof data.userId !== 'undefined'

const getOne = async (tableName: string, noteId: string, userId: string) => {
  const result = await db.get({
    TableName: tableName,
    Key: {
      userId: userId,
      noteId: noteId
    }
  }).promise();

  return result.Item
};

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  const data = parseBody(event);

  if (typeof process.env.tableName === "undefined")
    return internalErrorResponse("tableName is undefined");

  const tableName = process.env.tableName;

  if (!isValid(data)) return badRequestResponse("noteId is required in path, userId is required in query");

  const items = await getOne(tableName, data.noteId as string, data.userId as string);

  return successResponse(items)
};
import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";
import { DynamoDB } from "aws-sdk";
import badRequestResponse from "../responses/badRequestResponse";
import internalErrorResponse from "../responses/internalErrorResponse";
import successResponse from "../responses/successResponse";

type RequestParams = {
  noteId?: string;
  userId?: string
};

const db = new DynamoDB.DocumentClient();

const parseBody = (event: APIGatewayProxyEventV2): RequestParams => {
  const pathData = event.pathParameters;
  const queryData = event.queryStringParameters;

  return {
    noteId: pathData?.noteId,
    userId: queryData?.userId
  };
};

const isValid = (data: RequestParams) => typeof data.noteId !== "undefined" && typeof data.userId !== 'undefined'

const getOne = async (tableName: string, noteId: string, userId: string) => {
  const result = await db.get({
    TableName: tableName,
    Key: {
      userId: userId,
      noteId: noteId
    }
  }).promise();

  return result.Item
};

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  const data = parseBody(event);

  if (typeof process.env.tableName === "undefined")
    return internalErrorResponse("tableName is undefined");

  const tableName = process.env.tableName;

  if (!isValid(data)) return badRequestResponse("noteId is required in path, userId is required in query");

  const items = await getOne(tableName, data.noteId as string, data.userId as string);

  return successResponse(items)
};

Enter fullscreen mode Exit fullscreen mode

GetAll

src/notes/getAll.ts

import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";
import { DynamoDB } from "aws-sdk";
import badRequestResponse from "../responses/badRequestResponse";
import internalErrorResponse from "../responses/internalErrorResponse";
import successResponse from "../responses/successResponse";

type PathParams = {
  userId?: string;
};

const db = new DynamoDB.DocumentClient();

const parseBody = (event: APIGatewayProxyEventV2): PathParams => {
  const data = event.queryStringParameters;

  return {
    userId: data?.userId,
  };
};

const isValid = (data: PathParams) => typeof data.userId !== "undefined";

const getByUserId = async (tableName: string, userId: string) => {
  const result = await db.query({
    TableName: tableName,
    KeyConditionExpression: "userId = :userId",
    ExpressionAttributeValues: {
      ":userId": userId,
    },
  }).promise();

  return result.Items
};

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  const data = parseBody(event);

  if (typeof process.env.tableName === "undefined")
    return internalErrorResponse("tableName is undefined");

  const tableName = process.env.tableName;

  if (!isValid(data)) return badRequestResponse("userId is required in query");

  const items = await getByUserId(tableName, data.userId as string);

  return successResponse(items)
};

Enter fullscreen mode Exit fullscreen mode

Testing

Once we've got all the above completed, we can actually test our endpoints and create and read back data

create:

POST https://AWS_ENDPOINT_HERE/notes

{
  "userId": "USER_ID",
  "content": "Hello world"
}

Enter fullscreen mode Exit fullscreen mode

Which responds with:

200

{
  "content": "Hello world",
  "createdAt": 1619177078298,
  "noteId": "NOTE_ID_UUID",
  "userId": "USER_ID"
}

Enter fullscreen mode Exit fullscreen mode

get:

GET https://AWS_ENDPOINT_HERE/notes/NOTE_ID_UUID?userId=USER_ID


200

{
  "content": "Hello world",
  "createdAt": 1619177078298,
  "noteId": "NOTE_ID_UUID",
  "userId": "USER_ID"
}

Enter fullscreen mode Exit fullscreen mode

getAll

GET htttps://AWS_ENDPOINT_HERE/notes?userId=USER_ID


200

[
  {
    "content": "Hello world",
    "createdAt": 1619177078298,
    "noteId": "NOTE_ID_UUID",
    "userId": "USER_ID"
  }
]

Enter fullscreen mode Exit fullscreen mode

Creating Notes Using a Queue

When working with microservices a common pattern is to use a message queue for any operations that can happen in an asynchronous fashion, we can create an SQS queue which we can use to stage messages and then separately save them at a rate that we're able to process them

In order to make this kind of logic we're going to break up our create data flow - a the moment it's this:

lambda -> dynamo
return <-

Enter fullscreen mode Exit fullscreen mode

We're going to turn it into this:

lambda1 -> sqs
 return <-

          sqs -> lambda2 -> dynamo

Enter fullscreen mode Exit fullscreen mode

This kind of pattern becomes especially useful if we're doing a lot more stuff with the data other than just the single DB operation and also allows us to retry things like saving to the DB if we have errors, etc.

A more complex data flow could look something like this (not what we're implementing):

lambda1 -> sqs
 return <-

           sqs -> lambda2 -> dynamo // save to db
               -> lambda3 -> s3 // generate a report
           sqs <-

           sqs -> lambda4 // send an email

Enter fullscreen mode Exit fullscreen mode

Create Queue

SST provides us with the sst.Queue class that we can use for this purpose

To create a Queue you can use the following in stack:

const queue = new sst.Queue(this, "NotesQueue", {
  consumer: "src/consumers/createNote.handler",
});

queue.attachPermissions([table]);
queue.consumerFunction?.addEnvironment(
  "tableName",
  table.dynamodbTable.tableName
);

Enter fullscreen mode Exit fullscreen mode

The above code does the following:

  1. Create a queue
  2. Give the queue permission to access the table
  3. Add the tableName environment variable to the queue's consumerFunction

We will also need to grant permissions to the API to access the queue so that our create handler is able to add messages to the queue

api.attachPermissions([table, queue]);

Enter fullscreen mode Exit fullscreen mode

Which means our Stack now looks like this:

lib/MyStack.ts

import * as sst from "@serverless-stack/resources";

export default class MyStack extends sst.Stack {
  constructor(scope: sst.App, id: string, props?: sst.StackProps) {
    super(scope, id, props);

    const table = new sst.Table(this, "Notes", {
      fields: {
        userId: sst.TableFieldType.STRING,
        noteId: sst.TableFieldType.STRING,
      },
      primaryIndex: {
        partitionKey: "userId",
        sortKey: "noteId",
      },
    });

    const queue = new sst.Queue(this, "NotesQueue", {
      consumer: "src/consumers/createNote.handler",
    });

    queue.attachPermissions([table]);
    queue.consumerFunction?.addEnvironment(
      "tableName",
      table.dynamodbTable.tableName
    );

    // Create the HTTP API
    const api = new sst.Api(this, "Api", {
      defaultFunctionProps: {
        timeout: 60, // increase timeout so we can debug
        environment: {
          tableName: table.dynamodbTable.tableName,
          queueUrl: queue.sqsQueue.queueUrl,
        },
      },
      routes: {
        "GET /": "src/lambda.handler",
        "GET /hello": "src/hello.handler",
        "GET /notes": "src/notes/getAll.handler",
        "POST /notes": "src/notes/create.handler",
        "GET /notes/{noteId}": "src/notes/get.handler",
      },
    });

    api.attachPermissions([table, queue]);

    // Show API endpoint in output
    this.addOutputs({
      ApiEndpoint: api.httpApi.apiEndpoint,
    });
  }
}

Enter fullscreen mode Exit fullscreen mode

Update the Create Handler

Since we plan to create notes via a queue we will update our create function in the handler to create a new message in the queue, this is done using the SQS class from aws-sdk:

src/notes/create.ts

import { SQS } from "aws-sdk";

const queue = new SQS();

Enter fullscreen mode Exit fullscreen mode

Once we've got our instance, the create function is done by means of the queue.sendMessage function:

src/notes/create.ts

const create = async (queueUrl: string, item: Note) => {
  return await queue
    .sendMessage({
      QueueUrl: queueUrl,
      DelaySeconds: 0,
      MessageBody: JSON.stringify(item),
    })
    .promise();
};

Enter fullscreen mode Exit fullscreen mode

Lastly, our handler remains mostly the same with the exception of some additional validation to check that we have the queue connection information in the environment:

src/notes/create.ts

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  // pre-save validation
  if (typeof process.env.queueUrl === "undefined")
    return internalErrorResponse("queueUrl is undefined");

  const queueUrl = process.env.queueUrl;

  const data = parseBody(event);

  if (!isValid(data))
    return badRequestResponse("userId and content are required");

  // save process
  const item = toItem(data.userId, data.content);
  const creatresult = await create(queueUrl, item);

  if (!creatresult.MessageId) internalErrorResponse("MessageId is undefined");

  return successResponse(item);
};

Enter fullscreen mode Exit fullscreen mode

Implementing the above into the create handler means that our create.ts file now looks like this:

src/notes/create.ts

import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";
import { v1 } from "uuid";
import internalErrorResponse from "../responses/internalErrorResponse";
import successResponse from "../responses/successResponse";
import badRequestResponse from "../responses/badRequestResponse";
import Note from "./Note";
import { SQS } from "aws-sdk";

const queue = new SQS();

// helper functions start

const toItem = (data: string, content: string): Note => {
  return {
    userId: data,
    noteId: v1(),
    content: content,
    createdAt: Date.now(),
  };
};

const parseBody = (event: APIGatewayProxyEventV2) => {
  const data = JSON.parse(event.body || "{}");

  return {
    userId: data.userId,
    content: data.content,
  };
};

const isValid = (data: Partial<Note>) =>
  typeof data.userId !== "undefined" && typeof data.content !== "undefined";

// helper functions end

const create = async (queueUrl: string, item: Note) => {
  return await queue
    .sendMessage({
      QueueUrl: queueUrl,
      DelaySeconds: 0,
      MessageBody: JSON.stringify(item),
    })
    .promise();
};

export const handler: APIGatewayProxyHandlerV2 = async (
  event: APIGatewayProxyEventV2
) => {
  // pre-save validation
  if (typeof process.env.queueUrl === "undefined")
    return internalErrorResponse("queueUrl is undefined");

  const queueUrl = process.env.queueUrl;

  const data = parseBody(event);

  if (!isValid(data))
    return badRequestResponse("userId and content are required");

  // save process
  const item = toItem(data.userId, data.content);
  const creatresult = await create(queueUrl, item);

  if (!creatresult.MessageId) internalErrorResponse("MessageId is undefined");

  return successResponse(item);
};

Enter fullscreen mode Exit fullscreen mode

Add Queue-Based Create Handler

Now that we've updated our logic to save the notes into the queue, we need to add the logic for the src/consumers/createNote.handler consumer function as we specified above, this handler will be sent an SQSEvent and will make use of the DynamoDB Table we gave it permissions to use

First, we take the create function that was previously on the create.ts file for saving to the DB:

src/consumers/createNote.ts

import { DynamoDB } from "aws-sdk";

const db = new DynamoDB.DocumentClient();

const create = async (tableName: string, item: Note) => {
  const createResult = await db
    .put({ TableName: tableName, Item: item })
    .promise();
  if (!createResult) throw new Error("create failed");

  return createResult;
};

Enter fullscreen mode Exit fullscreen mode

We'll also need a function for parsing the SQSRecord object into a Note:

src/consumers/createNote.ts

const parseBody = (record: SQSRecord): Note => {
  const { noteId, userId, content, createdAt } = JSON.parse(
    record.body
  ) as Note;

  // do this to ensure we only extract information we need
  return {
    noteId,
    userId,
    content,
    createdAt,
  };
};

Enter fullscreen mode Exit fullscreen mode

And finally we consume the above through the handler, you can see in the below code that we are iterating over the event.Records object, this is because the SQSEvent adds each new event into this array, the reason for this is because we can also specify batching into our Queue so that the handler is only triggered after n events instead of each time, and though this isn't happening in our case, we still should handle this for our handler:

src/consumers/createNote.ts

export const handler: SQSHandler = async (event) => {
  // pre-save environment check
  if (typeof process.env.tableName === "undefined")
    throw new Error("tableName is undefined");

  const tableName = process.env.tableName;

  for (let i = 0; i < event.Records.length; i++) {
    const r = event.Records[i];
    const item = parseBody(r);
    console.log(item);

    const result = await create(tableName, item);
    console.log(result);
  }
};

Enter fullscreen mode Exit fullscreen mode

Putting all the above together our createNote.ts file now has the following code:

import { SQSHandler, SQSRecord } from "aws-lambda";
import Note from "../notes/Note";
import { DynamoDB } from "aws-sdk";

const db = new DynamoDB.DocumentClient();

const create = async (tableName: string, item: Note) => {
  const createResult = await db
    .put({ TableName: tableName, Item: item })
    .promise();
  if (!createResult) throw new Error("create failed");

  return createResult;
};

const parseBody = (record: SQSRecord): Note => {
  const { noteId, userId, content, createdAt } = JSON.parse(
    record.body
  ) as Note;

  // do this to ensure we only extract information we need
  return {
    noteId,
    userId,
    content,
    createdAt,
  };
};

export const handler: SQSHandler = async (event) => {
  if (typeof process.env.tableName === "undefined")
    throw new Error("tableName is undefined");

  const tableName = process.env.tableName;

  for (let i = 0; i < event.Records.length; i++) {
    const r = event.Records[i];
    const item = parseBody(r);
    console.log(item);

    const result = await create(tableName, item);
    console.log(result);
  }
};

Enter fullscreen mode Exit fullscreen mode

This completes the implementation of the asynchronous saving mechanism for notes. As far as a consumer of our API is concerned, nothing has changed and they will still be able to use the API exactly as we had in the Testing section above

Deploy

Thus far, we've just been running our API in debug mode via the npm run start command, while useful for testing this adds a lot of code to make debugging possible, and isn't something we'd want in our final deployed code

Deploying using sst is still very easy, all we need to do is run the npm run deploy command and this will update our lambda to use a production build of the code instead:

npm run deploy

Enter fullscreen mode Exit fullscreen mode

Teardown

Lastly, the sst CLI also provides us with a function to teardown our start/deploy code. So once you're done playing around you can use this to teardown all your deployed services:

npm run remove

Enter fullscreen mode Exit fullscreen mode

Note that running the remove command will not delete the DB tables, you will need to do this manually

Discussion (0)