Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

re:Invent Deep Dive on Lambda Layers and Runtime API

973 views

Published on

This presentation was given at AWS Builders' Days in December 2018

  • Login to see the comments

re:Invent Deep Dive on Lambda Layers and Runtime API

  1. 1. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Layers and Runtime API Danilo Poccia Principal Evangelist, Serverless @danilop
  2. 2. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Agenda Lambda Layers Lambda Runtime API Demo
  3. 3. Lambda Layers
  4. 4. Photo by Thibault Mokuenko on Unsplash A way to centrally manage code and data that is shared across multiple functions
  5. 5. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Layers Use Cases Custom code that is used by multiple functions, Partner libraries, etc. Shared code that doesn’t change frequently Layers can be shared across Accounts, publicly with any Account and all Accounts within an Organization Lambda Layers Lambda Function Lambda Function Lambda Layers Account A Account B Same-account Layers Lambda Layers Icon made by Freepik from www.flaticon.com
  6. 6. © 2018, 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 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 Icon made by Freepik from www.flaticon.com
  7. 7. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Using Lambda Layers 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 (250MB) Lambda Layers Lambda Function Lambda Layers Lambda Layers Lambda Layers Lambda Layers Up to 5 Layers /opt NodeJS v11.X provided runtime /opt Node runtime + node_modules
  8. 8. © 2018, 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)
  9. 9. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Layers API New APIs • ListLayers • ListLayerVersions • PublishLayerVersion • DeleteLayerVersion • GetLayerVersion • GetLayerVersionPolicy • AddLayerVersionPermission • RemoveLayerVersionPermission Updated • CreateFunction • UpdateFunctionConfiguration docs.aws.amazon.com/lambda/latest/dg/API_Reference.html
  10. 10. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SAM support BashRuntime: Type: AWS::Serverless::LayerVersion Properties: LayerName: bash-sam Description: Bash Runtime FWIW ContentUri: bash_runtime LicenseInfo: 'MIT-0 license’ RetentionPolicy: Retain HelloBashFunction: Type: AWS::Serverless::Function Properties: CodeUri: bash_hello Handler: hello.handler Runtime: provided Layers: - !Ref BashRuntime - <LayerTwoArn> New AWS::Serverless::LayerVersion resource creates new Lambda Layer from local filesystem or S3 URI Layers can also be restricted to certain Runtimes (CompatibleRuntimes property) SAM CLI 0.8.1+ supports Layers locally and remotely (ARN)
  11. 11. Lambda Runtime API
  12. 12. Photo by Jeremy Lapak on Unsplash A simple interface to use any programming language, or a specific language version, for developing your functions
  13. 13. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Runtime API Select a custom runtime in the console (provided in the API/SDKs/CLI) as the runtime of a Lambda function 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 Bash FWIW docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html
  14. 14. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Runtime Boostrap The bootstrap executable act 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
  15. 15. © 2018, 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
  16. 16. © 2018, 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 More info and OpenAPI specification available: docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
  17. 17. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Open Source Runtimes already available C++ (AWS) Rust (AWS) Nim (LambCI) Erlang (Alert Logic) Elixir (Alert Logic) Cobol (Blu Age) Node.js (NodeSource N|Solid) PHP (Stackery) github.com/mthenw/awesome-layers Check this out for more links
  18. 18. Demo
  19. 19. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Layers Demo • Node.js • Creating a layer with moment.js, request, and request-promise • Using the layer • Python • SciPy & NumPy layer example • Adding a matplotlib layer
  20. 20. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Runtime API Demo • Creating a Custom Runtime for… Bash functions! • Bootstrap • hello.sh
  21. 21. Photo by Christoph Krichenbauer on Unsplash What are you going to build?
  22. 22. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Thank you! @danilop

×