mrbugsuncle
mrbugsuncle

Reputation: 1

AWS lambda getting handler or context information in node js

I am having a SDK which i include in my lambda function. This SDK is responsible for gathering information about the context of lambda function and sends it to my remote server. Below is my sample lambda code

import mymodule 

 exports.handler = async (event, context, callback) => {
    var array = [];
    var count = 11000000; //loop to max number just to make sure we hit the limit
  // var count = 110000
    const str = "This is the memory error, will get the memory error.";
    
    //Start appending array with the string  
    for (var i = 0; i < count; i++) {
      array.push(str);
    }
  
    console.log(array);
  };

in my SDK code i am capturing lambda_bootstrap by following statement

lambda_bootstap = require.main

this returns me majority of the parameters but dose not return the context any help to get lambda context in my SDK would be great. TIA

Upvotes: 0

Views: 950

Answers (1)

Noel Llevares
Noel Llevares

Reputation: 16037

You are importing mymodule at the module-level which means your SDK will be instantiated outside of an invocation context. So your SDK will be called only during the cold start and not in every invocation. context is only available within the invocation itself.

If you wish to get the context (which varies in every invocation), you have to call your SDK inside the handler.

What you essentially need to do is to create a wrapper for the Lambda handler so that you can call your SDK in each invocation. Middy JS provides with you with some patterns on how to do this.

Upvotes: 1

Related Questions