More Related Content Similar to AWS Lambda Layers, the Runtime API, & Nested Applications: re:Invent 2018 Recap at the AWS Loft - San Francisco (20) More from Amazon Web Services (20) AWS Lambda Layers, the Runtime API, & Nested Applications: re:Invent 2018 Recap at the AWS Loft - San Francisco1. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Pop-up Loft
AWS Lambda Layers and the Runtime API
Chris Munns
Principal Developer Advocate
AWS Serverless
2. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
About me:
Chris Munns - munns@amazon.com, @chrismunns
– Principal Developer Advocate - Serverless
– New Yorker
– Previously:
• AWS Business Development Manager – DevOps, July ’15 - Feb ‘17
• AWS Solutions Architect Nov, 2011- Dec 2014
• Formerly on operations teams @Etsy and @Meetup
• Little time at a hedge fund, Xerox and a few other startups
– Rochester Institute of Technology: Applied Networking and Systems
Administration ’05
– Internet infrastructure geek
3. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
https://secure.flickr.com/photos/mgifford/4525333972
Why are we
here today?
4. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
5. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Serverless applications
Services (anything)
Changes in
data state
Requests to
endpoints
Changes in
resource state
Event source Function
Node.js
Python
Java
C#
Go
Ruby
Runtime API
6. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Anatomy of a Lambda function
Handler() function
Function to be executed
upon invocation
Event object
Data sent during
Lambda function
Invocation
Context object
Methods available to
interact with runtime
information (request ID,
log group, more)
public String handleRequest(Book book, Context context) {
saveBook(book);
return book.getName() + " saved!";
}
7. Anatomy of a Lambda function
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
8. Anatomy of a Lambda function
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Functions will then grow in complexity with business logic sub-functions
9. Anatomy of a Lambda functionImport sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Business logic sub-functions
10. Anatomy of a Lambda functionImport sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration information, common helper functions
Common helper functions
Business logic sub-functions
11. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Anatomy of a serverless application
Amazon API
Gateway
/orders
/forums
/search
/lists
/user
/...
Now assume that I
have an API based
workload
12. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Anatomy of a serverless application
Databases/
Data stores
AWS Secrets
Manager / AWS
Parameter Store
It needs to read/write
to a DB and get keys
and configuration from
external services
13. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Anatomy of a serverless application
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Amazon API
Gateway
/orders
/forums
/search
/lists
/user
/...
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Databases/
Data stores
AWS Secrets
Manager / AWS
Parameter Store
14. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Anatomy of a serverless application
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Amazon API
Gateway
/orders
/forums
/search
/lists
/user
/...
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Pre-handler-secret-getter() {
}
Function Pre-handler-db-connect(){
}
Function subFunctionA(thing){
## logic here
}
Function subFunctionA(thing){
## logic here
}
Dependencies, configuration
information, common helper
functions
Common helper functions
Business logic sub-functions
Databases/
Data stores
AWS Secrets
Manager / AWS
Parameter Store
There could be a lot of
duplicated code here!
15. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Anatomy of a serverless application
Amazon API
Gateway
/orders
/forums
/search
/lists
/user
/...
Databases/
Data stores
AWS Secrets
Manager / AWS
Parameter Store
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function
Import sdk
Import http-lib
Import ham-sandwich
Pre-handler-secret-getter()
Pre-handler-db-connect()
Function myhandler(event, context) {
<Event handling logic> {
result = SubfunctionA()
}else {
result = SubfunctionB()
return result;
}
Function
Dependencies, configuration information,
common helper functions
Common helper functions
Business logic sub-functions
We want something
more like this:
16. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Layers
Lets functions easily share code: Upload layer
once, reference within any function
Layer can be anything: dependencies, training
data, configuration files, etc
Promote separation of responsibilities, lets
developers iterate faster on writing business logic
Built in support for secure sharing by ecosystem
17. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Using Lambda Layers
• Put common components in a ZIP file and
upload it as a Lambda Layer
• Layers are immutable and can be versioned
to manage updates
• When a version is deleted or permissions to
use it are revoked, functions that used it
previously will continue to work, but you won’t
be able to create new ones
• You can reference up to five layers, one of
which can optionally be a custom runtime
Lambda
Layers
arn:aws:lambda:region:accountId:layer:shared-lib :1
Lambda
Layers
arn:aws:lambda:region:accountId:layer:shared-lib:2
Lambda
Layers
arn:aws:lambda:region:accountId:layer:shared-lib:3
18. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
How Lambda Layers Work
• Order is important because each layer is a ZIP file, and they are all
extracted in the same path
– /opt
– Each layer can potentially overwrite the previous one
• This approach can be used to customize the environment
– For example, the first layer can be a custom runtime and the second layer
adds specific versions of the libraries you need
• The storage of your Lambda Layers takes part in the AWS
Lambda Function storage per region limit (75GB)
19. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Including Library Dependencies in a Layer
Runtime Folders
Node.js
nodejs/node_modules
nodejs/node8/node_modules (NODE_PATH)
Python
python
python/lib/python3.7/site-packages (site directories)
Java java/lib (CLASSPATH)
Ruby
ruby/gems/2.5.0 (GEM_PATH)
ruby/lib (RUBY_LIB)
All
bin (PATH)
lib (LD_LIBRARY_PATH)
20. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Layers Permissions
• Layers can be used within
– An AWS account
– Shared between accounts
– Shared publicly with the broad developer community
• AWS published a public layer which includes NumPy and SciPy, two
popular scientific libraries for Python
– This prebuilt and optimized layer can help you start very quickly with
data processing and machine learning applications
• More from AWS coming!
21. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
DEMO!
22. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
23. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
AWS Lambda wants to make
it easy for developers to get
started writing applications
without needing to worry
about installing software on
servers or in containers
24. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Managing the language
runtimes for our
customers helps make it
easier to do this!
25. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Currently supported languages and versions
• Node.js 6.10
• Node.js 8.10
• Python 2.7
• Python 3.6
• Python 3.7
• Go 1.x
• Ruby 2.5
• Java 8
• .Net Core 1.0 (C#)
• .Net Core 2.0 (C#)
• .Net Core 2.1
(C#/PowerShell)
26. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Currently supported languages and versions
• Node.js 6.10
• Node.js 8.10
• Python 2.7
• Python 3.6
• Python 3.7
• Go 1.x
• Ruby 2.5
• Java 8
• .Net Core 1.0 (C#)
• .Net Core 2.0 (C#)
• .Net Core 2.1
(C#/PowerShell)
These represent the majority of what our customers
are writing applications in today...
27. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
But there’s a long longtail of other languages..
• The Stack Overflow “Developer Survey” from
2018 mentioned 25 different languages
(without versions) used by developers.
• https://insights.stackoverflow.com/survey/2018
• GitHub’s 2018 “State of the Octoverse” report
lists a top 10 as well as top 10 growing
languages including languages such as: Rust,
Groovy, and Kotlin.
• https://octoverse.github.com/projects
From Stack Overflow
Developer Survey 2018
28. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
But there’s a long longtail of other languages..
• The Stack Overflow “Developer Survey” from
2018 mentioned 25 different languages
(without versions) used by developers.
• https://insights.stackoverflow.com/survey/2018
• GitHub’s 2018 “State of the Octoverse” report
lists a top 10 as well as top 10 growing
languages including languages such as: Rust,
Groovy, and Kotlin.
• https://octoverse.github.com/projects
From Stack Overflow
Developer Survey 2018
Customers care about these, and we care about
you.
29. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Custom Runtimes
Bring any Linux compatible language runtime
Powered by new Runtime API - Codifies the
runtime calling conventions and integration
points
At launch, custom runtimes powering Ruby
support in AWS Lambda, more runtimes from
partners (like Erlang)
Custom runtimes distributed as “layers”
Rule
Stack
30. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Runtime API
• You can now select a custom runtime in the console (provided in the
API/SDKs/CLI) as the runtime of a Lambda function
• With this selection, the function must include (in its code or in a layer)
an executable file called bootstrap
• The runtime bootstrap is responsible for the communication between your
code and the Lambda environment
• Your code can use any programming language
31. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Runtime Bootstrap
• The bootstrap executable acts as a bridge
between the Runtime HTTP API and the
Function to be executed
• Bootstrap needs to manage response/error
handling, context creation and function
execution
• Information on the interface endpoint and
the function handler are shared as
environment variables
/runtime API
/invocation/next
/init/error /ID/error
/invocation/ID/response
/invocation/ID/error
bootstrap
Process events/headers
Clean up
Initialize and Invoke function
Response/Error handling
Lambda
Function
32. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Runtime API - Runtime Interactions
• POST /runtime/init/error
• GET /runtime/invocation/next
– The invocation-id is in the Lambda-Runtime-Aws-Request-Id header
– For AWS X-Ray there is a tracing header Lambda-Runtime-Trace-Id
• POST /runtime/invocation/{invocation-id}/response
• POST /runtime/invocation/{invocation-id}/error
• Sample Endpoint + Resource Path
– http://${AWS_LAMBDA_RUNTIME_API}/2018-06-
01/runtime/invocation/next
33. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Runtime API - Processing Tasks
1. Get an event
2. Propagate tracing ID
3. Create a context object
4. Invoke the function handler
5. Handle the response
6. Handle errors
7. Cleanup
34. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Lambda Runtime API - Environment Variables
• AWS_LAMBDA_RUNTIME_API
– HOSTNAME:PORT
• _HANDLER
– SCRIPT_NAME.FUNCTION_NAME
• LAMBDA_TASK_ROOT
– The directory that contains the function code
35. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Using Custom Runtimes
• For the execution of your code, you can use anything that can run in
the Lambda execution environment.
– For example, you can bring an interpreter for the programming language of
your choice.
• You only need to know how the Runtime API works if you want to
manage or publish your own runtimes
• As a developer, you can quickly use runtimes that are shared with you
as layers
– Custom runtimes can be shared as layers so that developers can pick them
up and use their favorite programming language when authoring Lambda
functions
36. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
New: Open Source Runtimes
AWS OPEN SOURCE PARTNER SUPPORTED
o f f e r e d b y
o f f e r e d b y
o f f e r e d b y
o f f e r e d b y
Can find more here: https://github.com/mthenw/awesome-layers !
37. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Stackery’s PHP Lambda Layer
38. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
DEMO!
39. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
aws.amazon.com/serverless
40. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Chris Munns
munns@amazon.com
@chrismunnshttps://www.flickr.com/photos/theredproject/3302110152/
41. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
?
https://secure.flickr.com/photos/dullhunk/202872717/