Building a Serverless Node.js API with AWS and Serverless Framework
Published: 2023-12-11 10:36:31

---
title: Building a Serverless Node.js API with AWS and Serverless Framework
published: true
date: 2023-12-10 09:25:40 UTC
tags: 
canonical_url: https://arjunmenon.hashnode.dev/building-a-serverless-nodejs-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](https://www.serverless.com/).

2. Node.js and npm installed. You can download them from [nodejs.org](https://www.serverless.com/).

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

```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702199748384/57fea4a2-d7e6-4ac2-89d6-80989bb56854.png)

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

## Testing with Postman

Use [Postman](https://www.postman.com/) to test your APIs. Here are some sample requests:

- **GET /:** Retrieve a welcome message.

- ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702199807451/ad87e735-339a-46f9-8f7a-f8b1bce092af.png)

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

- ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702199884929/21223cd6-34d3-4288-aa4f-443aa2903ef6.png)

- **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**](https://www.linkedin.com/in/arjunmenon-devops/).

Check out my [**GitHub**](https://github.com/ArjunMnn) profile.