Menu
AWS Lambda
Developer Guide

Using the Earlier Node.js Runtime v0.10.42

As of April 2017, AWS Lambda supports Node.js 6.10 and Node.js 4.3. For information about specifying this runtime when you create your Lambda function, see the --runtime parameter of CreateFunction.

Node v0.10.42 is currently marked as deprecated. As of Jan 2017, you can no longer create new functions using the Node v0.10.42 runtime. Existing functions will be supported until early 2017. We recommend you migrate existing functions to the newer Node runtime versions available on AWS Lambda (nodejs4.3 or nodejs6.10) as soon as possible. The section highlights AWS Lambda's runtime support policy, along with behavior unique to runtime v0.10.42 and how to migrate your existing functions to newer versions.

Runtime Support Policy

AWS Lambda will only deprecate runtimes that are marked as EOL (End of Life) at the end of their maintenance window as specified in the Node LTS working group page. Versions that are marked for EOL (like Node 0.10) will stop supporting new function-creation first. Existing functions will continue to work until customers have sufficient time to migrate to newer versions, and we will work with individual customers as needed. AWS Lambda will add support for additional LTS (Long Term Support) versions of Node within a few months of the version being marked as LTS.

Transitioning Lambda Function Code to Newer Runtimes

The following sections explain how to migrate your existing Lambda function code to newer runtimes:

  1. Review all your existing Lambda functions and plan your migration. You can obtain your list of functions in the following ways:

  2. For each function:

    1. Update the runtime first. We strongly recommend updating any use of the context method and replacing it with the callback approach. For more details, see The Context Methods in Node.js Runtime v0.10.42.

    2. Test and verify the Lambda function passes your internal validation for its behavior. If it fails, you may need to update your Lambda code to work in the new runtime:

  3. Once your function is invoked successfully, the transition is complete.

Listing Lambda Functions and Updating the Runtime Using the CLI

You can use the ListFunctions command to return a list of all Lambda functions and from that list those created in the v0.10 runtime. The following code samples below demonstrate how to do that.

  • ListFunctions: The following code returns a list of all Node.js functions for that account:

    Copy
    aws lambda list-functions --query 'Functions[*].[FunctionName, Runtime]' | awk '{ if ( /nodejs/) print $0}' lambdafunctions.txt | sort -n | uniq -c | sort –nr
  • ListFunctions: The following code returns a list of all Node.js v0.10 functions for that account:

    Copy
    aws lambda list-functions --query 'Functions[*].[FunctionName, Runtime]' | grep -v nodejs4.3 lambdafunctions.txt | grep -B1 nodejs| grep , | sort

For each Lambda function returned that was created using the v0.10 runtime, use the UpdateFunctionConfiguration command and set the --runtime value to nodejs4.3 or 6.10.

Listing Lambda Functions and Updating to Newer Runtime Using the Lambda Console

To migrate Lambda functions to newer runtimes using the console:

  • Sign in to the AWS Management Console and open the Lambda console.

  • Choose the Runtime tab. This will sort all the Lambda functions for that region by their runtime value.

  • Open each Lambda function with a runtime value of node.js and then choose the Configuration tab.

  • Set the Runtime value to Node.js 4.3 or Node.js 6.10.

  • Repeat this process for each region, as necessary.

The Context Methods in Node.js Runtime v0.10.42

Node.js runtime v0.10.42 does not support the callback parameter for your Lambda function that runtimes v4.3 and v6.10 support. When using runtime v0.10.42, you use the following context object methods to properly terminate your Lambda function. The context object supports the done(), succeed(), and fail() methods that you can use to terminate your Lambda function. These methods are also present in runtimes v4.3 and v6.10 for backward compatibility. For information about transitioning your code to use runtime v4.3 or v6.10, see Transitioning Lambda Function Code to Newer Runtimes.

context.succeed()

Indicates the Lambda function execution and all callbacks completed successfully. Here's the general syntax:

Copy
context.succeed(Object result);

Where:

result – is an optional parameter and it can be used to provide the result of the function execution.

The result provided must be JSON.stringify compatible. If AWS Lambda fails to stringify or encounters another error, an unhandled exception is thrown, with the X-Amz-Function-Error response header set to Unhandled.

You can call this method without any parameters (succeed()) or pass a null value (succeed(null)).

