SlideShare a Scribd company logo
1 of 34
Download to read offline
AWS Lambda: Best Practices
and Common Mistakes
Given by Derek C. Ashmore
Chicago Cloud Conference
September 21, 2020
©2020 Derek C. Ashmore, All Rights Reserved 1
Who am I?
• Professional Geek
since 1987
• Java/J2EE/Java EE
since 1999
• AWS since 2010
• Azure since 2017
• Specialties
• Cloud
Workshops
• Cloud-native
Applications
• Yes – I still code!
©2020 Derek C. Ashmore, All Rights Reserved 2
Discussion Resources
• This slide deck
– https://www.slideshare.net/derekashmore/presentations
• Sample code on my Github
– https://github.com/Derek-Ashmore/
• Slide deck has hyper-links!
– Don’t bother writing down URLs
©2020 Derek C. Ashmore, All Rights Reserved 3
Agenda
The “What”
and “Why”
of AWS
Lambda
Code-Level
Tips
Operation
and Design
Habits
Summary /
Q&A
© 2020 Derek C. Ashmore, All Rights Reserved 4
What are AWS Lambdas?
• You provide custom code -> AWS runs it
– Java, Node.js, Python, Go, Ruby, .Net Core
– Can implement custom runtimes for languages not in the list!
• Computing power with less management
– AWS manages the hardware
– AWS autoscales that hardware
– AWS maintains that hardware
• Lambdas are event driven
– API Gateway (e.g. RESTful Web Service call)
– Many more
• Lambdas are stateless
• Not to be confused with “Lambda Expressions” in Java 8
© 2020 Derek C. Ashmore, All Rights Reserved 5
Lambda Event Sources
• API Gateway
• SNS Messaging
Subscriptions
• Schedule
• Storage writes
– S3, DynamoDB, Kenesis
© 2020 Derek C. Ashmore, All Rights Reserved 6
• SES Email receipt
• Cloudwatch
– Schedule, Events, log entries
• Cognito (Security)
• CloudFormation
– Creation script
What’s the Business Benefit
• Less Maintenance Hassle
• Unlimited Parallelism
• Current cost advantage
– Don’t pay for idle
– CPU cost currently lower
• Free tier
– 1 M executions and 400K compute seconds per month
– Memory allocated determines allowed free-tier runtime
• 20 cents per 1 M executions + memory/runtime cost
– Administration cost
• No O/S upgrades, server backups, etc.
© 2020 Derek C. Ashmore, All Rights Reserved 7
There’s no free lunch
• Less control over environment
– Harder to tune
– Memory and time limits on execution
• Few Environment amenities
– No connection pooling, session support, caching
• Proprietary Interface
– Potential Technical Lock-in
• No Guarantee that AWS cost will be constant
– Potential Business Risk
• Modern version of CGI
© 2020 Derek C. Ashmore, All Rights Reserved 8
Agenda
The “What”
and “Why”
of AWS
Lambda
Code-Level
Tips
Operation
and Design
Habits
Summary /
Q&A
© 2020 Derek C. Ashmore, All Rights Reserved 9
What Makes a “Best Practice”?
• Makes Support Easier
• Increases Reuse
• Increases Performance
• Minimizes Resource Consumption
– Labor
– Runtime
©2018 Derek C. Ashmore, All Rights Reserved 10
Let’s start with Low-Hanging Fruit
© 2020 Derek C. Ashmore, All Rights Reserved 11
Report Inputs/Env on Exception
• Place a Try / Catch in your handler
– Python Example
– Java Example
• Also check your arguments with a clear error message
© 2020 Derek C. Ashmore, All Rights Reserved 12
def crossAccountHandler(event, context):
try:
………………
except Exception as e:
e.args += (event,vars(context))
raise
Check Arguments Up Front
• Check your arguments with a clear error message
– Python Example
– Java Example
© 2020 Derek C. Ashmore, All Rights Reserved 13
def crossAccountHandler(event, context):
try:
if 'Assumed_Role' in event:
…………………
else:
raise Exception('Assumed_Role not provided as argument')
except Exception as e:
Specify Lambda Source Repo
• Explicitly put the source repository name in the Lambda comments
– In most organizations, the repository name isn’t obvious
– Others changing your code need it
– You don’t want source control to be out of date
© 2020 Derek C. Ashmore, All Rights Reserved 14
"""
secretLambda.py
……………
Source Control: https://github.com/Derek-Ashmore/AWSDevOpsUtilities
"""
Separate Lambda from Business Logic
• Make business logic reusable
– Callable by other applications
– Usable on premises
• Easier to locally develop and debug
– Lambda-specific logic is thin!
© 2020 Derek C. Ashmore, All Rights Reserved 15
def startStopHandler(event, context):
try:
executeStopStart(datetime.datetime.now()
, os.getenv('Scheduled_StartTime', ‘’)
, os.getenv('Scheduled_StopTime', ‘’)
, os.getenv('Scheduled_StartStop_Days', 'M,T,W,R,F’))
……………
return 0;
This is low-hanging fruit that will be appreciated by
your fellow developers!
©2018 Derek C. Ashmore, All Rights Reserved 16
• Log All Inputs and Environment on
Exception
• Check all arguments up front
• Document the source repo at the top.
• Repo readme can have other
developer specifics
• Separate Lambda code from business
logic
• Now let’s talk design and operations….
Agenda
The “What”
and “Why”
of AWS
Lambda
Code-Level
Tips
Operation
and Design
Habits
Summary /
Q&A
© 2020 Derek C. Ashmore, All Rights Reserved 17
Automate builds and deployments!
© 2020 Derek C. Ashmore, All Rights Reserved 18
Lambda Copies Everywhere!
• Changes / Bug Fixes need to be deployed everywhere
• Solving with automation solves the wrong problem!
© 2020 Derek C. Ashmore, All Rights Reserved 19
One Copy for All!
• Scalable – only need to add accounts over time
• Bugfixes in one place
• Configuration usually in common DynamoDB table(s)
• Sample in Python here
©2018 Derek C. Ashmore, All Rights Reserved 20
Cross-Account Execution
• Algorithm is
– Assume a remote-account role using STS
• The response has temporary credentials
– Create a session using the remote account creds
– Do work in the remote account
• Example here: Derek-Ashmore/AWSDevOpsUtilities (Github)
© 2020 Derek C. Ashmore, All Rights Reserved 21
For workloads over 15 min
• Executor that invokes lambda asynchronously for
each account
• Sample in Python here
©2018 Derek C. Ashmore, All Rights Reserved 22
Limit Custom Nesting to One Level
• Debugging with nested executions is
– Time consuming and difficult
– Can’t do locally
– Absolutely requires unique correlation id for the entire transaction
• Allows you to tell invocation history for one logical transaction
– Instead of deep custom nesting, use AWS Step Functions
• Use Step Functions if you need more
© 2020 Derek C. Ashmore, All Rights Reserved 23
Nested Calls using AWS Step Functions
• AWS Step Functions
– Uses a State Machine model
• Think turn-style to get access to train
– States are “Locked” and “Unlocked”
– Locked → Payment input allowed, then “Unlocked”
– Unlocked → One person allowed through, then “Locked”
– Automatically provides correlation between invocations
• Unified logs for the entire transaction
– Now supported by X-ray (09/2020)
• Execution time and health per Step Function workflow
© 2020 Derek C. Ashmore, All Rights Reserved 24
Operations and Design Habits
© 2020 Derek C. Ashmore, All Rights Reserved 25
• Automate Builds and Deployments
• Only install Lambda’s Once
• Limit Lambda nesting to One Level
• Step functions if you need more
• Now let’s talk dependencies and secrets
Use Configuration Injection
• No environment specifics hardcoded in the Lambda deployment
• Use Environment Variables on the Lambda Definition
– No un-encrypted secrets (e.g. database password)
• Use Arguments in the triggering event
– No un-encrypted secrets
• Anti-Example
– Splunk forwarding Lambda with hard-coded Splunk channels
© 2020 Derek C. Ashmore, All Rights Reserved 26
Providing Secrets to Lambdas
• Secrets are needed items like credentials of any type.
• Use IAM Roles to grant permission to read secrets
• Options are:
– Use KMS
• Encrypt credential and base64 encode it
– Place encrypted version in environment variable
• Sample Lambda and Encryption Script (here)
– Use a Digital Vault (e.g. AWS Secrets Manager)
• Sample Lambda here
© 2020 Derek C. Ashmore, All Rights Reserved 27
AWS Secrets Manager
• Use IAM Roles to grant
permission to read secrets
• You don’t need a “secret” to
get a “secret”!
© 2020 Derek C. Ashmore, All Rights Reserved 28
Avoid Heavy-Footprint Dependencies
• Minimizes load time
– Mitigates cold-start problem
• Java
– Use Guice over Spring
• Python
– Use AWS provided deps first (list is
here)
© 2020 Derek C. Ashmore, All Rights Reserved 29
Idempotence
• Possible for your Lambda to be invoked multiple times for the same event
– Prevent repeat actions from having a different effect.
• Options
– Record the event id –> Skip repeated events
• Most event sources provide a unique request id
– Lambda invoking lambda does not!
• Negatively affects performance
– 1 extra read
– 1 extra write
• Need to roll-off old events
– Insure that the effect is the same each time
• Not perfect → You don’t control invocation order
© 2020 Derek C. Ashmore, All Rights Reserved 30
Dependency Management
• AWS Layers can be used for
dependency management
– Can upgrade your library
dependencies in one location
• Provides convenience
• Use for custom runtimes!
• Beware of unintended
consequences
– Easies to inadvertently break
published lambdas using layers
© 2020 Derek C. Ashmore, All Rights Reserved 31
Common Mistakes
• Deploying inappropriate workloads as Lambdas
– Install full Java web application as Lambda
• Required keep-alive lambdas to prevent cold-starts
• Not keeping business logic unit-testable
• Creating the distributed monolith
– Lambdas with extensive nested execution
– Lambdas not independently deployable
© 2020 Derek C. Ashmore, All Rights Reserved 32
Further Reading
• This slide deck
– https://www.slideshare.net/derekashmore/presentations
• AWS Lambda Reading List
– http://www.derekashmore.com/2016/04/aws-lambda-reading-list.html
• Amazon’s Published Best Practice List
– https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html
© 2020 Derek C. Ashmore, All Rights Reserved 33
Questions?
• Derek Ashmore:
– Blog: www.derekashmore.com
– LinkedIn: www.linkedin.com/in/derekashmore
• Connect Invites from attendees welcome
– Twitter: https://twitter.com/Derek_Ashmore
– GitHub: https://github.com/Derek-Ashmore
– Book: http://dvtpress.com/
© 2020 Derek C. Ashmore, All Rights Reserved 34

More Related Content

What's hot

SAP Teched 2012 Session Tec3438 Automate IaaS SAP deployments
SAP Teched 2012 Session Tec3438 Automate IaaS SAP deploymentsSAP Teched 2012 Session Tec3438 Automate IaaS SAP deployments
SAP Teched 2012 Session Tec3438 Automate IaaS SAP deploymentsChris Kernaghan
 
Flintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test LabFlintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test LabTechWell
 
Delivering Mobile Apps That Perform
Delivering Mobile Apps That PerformDelivering Mobile Apps That Perform
Delivering Mobile Apps That PerformRuben Goncalves
 
Calculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the CloudCalculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the CloudAcquia
 
Managing Performance in the Cloud
Managing Performance in the CloudManaging Performance in the Cloud
Managing Performance in the CloudDevOpsGroup
 
BOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demandBOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demandDevOps Enterprise Summit
 
Make a Move to the Azure Cloud with SoftNAS
Make a Move to the Azure Cloud with SoftNASMake a Move to the Azure Cloud with SoftNAS
Make a Move to the Azure Cloud with SoftNASBuurst
 
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...DevOps Enterprise Summit
 
Zero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesZero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesAlexander Penev
 
Automatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI PlanningAutomatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI PlanningHiroshi Wada
 
The Rocky Cloud Road
The Rocky Cloud RoadThe Rocky Cloud Road
The Rocky Cloud RoadGert Drapers
 
Embracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at NetflixEmbracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at NetflixJosh Evans
 
Cleaning Out Your IT Closet - SPSRED 2013
Cleaning Out Your IT Closet - SPSRED 2013Cleaning Out Your IT Closet - SPSRED 2013
Cleaning Out Your IT Closet - SPSRED 2013adamtoth
 
[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric Cloud[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric CloudPerforce
 
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...DevOps Enterprise Summit
 
Picnic Software - Developing a flexible and scalable application
Picnic Software - Developing a flexible and scalable applicationPicnic Software - Developing a flexible and scalable application
Picnic Software - Developing a flexible and scalable applicationNick Josevski
 
#NetflixEverywhere Global Architecture
#NetflixEverywhere Global Architecture#NetflixEverywhere Global Architecture
#NetflixEverywhere Global ArchitectureJosh Evans
 
Harvard it summit 2016 - opencast in the cloud at harvard dce- live and on-d...
Harvard it summit 2016  - opencast in the cloud at harvard dce- live and on-d...Harvard it summit 2016  - opencast in the cloud at harvard dce- live and on-d...
Harvard it summit 2016 - opencast in the cloud at harvard dce- live and on-d...kevin_donovan
 
AWS Summit 2013 | India - Running High Churn Development & Test Environments,...
AWS Summit 2013 | India - Running High Churn Development & Test Environments,...AWS Summit 2013 | India - Running High Churn Development & Test Environments,...
AWS Summit 2013 | India - Running High Churn Development & Test Environments,...Amazon Web Services
 

What's hot (20)

SAP Teched 2012 Session Tec3438 Automate IaaS SAP deployments
SAP Teched 2012 Session Tec3438 Automate IaaS SAP deploymentsSAP Teched 2012 Session Tec3438 Automate IaaS SAP deployments
SAP Teched 2012 Session Tec3438 Automate IaaS SAP deployments
 
Flintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test LabFlintstones or Jetsons? Jump Start Your Virtual Test Lab
Flintstones or Jetsons? Jump Start Your Virtual Test Lab
 
Delivering Mobile Apps That Perform
Delivering Mobile Apps That PerformDelivering Mobile Apps That Perform
Delivering Mobile Apps That Perform
 
Calculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the CloudCalculating the Savings of Moving Your Drupal Site to the Cloud
Calculating the Savings of Moving Your Drupal Site to the Cloud
 
Managing Performance in the Cloud
Managing Performance in the CloudManaging Performance in the Cloud
Managing Performance in the Cloud
 
BOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demandBOSE - Josh Steckler - Automating Automation: Build environments, on-demand
BOSE - Josh Steckler - Automating Automation: Build environments, on-demand
 
Make a Move to the Azure Cloud with SoftNAS
Make a Move to the Azure Cloud with SoftNASMake a Move to the Azure Cloud with SoftNAS
Make a Move to the Azure Cloud with SoftNAS
 
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
 
Zero Downtime JEE Architectures
Zero Downtime JEE ArchitecturesZero Downtime JEE Architectures
Zero Downtime JEE Architectures
 
Cloudy with a Chance of Databases
Cloudy with a Chance of DatabasesCloudy with a Chance of Databases
Cloudy with a Chance of Databases
 
Automatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI PlanningAutomatic Undo for Cloud Management via AI Planning
Automatic Undo for Cloud Management via AI Planning
 
The Rocky Cloud Road
The Rocky Cloud RoadThe Rocky Cloud Road
The Rocky Cloud Road
 
Embracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at NetflixEmbracing Failure - Fault Injection and Service Resilience at Netflix
Embracing Failure - Fault Injection and Service Resilience at Netflix
 
Cleaning Out Your IT Closet - SPSRED 2013
Cleaning Out Your IT Closet - SPSRED 2013Cleaning Out Your IT Closet - SPSRED 2013
Cleaning Out Your IT Closet - SPSRED 2013
 
[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric Cloud[India Merge World Tour] Electric Cloud
[India Merge World Tour] Electric Cloud
 
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
Sam Fell - Electric Cloud - Faster Continuous Integration with ElectricAccele...
 
Picnic Software - Developing a flexible and scalable application
Picnic Software - Developing a flexible and scalable applicationPicnic Software - Developing a flexible and scalable application
Picnic Software - Developing a flexible and scalable application
 
#NetflixEverywhere Global Architecture
#NetflixEverywhere Global Architecture#NetflixEverywhere Global Architecture
#NetflixEverywhere Global Architecture
 
Harvard it summit 2016 - opencast in the cloud at harvard dce- live and on-d...
Harvard it summit 2016  - opencast in the cloud at harvard dce- live and on-d...Harvard it summit 2016  - opencast in the cloud at harvard dce- live and on-d...
Harvard it summit 2016 - opencast in the cloud at harvard dce- live and on-d...
 
AWS Summit 2013 | India - Running High Churn Development & Test Environments,...
AWS Summit 2013 | India - Running High Churn Development & Test Environments,...AWS Summit 2013 | India - Running High Churn Development & Test Environments,...
AWS Summit 2013 | India - Running High Churn Development & Test Environments,...
 

Similar to AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2020

AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019Derek Ashmore
 
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019Derek Ashmore
 
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019Derek Ashmore
 
AWS Lambda Deployments: Best Practices and Common Mistakes O'Reilly Software...
AWS Lambda Deployments:  Best Practices and Common Mistakes O'Reilly Software...AWS Lambda Deployments:  Best Practices and Common Mistakes O'Reilly Software...
AWS Lambda Deployments: Best Practices and Common Mistakes O'Reilly Software...Derek Ashmore
 
AWS Community Day - Derek C. Ashmore - AWS Lambda: Best Practices
AWS Community Day  - Derek C. Ashmore - AWS Lambda: Best Practices AWS Community Day  - Derek C. Ashmore - AWS Lambda: Best Practices
AWS Community Day - Derek C. Ashmore - AWS Lambda: Best Practices AWS Chicago
 
AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019
AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019
AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019Derek Ashmore
 
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07Derek Ashmore
 
Aws lambda best practices - ignite - dev opsdays-charlotte
Aws lambda   best practices - ignite - dev opsdays-charlotteAws lambda   best practices - ignite - dev opsdays-charlotte
Aws lambda best practices - ignite - dev opsdays-charlotteDerek Ashmore
 
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02Derek Ashmore
 
Aws Lambda for Java Architects CJug-Chicago 2016-08-30
Aws Lambda for Java Architects CJug-Chicago 2016-08-30Aws Lambda for Java Architects CJug-Chicago 2016-08-30
Aws Lambda for Java Architects CJug-Chicago 2016-08-30Derek Ashmore
 
Aws Lambda for Java Architects - JavaOne -2016-09-19
Aws Lambda for Java Architects - JavaOne -2016-09-19Aws Lambda for Java Architects - JavaOne -2016-09-19
Aws Lambda for Java Architects - JavaOne -2016-09-19Derek Ashmore
 
Aws Lambda for Java Architects - Illinois VJug -2016-05-03
Aws Lambda for Java Architects - Illinois VJug -2016-05-03Aws Lambda for Java Architects - Illinois VJug -2016-05-03
Aws Lambda for Java Architects - Illinois VJug -2016-05-03Derek Ashmore
 
Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...
Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...
Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...Amazon Web Services
 
An Azure Primer for the AWS Solution Architect - DevOps West 2020
An Azure Primer for the AWS Solution Architect - DevOps West 2020An Azure Primer for the AWS Solution Architect - DevOps West 2020
An Azure Primer for the AWS Solution Architect - DevOps West 2020Derek Ashmore
 
Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...
Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...
Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...Amazon Web Services
 
Docker vs. Kubernetes vs. Serverless
Docker vs. Kubernetes vs. ServerlessDocker vs. Kubernetes vs. Serverless
Docker vs. Kubernetes vs. ServerlessLogicworksNY
 
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 Roman Plessl
 
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018Derek Ashmore
 
Serverless design considerations for Cloud Native workloads
Serverless design considerations for Cloud Native workloadsServerless design considerations for Cloud Native workloads
Serverless design considerations for Cloud Native workloadsTensult
 
What are DevOps Application Patterns on AWS…and why do I need them?
What are DevOps Application Patterns on AWS…and why do I need them?What are DevOps Application Patterns on AWS…and why do I need them?
What are DevOps Application Patterns on AWS…and why do I need them?DevOps.com
 

Similar to AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2020 (20)

AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
 
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
 
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
AWS Lambda: Best Practices and Common Mistakes - Dev Ops West 2019
 
AWS Lambda Deployments: Best Practices and Common Mistakes O'Reilly Software...
AWS Lambda Deployments:  Best Practices and Common Mistakes O'Reilly Software...AWS Lambda Deployments:  Best Practices and Common Mistakes O'Reilly Software...
AWS Lambda Deployments: Best Practices and Common Mistakes O'Reilly Software...
 
AWS Community Day - Derek C. Ashmore - AWS Lambda: Best Practices
AWS Community Day  - Derek C. Ashmore - AWS Lambda: Best Practices AWS Community Day  - Derek C. Ashmore - AWS Lambda: Best Practices
AWS Community Day - Derek C. Ashmore - AWS Lambda: Best Practices
 
AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019
AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019
AWS Lambda: Best Practices and Common Mistakes - AWS Community Days 2019
 
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
 
Aws lambda best practices - ignite - dev opsdays-charlotte
Aws lambda   best practices - ignite - dev opsdays-charlotteAws lambda   best practices - ignite - dev opsdays-charlotte
Aws lambda best practices - ignite - dev opsdays-charlotte
 
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
 
Aws Lambda for Java Architects CJug-Chicago 2016-08-30
Aws Lambda for Java Architects CJug-Chicago 2016-08-30Aws Lambda for Java Architects CJug-Chicago 2016-08-30
Aws Lambda for Java Architects CJug-Chicago 2016-08-30
 
Aws Lambda for Java Architects - JavaOne -2016-09-19
Aws Lambda for Java Architects - JavaOne -2016-09-19Aws Lambda for Java Architects - JavaOne -2016-09-19
Aws Lambda for Java Architects - JavaOne -2016-09-19
 
Aws Lambda for Java Architects - Illinois VJug -2016-05-03
Aws Lambda for Java Architects - Illinois VJug -2016-05-03Aws Lambda for Java Architects - Illinois VJug -2016-05-03
Aws Lambda for Java Architects - Illinois VJug -2016-05-03
 
Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...
Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...
Infrastructure Is Code with the AWS Cloud Development Kit (DEV372) - AWS re:I...
 
An Azure Primer for the AWS Solution Architect - DevOps West 2020
An Azure Primer for the AWS Solution Architect - DevOps West 2020An Azure Primer for the AWS Solution Architect - DevOps West 2020
An Azure Primer for the AWS Solution Architect - DevOps West 2020
 
Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...
Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...
Get the EDGE to scale: Using Cloudfront along with edge compute to scale your...
 
Docker vs. Kubernetes vs. Serverless
Docker vs. Kubernetes vs. ServerlessDocker vs. Kubernetes vs. Serverless
Docker vs. Kubernetes vs. Serverless
 
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
 
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
Microservices with Terraform, Docker and the Cloud. DevOps Wet 2018
 
Serverless design considerations for Cloud Native workloads
Serverless design considerations for Cloud Native workloadsServerless design considerations for Cloud Native workloads
Serverless design considerations for Cloud Native workloads
 
What are DevOps Application Patterns on AWS…and why do I need them?
What are DevOps Application Patterns on AWS…and why do I need them?What are DevOps Application Patterns on AWS…and why do I need them?
What are DevOps Application Patterns on AWS…and why do I need them?
 

Recently uploaded

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2020

  • 1. AWS Lambda: Best Practices and Common Mistakes Given by Derek C. Ashmore Chicago Cloud Conference September 21, 2020 ©2020 Derek C. Ashmore, All Rights Reserved 1
  • 2. Who am I? • Professional Geek since 1987 • Java/J2EE/Java EE since 1999 • AWS since 2010 • Azure since 2017 • Specialties • Cloud Workshops • Cloud-native Applications • Yes – I still code! ©2020 Derek C. Ashmore, All Rights Reserved 2
  • 3. Discussion Resources • This slide deck – https://www.slideshare.net/derekashmore/presentations • Sample code on my Github – https://github.com/Derek-Ashmore/ • Slide deck has hyper-links! – Don’t bother writing down URLs ©2020 Derek C. Ashmore, All Rights Reserved 3
  • 4. Agenda The “What” and “Why” of AWS Lambda Code-Level Tips Operation and Design Habits Summary / Q&A © 2020 Derek C. Ashmore, All Rights Reserved 4
  • 5. What are AWS Lambdas? • You provide custom code -> AWS runs it – Java, Node.js, Python, Go, Ruby, .Net Core – Can implement custom runtimes for languages not in the list! • Computing power with less management – AWS manages the hardware – AWS autoscales that hardware – AWS maintains that hardware • Lambdas are event driven – API Gateway (e.g. RESTful Web Service call) – Many more • Lambdas are stateless • Not to be confused with “Lambda Expressions” in Java 8 © 2020 Derek C. Ashmore, All Rights Reserved 5
  • 6. Lambda Event Sources • API Gateway • SNS Messaging Subscriptions • Schedule • Storage writes – S3, DynamoDB, Kenesis © 2020 Derek C. Ashmore, All Rights Reserved 6 • SES Email receipt • Cloudwatch – Schedule, Events, log entries • Cognito (Security) • CloudFormation – Creation script
  • 7. What’s the Business Benefit • Less Maintenance Hassle • Unlimited Parallelism • Current cost advantage – Don’t pay for idle – CPU cost currently lower • Free tier – 1 M executions and 400K compute seconds per month – Memory allocated determines allowed free-tier runtime • 20 cents per 1 M executions + memory/runtime cost – Administration cost • No O/S upgrades, server backups, etc. © 2020 Derek C. Ashmore, All Rights Reserved 7
  • 8. There’s no free lunch • Less control over environment – Harder to tune – Memory and time limits on execution • Few Environment amenities – No connection pooling, session support, caching • Proprietary Interface – Potential Technical Lock-in • No Guarantee that AWS cost will be constant – Potential Business Risk • Modern version of CGI © 2020 Derek C. Ashmore, All Rights Reserved 8
  • 9. Agenda The “What” and “Why” of AWS Lambda Code-Level Tips Operation and Design Habits Summary / Q&A © 2020 Derek C. Ashmore, All Rights Reserved 9
  • 10. What Makes a “Best Practice”? • Makes Support Easier • Increases Reuse • Increases Performance • Minimizes Resource Consumption – Labor – Runtime ©2018 Derek C. Ashmore, All Rights Reserved 10
  • 11. Let’s start with Low-Hanging Fruit © 2020 Derek C. Ashmore, All Rights Reserved 11
  • 12. Report Inputs/Env on Exception • Place a Try / Catch in your handler – Python Example – Java Example • Also check your arguments with a clear error message © 2020 Derek C. Ashmore, All Rights Reserved 12 def crossAccountHandler(event, context): try: ……………… except Exception as e: e.args += (event,vars(context)) raise
  • 13. Check Arguments Up Front • Check your arguments with a clear error message – Python Example – Java Example © 2020 Derek C. Ashmore, All Rights Reserved 13 def crossAccountHandler(event, context): try: if 'Assumed_Role' in event: ………………… else: raise Exception('Assumed_Role not provided as argument') except Exception as e:
  • 14. Specify Lambda Source Repo • Explicitly put the source repository name in the Lambda comments – In most organizations, the repository name isn’t obvious – Others changing your code need it – You don’t want source control to be out of date © 2020 Derek C. Ashmore, All Rights Reserved 14 """ secretLambda.py …………… Source Control: https://github.com/Derek-Ashmore/AWSDevOpsUtilities """
  • 15. Separate Lambda from Business Logic • Make business logic reusable – Callable by other applications – Usable on premises • Easier to locally develop and debug – Lambda-specific logic is thin! © 2020 Derek C. Ashmore, All Rights Reserved 15 def startStopHandler(event, context): try: executeStopStart(datetime.datetime.now() , os.getenv('Scheduled_StartTime', ‘’) , os.getenv('Scheduled_StopTime', ‘’) , os.getenv('Scheduled_StartStop_Days', 'M,T,W,R,F’)) …………… return 0;
  • 16. This is low-hanging fruit that will be appreciated by your fellow developers! ©2018 Derek C. Ashmore, All Rights Reserved 16 • Log All Inputs and Environment on Exception • Check all arguments up front • Document the source repo at the top. • Repo readme can have other developer specifics • Separate Lambda code from business logic • Now let’s talk design and operations….
  • 17. Agenda The “What” and “Why” of AWS Lambda Code-Level Tips Operation and Design Habits Summary / Q&A © 2020 Derek C. Ashmore, All Rights Reserved 17
  • 18. Automate builds and deployments! © 2020 Derek C. Ashmore, All Rights Reserved 18
  • 19. Lambda Copies Everywhere! • Changes / Bug Fixes need to be deployed everywhere • Solving with automation solves the wrong problem! © 2020 Derek C. Ashmore, All Rights Reserved 19
  • 20. One Copy for All! • Scalable – only need to add accounts over time • Bugfixes in one place • Configuration usually in common DynamoDB table(s) • Sample in Python here ©2018 Derek C. Ashmore, All Rights Reserved 20
  • 21. Cross-Account Execution • Algorithm is – Assume a remote-account role using STS • The response has temporary credentials – Create a session using the remote account creds – Do work in the remote account • Example here: Derek-Ashmore/AWSDevOpsUtilities (Github) © 2020 Derek C. Ashmore, All Rights Reserved 21
  • 22. For workloads over 15 min • Executor that invokes lambda asynchronously for each account • Sample in Python here ©2018 Derek C. Ashmore, All Rights Reserved 22
  • 23. Limit Custom Nesting to One Level • Debugging with nested executions is – Time consuming and difficult – Can’t do locally – Absolutely requires unique correlation id for the entire transaction • Allows you to tell invocation history for one logical transaction – Instead of deep custom nesting, use AWS Step Functions • Use Step Functions if you need more © 2020 Derek C. Ashmore, All Rights Reserved 23
  • 24. Nested Calls using AWS Step Functions • AWS Step Functions – Uses a State Machine model • Think turn-style to get access to train – States are “Locked” and “Unlocked” – Locked → Payment input allowed, then “Unlocked” – Unlocked → One person allowed through, then “Locked” – Automatically provides correlation between invocations • Unified logs for the entire transaction – Now supported by X-ray (09/2020) • Execution time and health per Step Function workflow © 2020 Derek C. Ashmore, All Rights Reserved 24
  • 25. Operations and Design Habits © 2020 Derek C. Ashmore, All Rights Reserved 25 • Automate Builds and Deployments • Only install Lambda’s Once • Limit Lambda nesting to One Level • Step functions if you need more • Now let’s talk dependencies and secrets
  • 26. Use Configuration Injection • No environment specifics hardcoded in the Lambda deployment • Use Environment Variables on the Lambda Definition – No un-encrypted secrets (e.g. database password) • Use Arguments in the triggering event – No un-encrypted secrets • Anti-Example – Splunk forwarding Lambda with hard-coded Splunk channels © 2020 Derek C. Ashmore, All Rights Reserved 26
  • 27. Providing Secrets to Lambdas • Secrets are needed items like credentials of any type. • Use IAM Roles to grant permission to read secrets • Options are: – Use KMS • Encrypt credential and base64 encode it – Place encrypted version in environment variable • Sample Lambda and Encryption Script (here) – Use a Digital Vault (e.g. AWS Secrets Manager) • Sample Lambda here © 2020 Derek C. Ashmore, All Rights Reserved 27
  • 28. AWS Secrets Manager • Use IAM Roles to grant permission to read secrets • You don’t need a “secret” to get a “secret”! © 2020 Derek C. Ashmore, All Rights Reserved 28
  • 29. Avoid Heavy-Footprint Dependencies • Minimizes load time – Mitigates cold-start problem • Java – Use Guice over Spring • Python – Use AWS provided deps first (list is here) © 2020 Derek C. Ashmore, All Rights Reserved 29
  • 30. Idempotence • Possible for your Lambda to be invoked multiple times for the same event – Prevent repeat actions from having a different effect. • Options – Record the event id –> Skip repeated events • Most event sources provide a unique request id – Lambda invoking lambda does not! • Negatively affects performance – 1 extra read – 1 extra write • Need to roll-off old events – Insure that the effect is the same each time • Not perfect → You don’t control invocation order © 2020 Derek C. Ashmore, All Rights Reserved 30
  • 31. Dependency Management • AWS Layers can be used for dependency management – Can upgrade your library dependencies in one location • Provides convenience • Use for custom runtimes! • Beware of unintended consequences – Easies to inadvertently break published lambdas using layers © 2020 Derek C. Ashmore, All Rights Reserved 31
  • 32. Common Mistakes • Deploying inappropriate workloads as Lambdas – Install full Java web application as Lambda • Required keep-alive lambdas to prevent cold-starts • Not keeping business logic unit-testable • Creating the distributed monolith – Lambdas with extensive nested execution – Lambdas not independently deployable © 2020 Derek C. Ashmore, All Rights Reserved 32
  • 33. Further Reading • This slide deck – https://www.slideshare.net/derekashmore/presentations • AWS Lambda Reading List – http://www.derekashmore.com/2016/04/aws-lambda-reading-list.html • Amazon’s Published Best Practice List – https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html © 2020 Derek C. Ashmore, All Rights Reserved 33
  • 34. Questions? • Derek Ashmore: – Blog: www.derekashmore.com – LinkedIn: www.linkedin.com/in/derekashmore • Connect Invites from attendees welcome – Twitter: https://twitter.com/Derek_Ashmore – GitHub: https://github.com/Derek-Ashmore – Book: http://dvtpress.com/ © 2020 Derek C. Ashmore, All Rights Reserved 34