Skip to content

Simple Rest API that is deployed via a serverless framework using nestjs as development framework

Notifications You must be signed in to change notification settings

desarrollomarquez/nestjs-serverless-aws

Repository files navigation

How to create a Rest API using the serverless framework to use only AWS pay-per-use services, using nestjs as a development framework?


Ilustrator API Rest Serverless Framework

  • The basic need is that we need to create a Rest API using Serverless charge-only services.

  • The solution will be to define the project through a typical development of a service in nestjs with typescript where initially we will deploy on a local machine and then we will deploy to an AWS account using the Serverless Framework.


Tools or Services:

  • Api Gateway
  • Lamda Function
  • DynamoDB
  • Cloudwatch
  • IAM Policys
  • Nestjs Framework

Steps:

1. Requirements to use AWS CLI:

  • AWS account.
  • IAM credentials. (Username and Password to enter the console).
  • IAM access key pair. (Access Key ID and Secret Access Key).

2. Installing and updating the AWS CLI:

Linux/Ubuntu:

Windows:

3. AWS CLI Quick Configuration:

Execute the command:

  • aws configure
Replace it with your own AWS user account values, as described:

  • AWS Access Key ID [None]: XXXXXXXXXXXXXXXXXXXX
  • AWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  • Default region name [None]: us-east-1
  • Default output format [None]: json
Validate the AWS CLI configuration:

  • aws configure list

4. Configuration of the Serverless Framework Project:

Clone the Serverless Framework project:

Move to the root folder:

  • cd nestjs-serverless-aws/

5. Create .env for Project:

The .env environment file must be created with the following variables, these can vary the values:

  • IS_OFFLINE=true
  • DYNAMODB_ENDPOINT=http://localhost:5000
  • USERS_TABLE_NAME=BooksTable

6. Install dependencies:

Install global dependencies :

  • sudo npm install --save aws-serverless-fastify or sudo npm install
Install the Serverless Framework dependencies for the services to use:

  • sudo serverless plugin install -n serverless-plugin-optimize
  • sudo serverless plugin install -n serverless-dynamodb-local
  • sudo serverless plugin install -n serverless-offline
Install dynamodb locally for development mode (dev):

  • sudo serverless dynamodb install

7. Deployment in Dev:

Generate the build and deploy the serverless development project (dev) offline, here we can perform tests locally to verify the project configuration before deploying to the AWS account:

  • sudo npm run build && serverless offline start

Ilustrator API Rest Serverless Framework

8. Deployment in AWS:

Generate the build and deploy the serverless project in AWS:

  • sudo serverless deploy
Verify the correct Serverless deployment in AWS of the Service:
Ilustrator API Rest Serverless Framework
should display the endpoints and lambda functions generated from the code for consumption.

9. Test Services:

Test the operation of the services that allow us to consume endpoints, such as Postman or another tool of your choice, Create .json to be consumed by a POST of the lambda function:

Ilustrator API Rest Serverless Framework

Verify through GET that the information is being stored in DynamoDB by requesting:

Ilustrator API Rest Serverless Framework

In this case, the serverless service is working correctly through the endpoint delivered by the deployment to AWS.


Review of the services used for deployment in the AWS console:

Ilustrator API Rest Serverless Framework

10. Remove the serverless project from AWS:

  • sudo serverless remove

Ilustrator API Rest Serverless Framework

I hope I have given you a hand if you have doubts write me. successes! ⭐️ From @Diego Márquez

About

Simple Rest API that is deployed via a serverless framework using nestjs as development framework

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published