Building a Simple API with Serverless Framework and AWS — Part 2
Welcome back! In Part 1, we covered the basics of setting up a simple API using the Serverless Framework and AWS. In this part, we’ll dive into testing your serverless application locally. Testing locally is a crucial step in the development process, as it allows you to quickly iterate and debug your functions without needing to deploy the application to AWS each time.
Why do we need to test our application locally?
Testing the applications locally is important for several reasons:
- Speed: Local testing is much faster than deploying to AWS for every change. This accelerates the development cycle.
- Cost: Reduces the cost associated with deploying to and running functions on AWS.
- Debugging: Provides immediate feedback and allows for easier debugging using local tools and IDEs.
- Isolation: Ensures that your changes do not affect the production environment until they are fully tested.
Setting Up serverless-offline
serverless-offline is a plugin for the Serverless Framework that emulates AWS Lambda and API Gateway on your local machine. It allows you to test your serverless applications locally.
Step 1: Install Serverless-Offline
To install the serverless-offline plugin, run the following command in your project directory:
after you run the command the serverless-offline npm package will be added to node_modules and you will see that the serverless-offline will be added in the packages.json.
Step 2: Configure serverless-offline
Open your serverless.yml
file and add the serverless-offline
plugin to the plugins section:
This configuration tells the Serverless Framework to use the Serverless-offline plugin.
Testing the Application Locally
With the serverless-offline plugin installed and configured, you can now start your serverless application locally.
Step 1: Start the Serverless Offline Environment
In your project directory, run the following command:
After running the command you should see output similiar to this.
This output indicates that your serverless application is running locally on http://localhost:3000
Step 2: Test the Local API
Open your browser or use a tool like curl
to test the local endpoint.
After running the curl command you will receive a JSON response similar to above image.
Next topic…
I am thinking what’s the next topic I will cover. Feel free to suggest in comment section what you want to learn.