Chapter 6 focuses on AWS Lambda, but it’s a rather vast topic, so we had to omit some things. This appendix covers information not included in chapter 6, such as information about Lambda’s execution environment, inherent limitations, and older runtimes.
At the time we wrote this appendix, Lambda ran on Amazon Linux with kernel version 4.1.17-22.30.amzn1.x86-24. If you’re like us, you might be interested in peeking under the hood and exploring the environment that Lambda has to offer. Luckily, we can get a bit of understanding of what’s under the covers by running shell commands. To try this yourself, do the following:
You can now run this function and pass in commands you want the function to execute:
{ "cmd" : "ls -al" }
Table D.1 shows the output if you run a few common commands.
Command |
Purpose |
Abbreviated output |
---|---|---|
uname -a | Prints system information. | Linux ip-10-0-95-167 4.1.17-22.30.amzn1.x86_64 #1 SMP Fri Feb 5 23:44:22 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux |
pwd | Prints the current working directory. | /var/task |
ls -al | Lists the contents of current directory. | drwxr-xr-x 2 slicer 497 4096 Apr 4 10:10 . drwxr-xr-x 20 root root 4096 Apr 4 09:04 .. -rw-rw-r-- 1 slicer 497 478 Apr 4 10:09 index.js |
env | Prints shell and environmental variables. | AWS_SESSION_TOKEN=FQoDYXd... AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/run2 LAMBDA_TASK_ROOT=/var/task LD_LIBRARY_PATH=/usr/local/lib64/node-v4.3.x/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib AWS_LAMBDA_LOG_STREAM_NAME=2017/01/23/[$LATEST]a65f9e2f349d4e9a8c9e193b0e175e78 AWS_LAMBDA_FUNCTION_NAME=run_command PATH=/usr/local/lib64/node-v4.3.x/bin:/usr/local/bin:/usr/bin/:/bin AWS_DEFAULT_REGION=us-east-1 PWD=/var/task AWS_SECRET_ACCESS_KEY=G9zLllGtxmL4... LAMBDA_RUNTIME_DIR=/var/runtime LANG=en_US.UTF-8 NODE_PATH=/var/runtime:/var/task:/var/runtime/node_modules AWS_REGION=us-east-1 AWS_ACCESS_KEY_ID=ASIAIKGQE5YIXTNE54JQ SHLVL=1 AWS_LAMBDA_FUNCTION_MEMORY_SIZE=128 _=/usr/bin/env |
cat /proc/cpuinfo | Prints the type of processor used by the system. |
processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 63 model name : Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz stepping : 2 microcode : 0x36 cpu MHz : 2900.074 cache size : 25600 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase bmi1 avx2 smep bmi2 erms invpcid xsaveopt bogomips : 5800.14 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 63 model name : Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz stepping : 2 microcode : 0x36 cpu MHz : 2900.074 cache size : 25600 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 1 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase bmi1 avx2 smep bmi2 erms invpcid xsaveopt bogomips : 5800.14 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual |
ls /var/runtime/node_modules | Lists included NodeJS modules. You do not need to provide these modules with your function. |
awslambda aws-sdk dynamodb-doc imagemagick |
Lambda executes code and scales automatically. It can handle thousands of requests per second. As with any system, however, there are limitations to think about. Table D.2 summarizes these. You can find the original AWS documentation related to Lambda’s limits at https://docs.aws.amazon.com/lambda/latest/dg/limits.html.
What is it? |
Default limit |
Explanation |
---|---|---|
Ephemeral disk capacity (/tmp space) | 512 MB | The total disk capacity you can use for temporary files |
Number of file descriptors | 1024 | Maximum number of files that can be opened by the function |
Number of processes and threads (combined total) | 1024 | Maximum number of threads and processes that can be spawned by the function |
Maximum execution duration per request | 300 seconds | The maximum number of seconds the function can execute for before it’s killed by the runtime |
Invoke request body payload size (RequestResponse) | 6 MB | The maximum size of the request when the function is invoked using the AWS SDK, the API Gateway, or the console |
Invoke request body payload size (Event) | 128 K | The maximum size of the request when the function is invoked by an event in AWS |
Invoke response body payload size (RequestResponse) | 6 MB | The maximum size of the response when the function is invoked using the AWS SDK, API Gateway, or the console |
AWS initially released Lambda, which used Node.js 0.10.42. That version of Lambda didn’t support the callback function. Instead, methods (succeed, fail, and done) available via the context object allowed the developer to cleanly terminate the function and return data to the caller. If you happen to come across a version of a Lambda function that uses the old Node.js 0.10.42 runtime, here’s what you need to know to use it correctly.
To properly terminate a Lambda function, you need to invoke one of the following three methods (this is different from the Node.js 4.3 or 6.10 versions where you can use callback):
You must always terminate the function using a succeed, fail, or done function. If you don’t do that, your function may continue to run even after you think it’s finished.
The context.succeed(Object result) method is called to indicate that the function has successfully finished execution. The result parameter is optional (you can use context.succeed() or context.succeed(null)), but it must be compatible with JSON.stringify if you decide to include it.
In case of a RequestResponse invocation type, calling this method will return an HTTP status 200 (OK). The body of the response will be set to the stringified version of result.
The context.fail(Error error) method is called to indicate that the function failed. Calling this function raises a handled exception. The error parameter is optional (you can leave it out or use a null). In the case of RequestResponse, if this parameter is provided, Lambda will try to stringify it and include it as the response body. It will set the HTTP status code to 400 (Bad Request) and also log the first 256 KB of the error object to CloudWatch.
Finally, there’s the context.done(Error error, Object result) method. This method can be used in lieu of the succeed and fail methods. The error and result parameters are optional. If a non-null value is provided for the error parameter, this function is treated the same way as context.fail(error). If the error parameter is null, then the function is treated as a context.succeed(result) method.
18.225.55.151