PinchiDev / notes-app

a simple notes app - serverless architecture
0 stars 0 forks source link

Simple notes app #1

Open PinchiDev opened 10 months ago

PinchiDev commented 10 months ago

Project Description

This project aims to develop the backend for a simple notes app using serverless-framework and AWS lambda.

Tasks

Get development environment in a Working State

Set up the Serverless backend

Build a Serverless REST API

Set up third-party billing API

Set up unit tests.

Deploy backend

Deploy the backend to production.

Contributing

Please feel free to comment on this issue, this is a work in progress from a developer in progress so any contributions are welcome.

PinchiDev commented 10 months ago

Setting up an AWS (Amazon Web Services) account was easy and self-explanatory just like creating the DynamoDB table. The S3 bucket (Amazon Simple Storage Service) we created will be used for storing the files that users may want to attach to a note. Additionally, we created a Cognito user pool, this is the way we will manage user accounts, logins, and permissions using AWS.

The Serverless framework was installed globally using npm with the command: npm install serverless -g. Following that, we installed a boilerplate or template using the Serverless CLI: serverless install --url https://github.com/AnomalyInnovations/serverless-nodejs-starter --name notes-app-api. 0b4d14ee7454aa1973799da1b8ca44d2efad1656

PinchiDev commented 10 months ago

We implement a 'Create' API to store user notes in the database by defining a Lambda function in a 'create.js' file located in the project's root directory.

Within this file, we first specify the AWS region where our table resides and initialize the DynamoDB client. Next, we extract and structure the data from the incoming request to set the parameters needed for creating an object. We then utilize the '.put' method in DynamoDB to insert a new item into our table. We also include a straightforward conditional statement to manage errors and successful response including the header that enables CORSS to communicate with the front end later on.

Then we need to configure our API endpoint, This is done in the serverless.yml file, in which we declare the name of the service, the environment in which it will create the item, and also the permissions for this lambda function.

We test the response by creating a /mocks directory and a file to represent the input parameters of the request, then we invoke our function using the command: serverless invoke local --function create --path mocks/create-event.json receiving a status code 200 as a successful response of the creation of the new item in the DB. c91fa33411529a724d961549cb15f5c2bc8b310c

PinchiDev commented 10 months ago

Before proceeding with the development of additional APIs, we need to refactor our code to ensure that our lambda functions are asynchronous and that we consistently use the same client. To achieve this, we have introduced two key components:

  1. lib/dynamodb-lib.js: This module is designed to streamline handling HTTP responses, facilitating the use of the same client instantiation throughout our codebase. By employing the .promise() method, we effectively manage asynchronous operations.
  2. lib/handler-lib.js: This file serves as a wrapper for our lambda functions, enabling us to handle responses seamlessly, whether they are synchronous or asynchronous in nature. a655488