Mais conteúdo relacionado Semelhante a Lambda Layers & Runtime API (20) Mais de Amazon Web Services (20) Lambda Layers & Runtime API2. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Layers & Runtime API
Kwunhok Chan
Solutions Architect
Amazon Web Services
3. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
4. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Agenda
Lambda Layers
Lambda Runtime API
Lamb
5. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
6. Photo by Thibault Mokuenko on Unsplash
A way to centrally manage code and data
that is shared across multiple functions
7. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Layers Use Cases
• Custom code, that is used by more than one function
• Libraries, modules, frameworks to simplify the implementation of your
business logic
8. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Layers Benefits
• Enforce separation of concerns, between dependencies and your
custom business logic
• Make your function code smaller and more focused on what you want
to build
• Speed up deployments, because less code must be packaged and
uploaded, and dependencies can be reused
9. © 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 can be versioned to manage updates
• Each version is immutable
• 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
10. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Using Lambda Layers
• In the configuration of a function, you can reference up to five layers
• One of which can optionally be a custom runtime
• When the function is invoked, layers are installed in the execution
environment in the order you provided
• The overall, uncompressed size of function and layers is subject to the usual unzipped
deployment package size limit (256MB)
11. © 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)
12. © 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 (LB_LIBRARY_PATH)
13. © 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 is publishing 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
14. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Layers API
• Updated
• CreateFunction
• UpdateFunctionConfiguration
• New
• ListLayers
• ListLayerVersions
• PublishLayerVersion
• DeleteLayerVersion
• GetLayerVersion
• GetLayerVersionPolicy
• AddLayerVersionPermission
• RemoveLayerVersionPermission
15. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Layers in the AWS Serverless Application Model (SAM)
AWSTemplateFormatVersion: '2010-09-09’
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template using Layers
Globals:
Function:
Timeout: 600
Resources:
OneLayerVersionServerlessFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.one_layer_hanlder
Runtime: python3.6
CodeUri: hello_world
Layers:
- <LayerOneVersionArn>
- <LayerTwoVersionArn>
16. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Layers in the AWS Serverless Application Model (SAM)
Resources:
MyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Description: Layer description
ContentUri: 's3://my-bucket/my-layer.zip’
CompatibleRuntimes:
- nodejs6.10
- nodejs8.10
LicenseInfo: 'Available under the MIT-0 license.’
RetentionPolicy: Retain
17. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
18. Photo by Jeremy Lapak on Unsplash
A simple interface to use
any programming
language, or a specific
language version, for
developing your
functions
19. © 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
20. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Runtime Boostrap
• The runtime bootstrap uses a simple HTTP based interface to
• get the event payload for a new invocation and
• return back the response from the function
• Information on the interface endpoint and the function handler are
shared as environment variables
21. © 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
22. © 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
23. © 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
24. © 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
25. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Open Source Runtimes
• C++ (AWS)
• Rust (AWS)
• Erlang (AlertLogic)
• Elixir (AlertLogic)
• Cobol (Blu Age)
• Node.js (NodeSource N|Solid)
• PHP (Stackery)
https://github.com/mthenw
/awesome-layers
26. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
27. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What we will be doing
• PHP
• Creating a PHP function
• Using Stackery layers
• Creating a Custom Runtime for… Bash functions!
• Bootstrap
• hello.sh
https://github.com/chankh/lambda-layers-workshop
29. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
31. Thank you!
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Kwunhok Chan
khchan@amazon.com
@kwunhok