|
| 1 | +# AWS Lambda Node.js Runtime |
| 2 | + |
| 3 | +AWS Lambda runtime API implemented in Node.js |
| 4 | + |
| 5 | +## Current versions |
| 6 | + |
| 7 | +* Node.js - **10.14.1** |
| 8 | +* aws-sdk - **2.368.0** |
| 9 | + |
| 10 | +## Goals |
| 11 | + |
| 12 | +* Provide always up-to-date Node.js execution environment. |
| 13 | +* Include the most recent `aws-cli` library. |
| 14 | +* MAke the runtime environment compatible with the default node6.10 and node8.10 environments |
| 15 | + |
| 16 | +## How to install? |
| 17 | + |
| 18 | +The following steps require `docker` and `docker-compose` to be installed on the system. |
| 19 | + |
| 20 | +The minimum `awscli` version is 1.16.67. |
| 21 | + |
| 22 | +### 1. Build |
| 23 | + |
| 24 | +Run: |
| 25 | + |
| 26 | +```bash |
| 27 | +./auto/package |
| 28 | +``` |
| 29 | + |
| 30 | +The script will build the Lambda layer in `stage/layer.zip` file. |
| 31 | + |
| 32 | +### 2. Deploy |
| 33 | + |
| 34 | +Deploy the runtime layer using the following command: |
| 35 | + |
| 36 | +```bash |
| 37 | +aws lambda publish-layer-version \ |
| 38 | + --layer-name node-10-runtime \ |
| 39 | + --description "nodejs-10.14.1 aws-cli-2.368.0" \ |
| 40 | + --compatible-runtimes provided \ |
| 41 | + --license-info Apache-2.0 \ |
| 42 | + --zip-file fileb://stage/layer.zip |
| 43 | +``` |
| 44 | + |
| 45 | +The output will look like this: |
| 46 | +```json |
| 47 | +{ |
| 48 | + "LayerVersionArn": "arn:aws:lambda:us-east-2:356111732087:layer:node-10-runtime:1", |
| 49 | + "Description": "nodejs-10.14.1 aws-cli-2.368.0", |
| 50 | + "CreatedDate": "2018-12-02T22:32:00.572+0000", |
| 51 | + "LayerArn": "arn:aws:lambda:us-east-2:356111732087:layer:node-10-runtime", |
| 52 | + "Content": { |
| 53 | + "CodeSize": 18104889, |
| 54 | + "CodeSha256": "VonrpX23FWJOmE4lvhpox+9PS9kuY4sng0o0wxNTROs=", |
| 55 | + "Location": "https://awslambda-us-east-2-layers.s3.us-east-2.amazonaws.com/snapshots/356111732087/node-10-runtime-f3415c38-d865-46b6-ae42-009985092116?......" |
| 56 | + }, |
| 57 | + "Version": 1, |
| 58 | + "CompatibleRuntimes": [ |
| 59 | + "provided" |
| 60 | + ], |
| 61 | + "LicenseInfo": "Apache-2.0" |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### 3. Share |
| 66 | + |
| 67 | +You can share the layer with other AWS accounts by executing the following command: |
| 68 | + |
| 69 | +```bash |
| 70 | +aws lambda add-layer-version-permission \ |
| 71 | + --layer-name node-10-runtime \ |
| 72 | + --version-number 1 \ |
| 73 | + --principal "*" \ |
| 74 | + --statement-id publish \ |
| 75 | + --action lambda:GetLayerVersion |
| 76 | +``` |
| 77 | + |
| 78 | +Response: |
| 79 | + |
| 80 | +```json |
| 81 | +{ |
| 82 | + "RevisionId": "8b5b2e27-5013-4983-ac1a-9008dff90bac", |
| 83 | + "Statement": "{\"Sid\":\"publish\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"lambda:GetLayerVersion\",\"Resource\":\"arn:aws:lambda:us-east-2:356111732087:layer:node-10-runtime:1\"}" |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### 4. Deploy lambda function using this runtime |
| 88 | + |
| 89 | +Let's assume that your lambda function is packaged as `lambda.zip` file and the handler is called `hello.handler`. The following command will deploy this function with the runtime layer that has just been deployed. |
| 90 | + |
| 91 | +1. Create the lambda zip file |
| 92 | + |
| 93 | + ```bash |
| 94 | + zip lambda.zip hello.js |
| 95 | + ``` |
| 96 | + |
| 97 | +2. Create the function |
| 98 | + |
| 99 | + ```bash |
| 100 | + aws lambda create-function \ |
| 101 | + --region us-east-2 \ |
| 102 | + --function-name node-10-runtime-example \ |
| 103 | + --zip-file fileb://lambda.zip \ |
| 104 | + --handler hello.handler \ |
| 105 | + --runtime provided \ |
| 106 | + --layers "arn:aws:lambda:us-east-2:356111732087:layer:node-10-runtime:2" \ |
| 107 | + --role arn:aws:iam::356111732087:role/lambda-role |
| 108 | + out.txt |
| 109 | + ``` |
| 110 | + |
| 111 | + Response: |
| 112 | + |
| 113 | + ```json |
| 114 | + { |
| 115 | + "Layers": [ |
| 116 | + { |
| 117 | + "CodeSize": 18104889, |
| 118 | + "Arn": "arn:aws:lambda:us-east-2:356111732087:layer:node-10-runtime:1" |
| 119 | + } |
| 120 | + ], |
| 121 | + "FunctionName": "node-10-runtime-example", |
| 122 | + "LastModified": "2018-12-02T22:59:10.408+0000", |
| 123 | + "RevisionId": "32e7e8a1-b5ba-4388-b6be-596278e36126", |
| 124 | + "MemorySize": 128, |
| 125 | + "Version": "$LATEST", |
| 126 | + "Role": "arn:aws:iam::356111732087:role/service-role/lambda-role", |
| 127 | + "Timeout": 3, |
| 128 | + "Runtime": "provided", |
| 129 | + "TracingConfig": { |
| 130 | + "Mode": "PassThrough" |
| 131 | + }, |
| 132 | + "CodeSha256": "shSeSmJZHv8Z0WmOAcFcHeSUGbRYRR1cFdbEudkSJHo=", |
| 133 | + "Description": "", |
| 134 | + "CodeSize": 340, |
| 135 | + "FunctionArn": "arn:aws:lambda:us-east-2:356111732087:function:node-10-runtime-example", |
| 136 | + "Handler": "hello.handler" |
| 137 | + } |
| 138 | + ``` |
| 139 | + |
| 140 | +3. Invoke the function |
| 141 | + |
| 142 | + ```bash |
| 143 | + aws lambda invoke \ |
| 144 | + --region us-east-2 \ |
| 145 | + --function-name node-10-runtime-example \ |
| 146 | + --payload '{"hello":"world"}' \ |
| 147 | + output.txt |
| 148 | +
|
| 149 | + cat output.txt |
| 150 | + ``` |
| 151 | + |
| 152 | +## Context object |
| 153 | + |
| 154 | +The context object is compatible with [the default node environments](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html). |
| 155 | + |
| 156 | +### Context Methods |
| 157 | + |
| 158 | +* `getRemainingTimeInMillis()` – Returns the number of milliseconds left before the execution times out. |
| 159 | +
|
| 160 | +### Context Properties |
| 161 | +
|
| 162 | +* `functionName` – The name of the Lambda function. |
| 163 | +* `functionVersion` – The version of the function. |
| 164 | +* `invokedFunctionArn` – The Amazon Resource Name (ARN) used to invoke the function. Indicates if the invoker specified a version number or alias. |
| 165 | +* `memoryLimitInMB` – The amount of memory configured on the function. |
| 166 | +* `awsRequestId` – The identifier of the invocation request. |
| 167 | +* `logGroupName` – The log group for the function. |
| 168 | +* `logStreamName` – The log stream for the function instance. |
| 169 | +* `callbackWaitsForEmptyEventLoop` – Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty. If false, any outstanding events will continue to run during the next invocation. |
| 170 | +
|
| 171 | +### Not yet supported properties |
| 172 | +
|
| 173 | +* `identity` - Information about the Amazon Cognito identity that authorized the request. |
| 174 | +* `clientContext` - Client context provided to the Lambda invoker by the client application. |
| 175 | +
|
| 176 | + _The [Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html) exposes the above properties in the response headers from the `/runtime/invocation/next` endpoint, but the documentation doesn't exactly specify the format of the header value._ |
| 177 | +
|
| 178 | +## Environment variables |
| 179 | +
|
| 180 | +Based on https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html |
| 181 | +
|
| 182 | +| Key | Reserved | Value | |
| 183 | +|--|--|--| |
| 184 | +|_HANDLER|Yes|The handler location configured on the function.| |
| 185 | +|AWS_REGION|Yes|The AWS region where the Lambda function is executed.| |
| 186 | +|AWS_EXECUTION_ENV|Yes|The [runtime identifier](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html), prefixed by AWS_Lambda_. For example, AWS_Lambda_java8.| |
| 187 | +|AWS_LAMBDA_FUNCTION_NAME|Yes|The name of the function.| |
| 188 | +|AWS_LAMBDA_FUNCTION_MEMORY_SIZE|Yes|The amount of memory available to the function in MB.| |
| 189 | +|AWS_LAMBDA_FUNCTION_VERSION|Yes|The version of the function being executed.| |
| 190 | +AWS_LAMBDA_LOG_GROUP_NAME AWS_LAMBDA_LOG_STREAM_NAME|Yes|The name of the Amazon CloudWatch Logs group and stream for the function.| |
| 191 | +|AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN|Yes|Access keys obtained from the function's execution role.| |
| 192 | +|LANG|No|en_US.UTF-8. This is the locale of the runtime.| |
| 193 | +|TZ|Yes|The environment's timezone (UTC). The execution environment uses NTP to synchronize the system clock.| |
| 194 | +|LAMBDA_TASK_ROOT|Yes|The path to your Lambda function code.| |
| 195 | +|LAMBDA_RUNTIME_DIR|Yes|The path to runtime libraries.| |
| 196 | +|PATH|No|/usr/local/bin:/usr/bin/:/bin:/opt/bin| |
| 197 | +|LD_LIBRARY_PATH|No|/lib64:/usr/lib64:$LAMBDA_RUNTIME_DIR:$LAMBDA_RUNTIME_DIR/lib:$LAMBDA_TASK_ROOT:$LAMBDA_TASK_ROOT/lib:/opt/lib| |
| 198 | +|NODE_PATH|No|/opt/node_modules| |
| 199 | +|AWS_LAMBDA_RUNTIME_API|Yes|(custom runtime) The host and port of the [runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html).| |
0 commit comments