The behavior of this method depends on the invocation type specified in the Lambda function invocation. For more information about invocation types, see Invoke.

  • If the Lambda function is invoked using the Event invocation type (asynchronous invocation), the method will return HTTP status 202, request accepted response.

  • If the Lambda function is invoked using the RequestResponse invocation type (synchronous invocation), the method will return HTTP status 200 (OK) and set the response body to the string representation of the result.

context.fail()

Indicates the Lambda function execution and all callbacks completed unsuccessfully, resulting in a handled exception. The general syntax is shown following:

Copy
context.fail(Error error);

Where:

error – is an optional parameter that you can use to provide the result of the Lambda function execution.

If the error value is non-null, the method will set the response body to the string representation of error and also write corresponding logs to CloudWatch. If AWS Lambda fails to stringify or encounters another error, an unhandled error occurs with the X-Amz-Function-Error header set to Unhandled.

Note

For the error from context.fail(error) and context.done(error, null), Lambda logs the first 256 KB of the error object. For larger error objects, AWS Lambda truncates the error and displays the text: Truncated by Lambda next to the error object.

You can call this method without any parameters (fail()) or pass a null value (fail(null)).

context.done()

Causes the Lambda function execution to terminate.

Note

This method complements the succeed() and fail() methods by allowing the use of the "error first" callback design pattern.  It provides no additional functionality.

The general syntax is:

Copy
context.done(Error error, Object result);

Where:

  • error – is an optional parameter that you can use to provide results of the failed Lambda function execution.

  • result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored.

You can call this method without any parameters (done()), or pass null (done(null)).

AWS Lambda treats any non-null value for the error parameter as a handled exception.

The function behavior depends on the invocation type specified at the time of the Lambda invocation. For more information about invocation types, see Invoke.

  • Regardless of the invocation type, the method automatically logs the string representation of non-null values of error to the Amazon CloudWatch Logs stream associated with the Lambda function.

  • If the Lambda function was invoked using the RequestResponse (synchronous) invocation type, the method returns response body as follows:

    • If error is null, set the response body to the JSON representation of result. This is similar to context.succeed().

    • If the error is not null or the function is called with a single argument of type error, the error value will be populated in the response body.

Note

For the error from both the done(error, null) and fail(error), Lambda logs the first 256 KB of the error object, and for larger error object, AWS Lambda truncates the log and displays the text Truncated by Lambda" next to the error object.

Comparing the Context and Callback Methods

If you previously created Lambda functions using Node.js runtime v0.10.42, you used one of the context object methods (done(), succeed(), and fail()) to terminate your Lambda function. In Node.js runtimes v4.3 and v6.10, these methods are supported primarily for backward compatibility. We recommend you use the callback (see Using the Callback Parameter). The following are callback examples equivalent to the context object methods:

  • The following example shows the context.done() method and corresponding equivalent callback supported in the newer runtime.

    Copy
    // Old way (Node.js runtime v0.10.42). context.done(null, 'Success message'); // New way (Node.js runtime v4.3 or v6.10). context.callbackWaitsForEmptyEventLoop = false; callback(null, 'Success message');

    Important

    For performance reasons, AWS Lambda may reuse the same Node.js process for multiple executions of the Lambda function. If this happens, AWS Lambda freezes the Node process between execution,retaining the state information it needs to continue execution.

    When the context methods are called, AWS Lambda freezes the Node process immediately, without waiting for the event loop associated with the process to empty. The process state and any events in the event loop are frozen. When the function is invoked again, if AWS Lambda re-uses the frozen process, the function execution continues with its same global state (for example, events that remained in the event loop will begin to get processed). However, when you use callback, AWS Lambda continues the Lambda function execution until the event loop is empty. After all events in the event loop are processed, AWS Lambda then freezes the Node process, including any state variables in the Lambda function. Therefore, if you want the same behavior as the context methods, you must set the context object property, callbackWaitsForEmptyEventLoop, to false.

  • The following example shows the context.succeed() method and corresponding equivalent callback supported in the newer runtime.

    Copy
    // Old way (Node.js runtime v0.10.42). context.succeed('Success message'); // New way (Node.js runtime v4.3 or v6.10). context.callbackWaitsForEmptyEventLoop = false; callback(null, 'Success message');

  • The following example shows the context.fail() method and corresponding equivalent callback supported in the newer runtime.

    Copy
    // Old way (Node.js runtime v0.10.42). context.fail('Fail object'); // New way (Node.js runtime v4.3 or v6.10). context.callbackWaitsForEmptyEventLoop = false; callback('Fail object', 'Failed result');