Developing Simple Lambda function using Node.js
Pre Requisite:
• AWS Account
• Visual Studio
• Node.JS tool
Step 1: Create Lambda function in Visual Studio
Step 1.1 Open Visual Studio (Run as Administrator)
Add Lambda Project in Visual Studio
File -> Add New Project
Create new project as “Hello World”.
On Next screen select – Create Simple project
Click Finish.
Below screen will appear after click on Finish.
By default some files will be added in project which we can be found in Solution explorer.
- _testdriver.js : This is a utility file to help invoke and debug the lambda function. It is not included as part of the bundle upload to Lambda.
- _sampleEvent : This is JSON file use to set Event sample event data to test / debug
- App.js : Main program file in which we will write code (Application logic)
- npm : Node Package module. Utility to install NPM package
- node_modules : Repository for node.js packages. Folder contains supporting node module files. By default it contains “aws-sdk” module. When add new module it will automatically get updated and store supporting file
Open App.JS file, file contains below code:
exports.handler : start execution of your Lambda function
Step 1.2 – Update _sampleEvent.json file
Step 1.3 – Update app.js file with below code
Step 1.4 – Run Application (Press F5), below screen should appear which contains Event information from _sampleEvent.JSON file
This complete our Hello world program.
Step 2: Deploy Lambda function in AWS console
Step 2.1: Open AWS console and find Lambda under Compute service.
Step 2.2: Screen will show List of Lambda functions. Click on Create Lambda function button
Step 2.3: Screen will show list of Blueprint use to create Lambda function. We can select existing blueprint or Skip this part and move next.
Click on Skip button on screen (Bottom right corner)
Step 2.4: Configure Lambda function.
Configure function - Set Name,Description,Runtime.
Set Lambda Function code details as shown below
Code entry type: Upload ZIP file
Handler: Main JavaScript file, in our case it is app.js. Update Handler Textbox as app.handler.
Role: Set role as lambda_basic_execution
Step 2.5: Create Zip folder contains code and Node modules
To create ZIP file open Physical location of Lambda code (HelloWorld Lambda Developed in VS2013)
Select below files/folder for ZIP,
1. Node_modules
2. App.js
Step 2.6: Set Advanced settings.
Review Lambda function
Click on Create Function button. This will create your First Lambda Function.
Step 3: Configure Test Event and Test Lambda function
Step 3.1: To set Test event click on Action button and select “Configure test event” option.
Select event template and set data. This is same data which we used to test in debug mode. You can find this in _sampleEvent.json file. Click Save button (Bottom Right corner)
Click on Test button to test Lambda function. Scroll down and you will see below content on screen.
Highlighted section in Blue is output generated by Context object.
Section highlighted in yellow is output log.
Step 4: See Test result logs in CloudWatch
Step 4.1: To check logs in click on Monitor Tab. Click “View logs in CloudWatch”
You can see details logs on new screen.