Day 7: Building a Serverless Node.js API with AWS and Serverless Framework

Day 7: Building a Serverless Node.js API with AWS and Serverless Framework

Welcome to this comprehensive tutorial on creating a serverless Node.js API using AWS services and the Serverless Framework. In this tutorial, we'll walk through the process of setting up a serverless project, deploying it to AWS using the Serverless Framework, and testing the APIs using Postman. The project allows you to manage tasks in a DynamoDB table, demonstrating basic CRUD operations.

Prerequisites

Before we start, make sure you have the following:

  1. Sign up on serverless.com.

  2. Node.js and npm installed. You can download them from nodejs.org.

  3. Serverless Framework installed globally. Run the following command:

npm install -g serverless
  1. AWS IAM User credentials for deployment.

Project Setup

Step 1: Clone the Project Repository

Clone the project repository from GitHub:

git clone https://github.com/ArjunMnn/aws-node-http-api-project-dev.git

Step 2: Install Dependencies

Navigate to the project directory and install the required npm packages:

cd aws-node-http-api-project-dev
npm install

Step 3: Configure AWS Credentials

Run the following command to configure your AWS credentials:

serverless config credentials --provider aws --key YOUR_ACCESS_KEY --secret YOUR_SECRET_KEY

Replace YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your AWS IAM User credentials.

Serverless Configuration

The serverless.yaml file defines the AWS infrastructure for your serverless project. Open the serverless.yaml file and replace the placeholder values in the provider section with your specific AWS configuration.

provider:
  name: aws
  runtime: nodejs14.x
  region: YOUR_AWS_REGION
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:*
      Resource:
        - arn:aws:dynamodb:YOUR_AWS_REGION:YOUR_ACCOUNT_ID:table/KaamKaro

Replace YOUR_AWS_REGION and YOUR_ACCOUNT_ID with your AWS region and account ID.

Serverless Functions

The project consists of several functions, each handling specific API endpoints. The function code is in separate files in the src directory.

  • hello.js: Handles the root endpoint and returns a welcome message.

  • kaamBharo.js: Adds a new task to the DynamoDB table.

  • kaamDikhao.js: Retrieves all tasks from the DynamoDB table.

  • kaamHatao.js: Deletes a task from the DynamoDB table.

  • kaamKhatamKaro.js: Updates the completion status of a task in the DynamoDB table.

DynamoDB Table

The serverless.yaml file includes a DynamoDB table definition. Ensure that you have the necessary permissions to create a DynamoDB table in your AWS account.

resources:
  Resources:
    KaamKaro:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: KaamKaro
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH

Deploy the Serverless Project

Run the following command to deploy the project to AWS:

serverless deploy

This command will package and deploy your project using AWS CloudFormation.

Testing with Postman

Use Postman to test your APIs. Here are some sample requests:

  • GET /: Retrieve a welcome message.

  • POST /kaam: Add a new task to the DynamoDB table.

  • GET /kaam: Retrieve all tasks from the DynamoDB table.

  • PUT /kaam/{id}: Update the completion status of a task in the DynamoDB table.

  • POST /kaam/{id}: Delete a task from the DynamoDB table.

Conclusion

Congratulations! You've successfully created a serverless Node.js API using AWS and the Serverless Framework. This project serves as a foundation for building scalable and cost-effective serverless applications. Feel free to explore and extend the functionality based on your requirements.

Follow me on LinkedIn.

Check out my GitHub profile.