SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Layers & Runtime API
Kwunhok Chan
Solutions Architect
Amazon Web Services
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Agenda
Lambda Layers
Lambda Runtime API
Lamb
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Photo by Thibault Mokuenko on Unsplash
A way to centrally manage code and data
that is shared across multiple functions
© 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
© 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
© 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
© 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)
© 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)
© 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)
© 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
© 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
© 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>
© 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
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Photo by Jeremy Lapak on Unsplash
A simple interface to use
any programming
language, or a specific
language version, for
developing your
functions
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 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
Photo by Christoph Krichenbauer on Unsplash
What are you going to build?
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
http://bit.ly/2REx0S6
Thank you!
© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Kwunhok Chan
khchan@amazon.com
@kwunhok

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
 
Costruisci e distribuisci applicazioni web moderne con AWS Amplify Console
Costruisci e distribuisci applicazioni web moderne con AWS Amplify ConsoleCostruisci e distribuisci applicazioni web moderne con AWS Amplify Console
Costruisci e distribuisci applicazioni web moderne con AWS Amplify Console
 
Unlocking Agility with the AWS Serverless Application Model (SAM) - AWS Summi...
Unlocking Agility with the AWS Serverless Application Model (SAM) - AWS Summi...Unlocking Agility with the AWS Serverless Application Model (SAM) - AWS Summi...
Unlocking Agility with the AWS Serverless Application Model (SAM) - AWS Summi...
 
Networking Best Practices for Your Serverless Applications
Networking Best Practices for Your Serverless ApplicationsNetworking Best Practices for Your Serverless Applications
Networking Best Practices for Your Serverless Applications
 
"Automating AWS Infrastructure with PowerShell", Martin Beeby, AWS Dev Day Ky...
"Automating AWS Infrastructure with PowerShell", Martin Beeby, AWS Dev Day Ky..."Automating AWS Infrastructure with PowerShell", Martin Beeby, AWS Dev Day Ky...
"Automating AWS Infrastructure with PowerShell", Martin Beeby, AWS Dev Day Ky...
 
Deep Dive on Serverless Application Development
Deep Dive on Serverless Application DevelopmentDeep Dive on Serverless Application Development
Deep Dive on Serverless Application Development
 
AWS ECS Workshop A Journey to Modern Applications
AWS ECS Workshop A Journey to Modern ApplicationsAWS ECS Workshop A Journey to Modern Applications
AWS ECS Workshop A Journey to Modern Applications
 
Serverless is dead.
Serverless is dead.Serverless is dead.
Serverless is dead.
 
Unlocking Agility with the AWS Serverless Application Model (SAM)
Unlocking Agility with the AWS Serverless Application Model (SAM)Unlocking Agility with the AWS Serverless Application Model (SAM)
Unlocking Agility with the AWS Serverless Application Model (SAM)
 
Serverless Architectures.pdf
Serverless Architectures.pdfServerless Architectures.pdf
Serverless Architectures.pdf
 
Practical Guidance for Increasing your Serverless Application's Security
Practical Guidance for Increasing your Serverless Application's SecurityPractical Guidance for Increasing your Serverless Application's Security
Practical Guidance for Increasing your Serverless Application's Security
 
Build and Deploy Serverless Applications with AWS SAM - SRV316 - Chicago AWS ...
Build and Deploy Serverless Applications with AWS SAM - SRV316 - Chicago AWS ...Build and Deploy Serverless Applications with AWS SAM - SRV316 - Chicago AWS ...
Build and Deploy Serverless Applications with AWS SAM - SRV316 - Chicago AWS ...
 
Getting Started with AWS Lambda and Serverless Computing
Getting Started with AWS Lambda and Serverless ComputingGetting Started with AWS Lambda and Serverless Computing
Getting Started with AWS Lambda and Serverless Computing
 
Deep Dive on Amazon Elastic Container Service (ECS) | AWS Summit Tel Aviv 2019
Deep Dive on Amazon Elastic Container Service (ECS)  | AWS Summit Tel Aviv 2019Deep Dive on Amazon Elastic Container Service (ECS)  | AWS Summit Tel Aviv 2019
Deep Dive on Amazon Elastic Container Service (ECS) | AWS Summit Tel Aviv 2019
 
Building CICD Pipelines for Serverless Applications
Building CICD Pipelines for Serverless ApplicationsBuilding CICD Pipelines for Serverless Applications
Building CICD Pipelines for Serverless Applications
 
Introduction to AWS Lambda and Serverless Applications
Introduction to AWS Lambda and Serverless ApplicationsIntroduction to AWS Lambda and Serverless Applications
Introduction to AWS Lambda and Serverless Applications
 
Serverless-AWS SAM CLI Session: Developer Meet Up
Serverless-AWS SAM CLI Session: Developer Meet UpServerless-AWS SAM CLI Session: Developer Meet Up
Serverless-AWS SAM CLI Session: Developer Meet Up
 
