Invoking a file from AWS lambda handler.js?

I have node.js file(i.e.abc.js) which will give the output when i run in my node.js editor. I want to run the same file in AWS Lambda.For that, I created a lambda and moved abc.js to there. To run, it seems i need to implement my abc.js file in handler.js(i.e.in lambda way means callback etc).

Is there any way to trigger abc.js from handler.js rather than implementing again the same thing in handler.js?

Checked regarding the above usecase but didn't find much on google.

Updated

My abc.js file

var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({
    region: "ap-south-1"
   });

// Create S3 service object
s3 = new AWS.S3();
var params= {};
 s3.listBuckets(params, bucketList);
function bucketList(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else   
  {   
        console.log(data)
  }
}

My handler.js in lambda and modifying it based on my interpretation of your answer.

exports.handler = async (event) => {
    const abc = require('./abc.js');
    // TODO implement
abc.bucketList();     
};

This is the error i am getting

Response:
{
  "errorMessage": "abc.bucketList is not a function",
  "errorType": "TypeError",
  "stackTrace": [
    "exports.handler (/var/task/index.js:5:5)"
  ]
}

Any help is appreciated.

Require your file

const abc = require('./abc.js');

And in the handler function call your code

abc.yourExportedMethod();

AWS Lambda function errors in Node.js, This error document appears in the invocation log and, for synchronous invocations, in the output. Example index.js file – Reference error. exports.​handler  When you invoke a function synchronously, Lambda runs the function and waits for a response. When the function execution ends, Lambda returns the response from the function's code with additional data, such as the version of the function that was executed. To invoke a function synchronously with the AWS CLI, use the invoke command.

You have some issues inside of your abc.js file.

Try this snippet:

const abc = async function() 
{
    try
    {
        const AWS = require('aws-sdk');
        let s3 = new AWS.S3({region: "ap-south-1", apiVersion: '2006-03-01' });
        let params= {};
        const s3Response = await s3.listBuckets(params).promise();

        console.log(s3Response); // your list of buckets 

    }
    catch (ex)
    {
        console.error(ex);
    }
}

export default abc;

AWS Lambda function logging in Node.js, AWS Lambda function logging in Node.js Your Lambda function comes with a CloudWatch Logs log group, with a log stream The runtime sends details about each invocation to the log stream, and relays Example index.js file – Logging. To run (invoke) or debug the local version of a Lambda function, and run (invoke) or debug that function locally with any nondefault or optional properties, you must first set those properties in the function's corresponding AWS SAM template file (for example, in a file named template.yaml within the project).

Add the following line at the bottom of your abc.js

exports.bucketList = bucketList

and the rest will work as you expect it to.

Building Lambda functions with Node.js, To execute the function, choose Test. The console creates a Lambda function with a single source file named index.js . You can edit this file and add more files​  Invoke an AWS Lambda function from another Lambda Function. This is a simple Java program which will invoke a Lambda function in Amazon Web Services (AWS) from another Lambda function. I have taken this code snippet from one of the production services that I recently wrote at work.

Invoking a file from AWS lambda handler.js?, Require your file const abc = require('./abc.js');. And in the handler function call your code abc.yourExportedMethod();. Since you don't have any common event-emitters like SNS, S3, SQS, you can invoke it using AWS Lambda SDK. Gotchas: The one requirement to invoke lambda is that proper IAM permission/role has to be associated with both invoker and invoking lambda. Let LambdaA be the invoker lambda and LambdaB be the invoking lambda. Both the lambdas has to be

node.js - Invoking a file from AWS lambda handler.js?, Require your file const abc = require('./abc.js');. And in the handler function call your code abc.yourExportedMethod();. Invoking a Lambda function from within another Lambda function not getting invoked. How can I invoke a Lambda function within a Lambda function? in function 1, it will be triggered by an API Gateway request and will invoke a second lambda function as an event (InvocationType = Event). in function 2, it will do an http request to an endpoint.

Hello World AWS Lambda function, Creating your first Lambda function using Node.js, and deploying to AWS. create a simple JavaScript Lambda function – for example, in a file called lambda​.js . for the Lambda to execute (in the Lambda terminology, that's the handler ). Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. To invoke a function asynchronously, set InvocationType to Event. For synchronous invocation, details about the function response, including errors, are included in the response body and headers.

Comments
  • Could you elaborate ? When i tried abc.yourExportedMethod(); in my handler.js it is throwing error. Please check my Updated Section in my question