Testing AWS Lambda Functions Locally with Node.js and Deploying to AWS
Before deploying your AWS Lambda function, it is crucial to test it locally to ensure that it works as expected. This tutorial will walk you through the process of setting up and testing an AWS Lambda function using Node.js and then deploying it to AWS Lambda.
Step 1: Set up your Lambda function project
First, create a new folder for your Lambda function and navigate to it:
mkdir my-lambda-function
cd my-lambda-function
Next, initialize a new Node.js project by running npm init
and follow the prompts:
npm init
This command will create a package.json
file in your project folder.
Step 2: Install required dependencies
Install the required dependencies using npm
. For example, if you need the ‘axios’ package, run:
npm install axios
This command will update the package.json
file and create a node_modules
folder with the installed dependencies.
Step 3: Write your Lambda function code
Create a new file called index.js
in the my-lambda-function
folder. This file will contain your Lambda function code. You can use the required packages like this:
const axios = require('axios');
exports.handler = async (event) => {
const response = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
};
Step 4: Test your Lambda function locally
In your my-lambda-function
folder, create a new file called test.js
. In the test.js
file, require the Lambda function and call the handler with a sample event and context. The event and context can be empty objects ({}
) if your function does not rely on specific input data. Log the output to the console:
const lambdaFunction = require('./index');
async function test() {
const event = {}; // Replace with sample event data if needed
const context = {}; // Replace with sample context data if needed
try {
const result = await lambdaFunction.handler(event, context);
console.log('Lambda function result:', result);
} catch (error) {
console.error('Error executing Lambda function:', error);
}
}
test();
Open a terminal in the my-lambda-function
folder and run the test script:
node test.js
This command will execute your Lambda function and log the output to the console.
Step 5: Zip your Lambda function
After testing your Lambda function locally and making sure it works as expected, zip the contents of the my-lambda-function
folder, including the node_modules
folder, index.js
, and package.json
. Make sure not to zip the folder itself.
zip -r my-lambda-function.zip index.js package.json node_modules
Step 6: Deploy your Lambda function to AWS
-
Now, go to the AWS Lambda console (https://console.aws.amazon.com/lambda).
-
Click on the “Create function” button.
-
Select “Author from scratch”, provide a function name, and choose the latest Node.js runtime (e.g., Node.js 18.x).
-
Under “Function code”, choose “Upload a .zip file” as the “Code entry type” and upload the
my-lambda-function.zip
file you created earlier. -
Click on “Create function” at the bottom of the page.
Now your Lambda function has the required package(s) and can use them in the function code. To test the function, create a test event by clicking the “Test” button in the console and then click “Test” again after configuring the test event.
Conclusion:
In this tutorial, you’ve learned how to test an AWS Lambda function locally using Node.js, zip the contents of your project folder, and deploy the function to AWS Lambda. This process will help you identify and fix issues early in the development process, saving time and resources.