/udacity-serverless

serverless

Primary LanguageTypeScriptMIT LicenseMIT

Serverless TODO App

This is a TODO application using AWS Lambda and Serverless framework for the udacity nanodegree.

Functionality of the application

This application allows creating/removing/updating/fetching TODO items. Each TODO item can have an attachment image. Each user only has access to TODO items that he/she has created.

TODO items

The application stores TODO items, and each TODO item contains the following fields:

  • todoId (string) - a unique id for an item
  • createdAt (string) - date and time when an item was created
  • userId (string) - id of the user that created an item
  • name (string) - name of a TODO item (e.g. "Change a light bulb")
  • dueDate (string) - date and time by which an item should be completed
  • done (boolean) - true if an item was completed, false otherwise
  • attachmentUrl (string) (optional) - a URL pointing to an image attached to a TODO item

Lambda Functions for the API Gateway

The following functions are configures in the serverless.yml file:

  • Auth - this function implements a custom authorizer for API Gateway that is added to all other functions.

  • GetTodos - returns all TODOs for a current user. A user id can be extracted from a JWT token that is sent by the frontend

The function return data looks like this:

{
  "items": [
    {
      "todoId": "123",
      "userId": "abc",
      "createdAt": "2019-07-27T20:01:45.424Z",
      "name": "Buy milk",
      "dueDate": "2019-07-29T20:01:45.424Z",
      "done": false,
      "attachmentUrl": "http://example.com/image.png"
    },
    {
      "todoId": "456",
      "userId": "abc",
      "createdAt": "2019-07-27T20:01:45.424Z",
      "name": "Send a letter",
      "dueDate": "2019-07-29T20:01:45.424Z",
      "done": true,
      "attachmentUrl": "http://example.com/image.png"
    },
  ]
}
  • CreateTodo - creates a new TODO for a current user. A shape of data send by a client application to this function can be found in the CreateTodoRequest.ts file

The function receives a new TODO item to be created in JSON format that looks like this:

{
  "createdAt": "2019-07-27T20:01:45.424Z",
  "name": "Buy milk",
  "dueDate": "2019-07-29T20:01:45.424Z",
  "done": false,
  "attachmentUrl": "http://example.com/image.png"
}

The function returns a new TODO item that looks like this:

{
  "item": {
    "todoId": "123",
    "userId": "abc",
    "createdAt": "2019-07-27T20:01:45.424Z",
    "name": "Buy milk",
    "dueDate": "2019-07-29T20:01:45.424Z",
    "done": false,
    "attachmentUrl": "http://example.com/image.png"
  }
}
  • UpdateTodo - updates an item created by a current user. A shape of data send by a client application to this function can be found in the UpdateTodoRequest.ts file

It receives an object that contains three fields that can be updated in a TODO item:

{
  "name": "Buy bread",
  "dueDate": "2019-07-29T20:01:45.424Z",
  "done": true
}

The id of an item that should be updated is passed as a URL parameter.

It returns an empty body.

  • DeleteTodo - deletes a TODO item created by a current user. Expects an id of a TODO item to remove.

It returns an empty body.

  • GenerateUploadUrl - returns a pre-signed URL that can be used to upload an attachment file for a TODO item.

It return a JSON object that looks like this:

{
  "uploadUrl": "https://s3-bucket-name.s3.eu-west-2.amazonaws.com/image.png"
}

All functions are already connected to appropriate events from API Gateway.

An id of a user can be extracted from a JWT token passed by a client.

All required AWS resources can be found in the resources section of the serverless.yml file such as DynamoDB table and S3 bucket.

Frontend

The client folder contains a web application that can use the API that is developed in this project.

This frontend works with the serverless application and you don't need to make any changes to the code. The only file that you need to edit is the config.ts file in the client folder. This file configures your client application and contains an API endpoint and Auth0 configuration:

const apiId = '...' API Gateway id
export const apiEndpoint = `https://${apiId}.execute-api.us-east-1.amazonaws.com/dev`

export const authConfig = {
  domain: '...',    // Domain from Auth0
  clientId: '...',  // Client id from an Auth0 application
  callbackUrl: 'http://localhost:3000/callback'
}

Change the region, if you want to use a different region.

Authentication

To use the implement authentication in your application, create an Auth0 application and copy "domain" and "client id" to the config.ts file in the client folder. We recommend using asymmetrically encrypted JWT tokens.

Logging

The project comes with a configured Winston logger that creates JSON formatted log statements. You can use it to write log messages like this:

import { createLogger } from '../../utils/logger'
const logger = createLogger('auth')

// You can provide additional information with every log statement
// This information can then be used to search for log statements in a log storage system
logger.info('User was authorized', {
  // Additional information stored with a log statement
  key: 'value'
})

How to run the application

Backend

To deploy an application run the following commands in the backend folder

npm install
sls deploy -v

Frontend

To run a client application first edit theclient/src/config.tsfile to set correct parameters. Then execute the commands in the client folder

npm install
npm run start

This starts a development server with the React application hosts the serverless TODO application and fetches data from the cloud

Postman collection

To test the API, use the Postman collection that contains sample requests. You can find a Postman collection in this project. To import this collection, do the following.

Click on the import button:

Alt text

Click on the "Choose Files":

Alt text

Select a file to import:

Alt text

Right click on the imported collection to set variables for the collection:

Alt text

Provide variables for the collection (similarly to how this was done in the course):

Alt text