invoke lambda nodejs
To invoke a Lambda function using Node.js, you can follow these steps:
- Install the AWS SDK for JavaScript by running the following command in your Node.js project directory:
npm install aws-sdk
- Create an instance of the AWS.Lambda class and specify the region where your Lambda function is deployed, like this:
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({region: 'your-region'});
- Define the parameters for invoking the Lambda function, including the function name and payload:
const params = {
FunctionName: 'your-function-name',
Payload: JSON.stringify({key: 'value'})
};
- Invoke the Lambda function using the invoke method and handle the response:
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
By following these steps, you can successfully invoke a Lambda function using Node.js and handle the response accordingly.