0

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

3
  • Note that an async handler should not use the callback parameter. Use async or callback, not both. Commented Jul 9, 2020 at 19:20
  • thats in lambda function, but what about getting the context of lambda inside my sdk code Commented Jul 9, 2020 at 19:42
  • You need to provide the context to your SDK. You can do it explicitly, or via some middleware solution. Commented Jul 9, 2020 at 21:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good recommendation on middy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.