From d8119a40372d7ab0a8655e3d296169c113a6a024 Mon Sep 17 00:00:00 2001 From: ksami Date: Sat, 20 Oct 2018 21:14:55 +0800 Subject: [PATCH 1/3] Fix line for Node.js to be consistent --- doc_source/programming-model-v2.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc_source/programming-model-v2.md b/doc_source/programming-model-v2.md index dfbeed57..63428c05 100644 --- a/doc_source/programming-model-v2.md +++ b/doc_source/programming-model-v2.md @@ -20,9 +20,9 @@ You write code for your Lambda function in one of the languages AWS Lambda suppo Your Lambda function code must be written in a stateless style, and have no affinity with the underlying compute infrastructure\. Your code should expect local file system access, child processes, and similar artifacts to be limited to the lifetime of the request\. Persistent state should be stored in Amazon S3, Amazon DynamoDB, or another cloud storage service\. Requiring functions to be stateless enables AWS Lambda to launch as many copies of a function as needed to scale to the incoming rate of events and requests\. These functions may not always run on the same compute instance from request to request, and a given instance of your Lambda function may be used more than once by AWS Lambda\. For more information, see [Best Practices for Working with AWS Lambda Functions](best-practices.md) **Topics** -+ [Programming Model\(Node\.js\)](programming-model.md) ++ [Programming Model for Authoring Lambda Functions in Node.js](programming-model.md) + [Programming Model for Authoring Lambda Functions in Java](java-programming-model.md) + [Programming Model for Authoring Lambda Functions in Python](python-programming-model.md) + [Programming Model for Authoring Lambda Functions in Go](go-programming-model.md) + [Programming Model for Authoring Lambda Functions in C\#](dotnet-programming-model.md) -+ [Programming Model for Authoring Lambda Functions in PowerShell](powershell-programming-model.md) \ No newline at end of file ++ [Programming Model for Authoring Lambda Functions in PowerShell](powershell-programming-model.md) From 05d4135c985fd887cea2616641ce16b42c0eba04 Mon Sep 17 00:00:00 2001 From: ksami Date: Sat, 20 Oct 2018 21:21:50 +0800 Subject: [PATCH 2/3] Fix syntax for callback --- doc_source/nodejs-prog-model-handler.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc_source/nodejs-prog-model-handler.md b/doc_source/nodejs-prog-model-handler.md index ee8a9409..712c5f42 100644 --- a/doc_source/nodejs-prog-model-handler.md +++ b/doc_source/nodejs-prog-model-handler.md @@ -27,7 +27,7 @@ If you discover that your Lambda function does not process the event using async The Node\.js runtimes v6\.10 and v8\.10 support the optional `callback` parameter\. You can use it to explicitly return information back to the caller\. The general syntax is: ``` -callback(Error error, Object result); +callback(error, result); ``` Where: @@ -108,4 +108,4 @@ exports.myHandler = async function(event, context) { 1. Replace the template code with the code provided in this section and create the function\. -1. Test the Lambda function using the **Sample event template** called **Hello World** provided in the Lambda console\. \ No newline at end of file +1. Test the Lambda function using the **Sample event template** called **Hello World** provided in the Lambda console\. From 6f979896ca6bdb81d2a50fb9f9689dd91f5204cb Mon Sep 17 00:00:00 2001 From: ksami Date: Sat, 20 Oct 2018 21:39:49 +0800 Subject: [PATCH 3/3] Add async style handler --- doc_source/nodejs-prog-model-handler.md | 52 +++++++++++++------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/doc_source/nodejs-prog-model-handler.md b/doc_source/nodejs-prog-model-handler.md index 712c5f42..d4af6028 100644 --- a/doc_source/nodejs-prog-model-handler.md +++ b/doc_source/nodejs-prog-model-handler.md @@ -1,15 +1,27 @@ # Lambda Function Handler \(Node\.js\) -AWS Lambda invokes your Lambda function via a `handler` object\. A `handler` represents the name of your Lambda function \(and serves as the entry point that AWS Lambda uses to execute your function code\. For example: +AWS Lambda invokes your Lambda function via a `handler` object\. A `handler` represents the name of your Lambda function \(and serves as the entry point that AWS Lambda uses to execute your function code\. For example: +Using Node\.js v6\.10 ``` -exports.myHandler = function(event, context, callback) { - ... function code +exports.myHandler = function(event, context, callback) { + ... function code callback(null, "some success message"); - // or - // callback("some error type"); + // or + // callback("some error type"); } ``` + +Using Node\.js v8\.10 supporting `async`/`await` +``` +exports.myHandler = async function(event, context) { + ... function code + return "some success message"; + // or + // throw new Error("some error type"); +} +``` + + `myHandler` – This is the name of the function AWS Lambda invokes\. Suppose you save this code as `helloworld.js`\. Then, `myHandler` is the function that contains your Lambda function code and `helloworld` is the name of the file that represents your deployment package\. For more information, see [Creating a Deployment Package \(Node\.js\)](nodejs-create-deployment-pkg.md)\. AWS Lambda supports two invocation types: @@ -58,27 +70,17 @@ Note the following: **Note** When the `callback(error, null)` \(and `callback(error)`\) is called, Lambda will log the first 256 KB of the error object\. For a larger error object, AWS Lambda truncates the log and displays the text `Truncated by Lambda` next to the error object\. -If you are using runtime version 8\.10, you can include the `async` keyword: - -``` -exports.myHandler = async function(event, context) { - ... - - // return information to the caller. -} -``` - ## Example Consider the following Node\.js example code\. ``` exports.myHandler = function(event, context, callback) { - console.log("value1 = " + event.key1); - console.log("value2 = " + event.key2); - callback(null, "some success message"); - // or - // callback("some error type"); + console.log("value1 = " + event.key1); + console.log("value2 = " + event.key2); + callback(null, "some success message"); + // or + // callback("some error type"); } ``` @@ -90,11 +92,11 @@ If you want to use the `async` feature provided by the v8\.10 runtime, consider ``` exports.myHandler = async function(event, context) { - console.log("value1 = " + event.key1); - console.log("value2 = " + event.key2); - return "some success message"; - // or - // throw new Error("some error type"); + console.log("value1 = " + event.key1); + console.log("value2 = " + event.key2); + return "some success message"; + // or + // throw new Error("some error type"); } ```