Build a Serverless Web Application in One Day
Build a Serverless Web Application in One DayBuild a Serverless Web Application in One Day
Build a Serverless Web Application in One Day
 
Building serverless applications with Amazon S3
Building serverless applications with Amazon S3Building serverless applications with Amazon S3
Building serverless applications with Amazon S3
 
Local Testing and Deployment Best Practices for Serverless Applications - AWS...
Local Testing and Deployment Best Practices for Serverless Applications - AWS...Local Testing and Deployment Best Practices for Serverless Applications - AWS...
Local Testing and Deployment Best Practices for Serverless Applications - AWS...
 

Semelhante a Lambda Layers & Runtime API

What's New in AWS Serverless and Containers
What's New in AWS Serverless and ContainersWhat's New in AWS Serverless and Containers
What's New in AWS Serverless and Containers
Amazon Web Services
 

Semelhante a Lambda Layers & Runtime API (20)

[NEW LAUNCH!] Lambda Layers (SRV375) - AWS re:Invent 2018
[NEW LAUNCH!] Lambda Layers (SRV375) - AWS re:Invent 2018[NEW LAUNCH!] Lambda Layers (SRV375) - AWS re:Invent 2018
[NEW LAUNCH!] Lambda Layers (SRV375) - AWS re:Invent 2018
 
Serverless Functions Deep Dive
Serverless Functions Deep DiveServerless Functions Deep Dive
Serverless Functions Deep Dive
 
Serverless functions deep dive
Serverless functions deep diveServerless functions deep dive
Serverless functions deep dive
 
Securing serverless and container services - SDD306 - AWS re:Inforce 2019
Securing serverless and container services - SDD306 - AWS re:Inforce 2019 Securing serverless and container services - SDD306 - AWS re:Inforce 2019
Securing serverless and container services - SDD306 - AWS re:Inforce 2019
 
Devops on serverless
Devops on serverlessDevops on serverless
Devops on serverless
 
re:Invent Deep Dive on Lambda Layers and Runtime API
re:Invent Deep Dive on Lambda Layers and Runtime APIre:Invent Deep Dive on Lambda Layers and Runtime API
re:Invent Deep Dive on Lambda Layers and Runtime API
 
What's New in AWS Serverless and Containers
What's New in AWS Serverless and ContainersWhat's New in AWS Serverless and Containers
What's New in AWS Serverless and Containers
 
Twelve-Factor serverless applications - MAD307 - New York AWS Summit
Twelve-Factor serverless applications - MAD307 - New York AWS SummitTwelve-Factor serverless applications - MAD307 - New York AWS Summit
Twelve-Factor serverless applications - MAD307 - New York AWS Summit
 
Serverless Computing: build and run applications without thinking about servers
Serverless Computing: build and run applications without thinking about serversServerless Computing: build and run applications without thinking about servers
Serverless Computing: build and run applications without thinking about servers
 
Serverless computing - Build and run applications without thinking about servers
Serverless computing - Build and run applications without thinking about serversServerless computing - Build and run applications without thinking about servers
Serverless computing - Build and run applications without thinking about servers
 
Serverless use cases with AWS Lambda - More Serverless Event
Serverless use cases with AWS Lambda - More Serverless EventServerless use cases with AWS Lambda - More Serverless Event
Serverless use cases with AWS Lambda - More Serverless Event
 
Twelve-Factor serverless applications - MAD311 - Chicago AWS Summit
Twelve-Factor serverless applications - MAD311 - Chicago AWS SummitTwelve-Factor serverless applications - MAD311 - Chicago AWS Summit
Twelve-Factor serverless applications - MAD311 - Chicago AWS Summit
 
Build and Deploy Serverless Applications with AWS SAM
Build and Deploy Serverless Applications with AWS SAM Build and Deploy Serverless Applications with AWS SAM
Build and Deploy Serverless Applications with AWS SAM
 
SMC302 Building Serverless Web Applications
SMC302 Building Serverless Web ApplicationsSMC302 Building Serverless Web Applications
SMC302 Building Serverless Web Applications
 
Twelve-factor serverless applications - MAD302 - Santa Clara AWS Summit
Twelve-factor serverless applications - MAD302 - Santa Clara AWS SummitTwelve-factor serverless applications - MAD302 - Santa Clara AWS Summit
Twelve-factor serverless applications - MAD302 - Santa Clara AWS Summit
 
Building Serverless Web Applications - May 2017 AWS Online Tech Talks
Building Serverless Web Applications  - May 2017 AWS Online Tech TalksBuilding Serverless Web Applications  - May 2017 AWS Online Tech Talks
Building Serverless Web Applications - May 2017 AWS Online Tech Talks
 
Building Serverless Web Applications - May 2017 AWS Online Tech Talks
Building Serverless Web Applications - May 2017 AWS Online Tech TalksBuilding Serverless Web Applications - May 2017 AWS Online Tech Talks
Building Serverless Web Applications - May 2017 AWS Online Tech Talks
 
