It’s easier than ever to power serverless architectures with our managed MongoDB as a service, MongoDB Atlas. In this session, we will explore the rise of serverless architectures and how they’ve rapidly integrated into public and private cloud offerings.
We will demonstrate how to build a simple REST API using AWS Lambda functions, how to create a highly available cluster in MongoDB Atlas, and finally, how to connect those pieces in a secure and scalable manner. We will then simulate load and show how to use the monitoring features of MongoDB Atlas. Finally, we will use MongoDB Compass to browse our database. You'll walk away with an understanding of how to build your first serverless cloud service, backed by a highly scalable database as a service.
This session is aimed at developers that are:
1. Building modern applications in an environment where fast delivery and agility are critical
2. Looking to reduce operational overhead so that they can focus on creating functionality for their customers
What's next?
Try it for yourself! Get started with MongoDB Atlas for free.
5. Serverless Frameworks and Platforms
https://github.com/serverless/serverless
https://www.zappa.io/
Chalice (awslabs)
https://github.com/awslabs/chalice
Frameworks for Cloud
providers
On-Prem PaaS – now offering FaaS
6. 1. Cloud services have
matured
2. BaaS “SaaS-ification”
3. API’s are the glue
4. Containers – now per
function
5. SysOps DevOps
NoOps
Less Ops, More
5 factors fuelling Serverless Computing
https://www.forbes.com/sites/janakirammsv/2016/02/28/five-factors-that-are-fueling-serverless-computing-part-1
8. Scheduled Jobs
• Sequencing / Orchestration (AWS Steps?)
Data Quality
• Trigger Identify Pass to function
Micro or Nano services
• Clicks or Taps
Event and IoT processing
• Don’t worry about scaling App Servers
Lightweight API’s
• Focus of today!
Good fit for Serverless?
13. CQRS pattern on Serverless Microservices
GET
API
PUT PATCH
POST DELETE …
API
API Key
API Key
Lambda Function(s)
Lambda Function(s)
Code
Code
Lambda Function(s)
VPC
Peering
14. Shape
• Person
• Insurance Policies
• Shape changes per policy type
• Addresses
Operations via API
• GET Customers with soon-to-expire
policies, within a geo radius
• GET Customers / by SSN, id, etc.
• PATCH Update basic contact info (cell,
email, …)
Customer Single View - Insurance Industry
(hypothetical)
High-level architecture of a
single view platform
18. IAM
• Role with Lambda execute policies
VPC
• VPC
• Security Groups – traffic rules
• Internet Gateway – outside
communication
• VPC Peering Connection - Route
Table
Required AWS Services
Lambda
• VPC, Security Group and IAM role
• Develop inline or upload
deployment package (.zip)
• Use MongoDB Driver – connect
with MongoDB Atlas
API Gateway
• API definition
• API Keys & Usage Plans
• Resources and HTTP Methods
• Map Routes to Lambda functions
20. MongoDB Atlas
• Provision a Cluster – M10+ need an assigned AWS region for VPC peer
• Same AWS region – (I will use us-west-2)
• Initiate VPC peer with AWS
AWS VPC
• Accept incoming Peering Connection
• Update Route Table
EC2
• Install MongoDB
• Test connection from the MongoDB Shell to ensure VPC Peer is working
• Optional but highly recommended – ensure VPC Peering is working before proceeding
to Lambda
MongoDB Atlas peered with your AWS VPC
29. Upload &
configure function
The handler function
The role with lambda permissions
The VPC (peered with Atlas)
The security group that allows traffic
At least 2 subnets
39. Connections and containers …..
http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html
… AWS Lambda maintains the container for some time in anticipation of another Lambda function
invocation. … the service freezes the container after a function completes, and thaws the container for
reuse. If AWS Lambda chooses to reuse the container, this has the following implications:
- Any declarations in your Lambda function code (outside the handler code, see Programming
Model) remains initialized, providing additional optimization when the function is invoked again. For
example, if your Lambda function establishes a database connection, instead of reestablishing
the connection, the original connection is used in subsequent invocations. You can add logic in
your code to check if a connection already exists before creating one.
43. Scaling?
Scaling Lambda
No user intervention required - Default safety
throttle of 100 concurrent executions per
account per region.
Functions invoked synchronously throw 429
error code. Functions invoked asynchronously
can absorb reasonable bursts for approx. 15-30
minutes. If exhausted, consider using Simple
Queue Service (SQS) or Simple Notification
Service (SNS) as the Dead Letter Queue (DLQ).
Read more at
https://aws.amazon.com/lambda/faqs/
Scaling MongoDB
Atlas
On-Demand
Zero downtime
Upscale/Downscale:
• Instance size
• Storage size
• IOPS
• Replication factor.
44. Pricing?
Lambda Costs
Cost depends on requests (per million), request
time, memory (GB) allocated to each function.
First 1 million requests per month free - $0.20 per
1 million requests thereafter.
$0.00001667 for every GB-second used.
Additional AWS services imply cost (e.g. API
Gateway, …)
Read more at
https://aws.amazon.com/lambda/pricing/
MongoDB Atlas Costs
Cost depends on instance size, storage, iops,
replication factor and backup retention.
M0 free – great for you (no VPC peering, use IP
whitelist)
M10 starts at $0.08/hr – great for team Dev
M30 starts at $0.54.hr – great for Production
Read more at
https://www.mongodb.com/cloud/atlas/pricing
45. Connections to MongoDB Atlas
Encrypt using AWS KMS – see this blog post:
https://www.mongodb.com/blog/post/serverless
-development-with-nodejs-aws-lambda-
mongodb-atlas
Container freeze & recycle?
• Connection outside lambda function helps
• On scale new containers, new
connections
• Ok if API is used in bursts, but maybe
not ok if used seldomly
• If not?
Others?
Local development? Lambda emulators
• python-lambda-local at
https://pypi.python.org/pypi/python-lambda-local
• lambda-local (node.js) at
https://www.npmjs.com/package/lambda-local
Serverless frameworks – evaluate them! … F500’s
are!
• Serverless Framework
• Zappa
• Chalice
• More! - https://thenewstack.io/tns-guide-
serverless-technologies-best-frameworks-
46. Façade Serverless Functions –
logic querying backend API
Backend Traditional stateful
layer - CRUD API to Data Stores
Would this be a
Serverless Architecture
??
Customer Single View - Insurance Industry
(hypothetical)
High-level architecture of a
single view platform
Stateful API
Service Layer
47.
48. Serverless
Architectures with
AWS Lambda and
MongoDB Atlas
Q&A
Use code "Sig" for 25% off!
Parties of 3+ get addtl 25%
Sig Narváez
Sr. Solutions
Architect
sig@mongodb.com
@SigNarvaez