This is the third part of a 3-part tutorial series on Getting started with amazon Lambda :
Let's start with lambda functions by creating a simple Hello World
Program using Amazon AWS lambda functions.
Copy the given code in the code editor on AWS as shown in the image below :
exports.handler = async (event) => {
var message = "Hello from Nodejsera! This is a Lambda function"
return message;
};
Now We need to access the lambda function using node.js
.Copy the given code in a .js
:
//Name of the file : aws-lambda-hello.js
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'Your Access Key', secretAccessKey: 'Your Access Key Secret',region:'Location',});
var lambda = new AWS.Lambda();
var params = {
FunctionName: 'helloWorld', /* Name of Your Lambda Function */
Payload: JSON.stringify({}) /* Empty */
};
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack); // Error
else console.log(data); // Success
});
We can run our code using the following command :
>node aws-lambda-hello.js
{ StatusCode: 200,
ExecutedVersion: '$LATEST',
Payload: '"Hello from Nodejsera! This is a Lambda function"' }
In this part of the Getting started with AWS lambda functions, we learned how we can code a lambda function. We also learned how to invoke a lambda function using node.js