AWSomeDay Zurich 2018 - How to go serverless
AWSomeDay Zurich 2018 - How to go serverless AWSomeDay Zurich 2018 - How to go serverless
AWSomeDay Zurich 2018 - How to go serverless
 
Serverless Architectural Patterns: Collision 2018
Serverless Architectural Patterns: Collision 2018Serverless Architectural Patterns: Collision 2018
Serverless Architectural Patterns: Collision 2018
 
Serverless workshop with Amazon Web Services
Serverless workshop with Amazon Web ServicesServerless workshop with Amazon Web Services
Serverless workshop with Amazon Web Services
 

Mais de Amazon Web Services

Tools for building your MVP on AWS
Tools for building your MVP on AWSTools for building your MVP on AWS
Tools for building your MVP on AWS
Amazon Web Services
 
How to Build a Winning Pitch Deck
How to Build a Winning Pitch DeckHow to Build a Winning Pitch Deck
How to Build a Winning Pitch Deck
Amazon Web Services
 
Building a web application without servers
Building a web application without serversBuilding a web application without servers
Building a web application without servers
Amazon Web Services
 
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
Amazon Web Services
 

Mais de Amazon Web Services (20)

Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...
Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...
Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...
 
Big Data per le Startup: come creare applicazioni Big Data in modalità Server...
Big Data per le Startup: come creare applicazioni Big Data in modalità Server...Big Data per le Startup: come creare applicazioni Big Data in modalità Server...
Big Data per le Startup: come creare applicazioni Big Data in modalità Server...
 
Esegui pod serverless con Amazon EKS e AWS Fargate
Esegui pod serverless con Amazon EKS e AWS FargateEsegui pod serverless con Amazon EKS e AWS Fargate
Esegui pod serverless con Amazon EKS e AWS Fargate
 
Costruire Applicazioni Moderne con AWS
Costruire Applicazioni Moderne con AWSCostruire Applicazioni Moderne con AWS
Costruire Applicazioni Moderne con AWS
 
Come spendere fino al 90% in meno con i container e le istanze spot
Come spendere fino al 90% in meno con i container e le istanze spot Come spendere fino al 90% in meno con i container e le istanze spot
Come spendere fino al 90% in meno con i container e le istanze spot
 
Open banking as a service
Open banking as a serviceOpen banking as a service
Open banking as a service
 
Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...
Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...
Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...
 
OpsWorks Configuration Management: automatizza la gestione e i deployment del...
OpsWorks Configuration Management: automatizza la gestione e i deployment del...OpsWorks Configuration Management: automatizza la gestione e i deployment del...
OpsWorks Configuration Management: automatizza la gestione e i deployment del...
 
Microsoft Active Directory su AWS per supportare i tuoi Windows Workloads
Microsoft Active Directory su AWS per supportare i tuoi Windows WorkloadsMicrosoft Active Directory su AWS per supportare i tuoi Windows Workloads
Microsoft Active Directory su AWS per supportare i tuoi Windows Workloads
 
Computer Vision con AWS
Computer Vision con AWSComputer Vision con AWS
Computer Vision con AWS
 
Database Oracle e VMware Cloud on AWS i miti da sfatare
Database Oracle e VMware Cloud on AWS i miti da sfatareDatabase Oracle e VMware Cloud on AWS i miti da sfatare
Database Oracle e VMware Cloud on AWS i miti da sfatare
 
Crea la tua prima serverless ledger-based app con QLDB e NodeJS
Crea la tua prima serverless ledger-based app con QLDB e NodeJSCrea la tua prima serverless ledger-based app con QLDB e NodeJS
Crea la tua prima serverless ledger-based app con QLDB e NodeJS
 
API moderne real-time per applicazioni mobili e web
API moderne real-time per applicazioni mobili e webAPI moderne real-time per applicazioni mobili e web
API moderne real-time per applicazioni mobili e web
 
Database Oracle e VMware Cloud™ on AWS: i miti da sfatare
Database Oracle e VMware Cloud™ on AWS: i miti da sfatareDatabase Oracle e VMware Cloud™ on AWS: i miti da sfatare
Database Oracle e VMware Cloud™ on AWS: i miti da sfatare
 
Tools for building your MVP on AWS
Tools for building your MVP on AWSTools for building your MVP on AWS
Tools for building your MVP on AWS
 
How to Build a Winning Pitch Deck
How to Build a Winning Pitch DeckHow to Build a Winning Pitch Deck
How to Build a Winning Pitch Deck
 
Building a web application without servers
Building a web application without serversBuilding a web application without servers
Building a web application without servers
 
Fundraising Essentials
Fundraising EssentialsFundraising Essentials
Fundraising Essentials
 
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
 
Introduzione a Amazon Elastic Container Service
Introduzione a Amazon Elastic Container ServiceIntroduzione a Amazon Elastic Container Service
Introduzione a Amazon Elastic Container Service
 

Lambda Layers & Runtime API

  • 1.
  • 2. © 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
  • 28. Photo by Christoph Krichenbauer on Unsplash What are you going to build?
  • 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