In this session, AWS Solutions Architect Paul Sears will provide an overview of AWS Lambda functions, including some key integration use cases with MongoDB Atlas. Developer Advocate Raphael Londner will walk you through how to code a Lambda function connected to MongoDB Atlas, with a specific focus on performance optimization. Raphael will then demonstrate how to orchestrate multiple Lambda functions inside a state machine built on top of AWS Step Functions.
5. #MDBW17
SERVERLESS COMPUTING - AWS LAMBDA
Run code without provisioning or managing servers – pay only for the
compute time you consume.
6. #MDBW17
Continuous ScalingNo Servers to
Manage
Subsecond
Metering
BENEFITS OF AWS LAMBDA
AWS Lambda handles:
• Operations and
management
• Provisioning and utilization
• Scaling
• Availability and fault
tolerance
Automatically scales your
application, running code in
response to each trigger
Your code runs in parallel and
processes each trigger
individually, scaling precisely
with the size of the workload
Pricing
• CPU and Network
scaled based on
RAM (128 MB to
1500 MB)
• $0.20 per
1M requests
• Price per 100ms
7. #MDBW17
AWS LAMBDA USE CASES
• Web applications
• Data processing (real-time streaming analytics)
• Scalable back ends (mobile apps, IoT devices)
• Amazon Alexa
• Chatbots
8. #MDBW17
Web
Applications
• Static
websites
• Complex web
apps
• Packages for
Flask and
Express
Data
Processing
• Real time
• MapReduce
• Batch
Chatbots
• Powering
chatbot logic
Backends
• Apps &
services
• Mobile
• IoT
</></>
Amazon
Alexa
• Powering
voice-enabled
apps
• Alexa Skills Kit
Autonomous
IT
• Policy engines
• Extending
AWS services
• Infrastructure
management
Data processing: Lambda + S3
COMMON USE CASES
13. #MDBW17
COMPONENTS OF A SERVERLESS APP
EVENT SOURCE
Requests to
endpoints
Changes in
resource state
FUNCTION SERVICES
14. #MDBW17
ANATOMY OF A LAMBDA FUNCTION
• Handler function (or method)
• An input parameter
‒ Integer, double, string, JSON…
• A context parameter
‒ Provides information such as
o Remaining time until timeout, log group and stream, request ID, etc…
‒ Exposes public properties
• A return value (optional)
15. #MDBW17
ANATOMY OF A LAMBDA FUNCTION (NODE.JS)
• Handler function in index.js
exports.myHandler = function(event, context, callback)
=> {…}
• Handler format in AWS Lambda: [moduleName].[functionName]
index.myHandler
16. #MDBW17
ANATOMY OF A LAMBDA FUNCTION (JAVA)
• Handler function in example.Hello class
package example;
public class Hello {
public String myHandler(type inputVal, Context context) {...}
• Handler format in Lambda: [package].[className]::[functionName]
17. #MDBW17
ANATOMY OF A LAMBDA FUNCTION (PYTHON)
• Handler function in hello_python.py file
def my_handler(event, context):
…
return some_value
• Handler format in Lambda: [fileName].[functionName]
18. #MDBW17
ANATOMY OF A LAMBDA FUNCTION (C#)
• Handler function in Example.Hello
namespace Example;
public class Hello {
public Stream MyHandler(type inputVal, ILambdaContext context) {...}
• Handler forma in Lambda:
[assemblyName]::[namespace].[className]::[methodName]
19. #MDBW17
HOW TO DEVELOP FOR LAMBDA & ATLAS
IN NODE.JS
Sign up for
Atlas
Create
a
cluster
Copy cluster
URI to a safe
location
Create
a DB
user
Init a
Node
project
Write code!
Import
MongoDB
Node driver
20. #MDBW17
HOW TO TEST AND DEPLOY YOUR LAMBDA
Upload your
package to
AWS Lambda
Use lambda-
local to test
locally
Zip your
Node.js
code
Create
your
Lambda
function
Set env.
variables,
memory,
security…
Test in AWS
(Console,
API
Gateway)
24. #MDBW17
BEST PRACTICES: LAMBDA FUNCTION
ü Use the right “timeout”
ü Utilize the functions local storage which is 500MB in size in the /tmp
ü Lower costs and improve performance by minimizing the use of startup code
not directly related to processing the current event
ü Use the built-in CloudWatch monitoring of your Lambda functions to view and
optimize request latencies
25. #MDBW17
THINGS TO REMEMBER: LAMBDA FUNCTION
ü Memory = “Power level”
q Higher levels offer more memory and more CPU power
ü Functions don’t have a notion of state
q Use MongoDB Atlas, S3, or Elasticache, or AWS Step Functions
q Wrap your config in a function and call it from your published code
ü Use the right access control for downstream services
q IAM roles and permissions for AWS services
q VPC for private endpoints
q KMS for storing credentials for downstream endpoints
26. #MDBW17
THINGS TO REMEMBER: LAMBDA
APPLICATION
ü Lambda scales by events/requests
q Plan for concurrent request rate on downstream services
ü Shared scaling responsibility for VPC enabled functions
q Sufficient IPs to match your expected concurrency
q at least one subnet in each availability zone
ü Retries are built in for asynchronous and Stream invokes
q Plan for retries for synchronous applications
27. #MDBW17
WHERE NOT TO CONSIDER LAMBDA (TODAY)
• Large software dependencies: Custom software applications with
licensing agreements such as MS-Office document processing, EDA
tools, Oracle databases, etc.
• OS dependencies: Software packages or applications which rely on
calling underlying Windows RPCs
• Custom hardware: GPU acceleration, hardware affinity
29. #MDBW17
AWS LAMBDA WITH MONGODB ATLAS
• Store database connection string in an environment variable
‒ Use –E parameter with lambda-local
‒ Reference it with process.env['MONGODB_ATLAS_URI']
• Encrypt database connection string in AWS Lambda
‒ Use AWS.KMS() to decrypt() the connection string
30. #MDBW17
PERFORMANCE BEST PRACTICES WITH NODE.JS
• Declare the db object outside the handler method
• Do NOT close the db object!
• Set context.callbackWaitsForEmptyEventLoop to
‘false’
• Try to re-use the db object if
db.serverConfig.isConnected() returns true
35. #MDBW17
“I WANT TO SEQUENCE FUNCTIONS”
“I want to select functions based on data”
“I want to retry functions”
“I want try/catch/finally”
Turning functions into apps
“I have code that runs for hours”
“I want to run functions in parallel”
36. #MDBW17
BENEFITS OF AWS STEP FUNCTIONS
Diagnose and debug
problems faster
Adapt to change
Easy to connect and
coordinate
distributed components and
microservices to quickly
create apps
Manages the operations
and infrastructure of
service coordination to
ensure availability at
scale, and
under failure
Productivity Agility Resilience
41. DEFINE IN JSON AND THEN VISUALIZE IN THE
CONSOLE• {
• ”Comment”: “Hello World Example",
• "StartAt” : "HelloWorld”,
• "States” : {
• "HelloWorld” : {
• "Type” : "Task",
• "Resource” :
"arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTI
ON_NAME”,
• "End” : true
• }
• }
42. #MDBW17
EXECUTE ONE OR ONE MILLION
Start
End
HelloWorld
Start
End
HelloWorld
Start
End
HelloWorld
Start
End
HelloWorld
Start
End
HelloWorld
Start
End
HelloWorld
Start
End
HelloWorld
Start
End
HelloWorld
44. #MDBW17
SEVEN STATE TYPES
Task A single unit of work
Choice Adds branching logic
Parallel Fork and join the data across tasks
Wait Delay for a specified time
Fail
Stops an execution and marks it as a
failure
Succeed Stops an execution successfully
Pass Passes its input to its output
47. #MDBW17
INTEGRATE WITH OTHER AWS SERVICES
• Create state machines and Activities with AWS CloudFormation
• Call Step Functions with Amazon API Gateway
• Start state machines in response to events or on a schedule with
CloudWatch Events
• Monitor state machine executions with CloudWatch
• Log API calls with CloudTrail
51. #MDBW17
FOLLOW-ON SESSION
• What: MongoDB Atlas and Serverless Architectures
• When: Wednesday, June 21 at 4.30PM
• Where: Crystal A
• Who: Tosin Ajayi, Sr. Solutions Architect
52. #MDBW17
REFERENCE LINKS
• Serverless development with Node.js, Lambda and MongoDB Atlas
• Optimizing AWS Lambda performance with MongoDB Atlas and
Node.js
• Step Functions with MongoDB Atlas Part 1
• Step Functions with MongoDB Atlas Part 2
• Lambda with MongoDB GitHub repository
• Step Functions with MongoDB GitHub repository