CMSC389L
Week 9
Serverless
March 30, 2018
Lambda
Lambda: What is it?
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability.
Serverless: Historical Context
- Data Centers: (pre-AWS)
- Maintain hardware! 😷
- Infrastructure as a Service: EC2
- Configure, manage, & scale servers
- Platform as a Service: Beanstalk
- Configure, manage OS/patches/etc.
- Containers: Docker, ECS
- Have to scale # of containers
- Serverless: Lambda
- Just write code -- no operational overhead
Higher Level Abstractions
Serverless on AWS
- Three key components of application architectures
Storage | Networking | Compute | |
---|---|---|---|
IaaS | EBS, RDS | N/A | EC2 |
Serverless | S3, DynamoDB | API Gateway, LBs | Lambda! |
Lambda: Benefits
- Zero administration: you don't need to manage, patch, and scale servers
- Continuous scaling: Lambda functions naturally scale horizontally, and you don't need to provision more instances as your traffic goes up!
-
Subsecond metering: Metering down to 100ms, which is 10x better than EC2
- You only pay for your usage -- you don't pay for idle time
- Security: Principle of Least Privilege: can configure IAM policies per-function, rather than per-application/per-instance
- Microservice Architecture: FaaS naturally encourages this
Lambda: What does it look like?
Lambda: Complexity
- Monolith vs Services/Functions:
- Monolith services are easy to reason about at the architecture level.
- Microservices / Functions lead to complicated relationships between functions.
- Why is that okay?
- Forces you to logically separate components.
- Therefore, easy to add/remove features (good for reducing tech debt).
- Much easier to develop on with a team, reduces organizational overhead.
- Forces you to logically separate components.
Lambda: Dealing w/ Complexity
-
Can be tricky to monitor/debug via CloudWatch logs.
- Use AWS X-Ray for distributed tracing, track requests between functions.
Lambda: Concepts
- Function: The code that you are executing. Supported languages: Node, Python, Java, C#.
- Handler: The main function in your code.
- Template: Amazon provides a number of templates that you can use to launch basic Lambda functions
- Role: IAM role that the function executes under. Necessary for security and debuggability.
-
Deployment Package: ZIP directory containing all code/dependencies.
- Can include your dependencies or other necessary assets
Lambda: Concepts (cont.)
- Trigger: An event that invokes a Lambda function.
- Timeout: Lambda functions are limited to 5m, but you should set this timeout lower depending on
-
Sync vs. Async: Synchronous mode will return a value, async just fires off the function.
- Async mode will receive an immediate response, indicating if the function started correctly.
Lambda: Triggers
- Lambda functions are invoked by some kind of trigger (aka event). For example:
- S3 (on upload, metadata change, removal, ...)
- DynamoDB (insert, update, delete, ...)
- CodeCommit (on git commit, ...)
- Scheduled (CRON-style), via CloudWatch Events
- Directly (over HTTP, say from a client's browser) via API Gateway
- In other words, Lambda enables event-based programming.
Lambda: Example Architecture
Lambda: How do they work?
- Similar to how EC2 uses a hypervisor to isolate instances, Lambda uses Linux containers (LXC).
- Containers are extremely lightweight -- no OS duplication.
- Amazon will run 100s of these on a single physical server.
- Controlled environment (libraries, language, etc.) means that AWS can eek out the efficiency of these instances.
Lambda @ Edge
- CDN's are great because they bring static content as close to your users as possible
- Reduce latencies, etc.
- What if you could do that with compute?
- L@E: Lets you run Lambda requests in between server/CloudFront or user/CloudFront
- Can inspect HTTP headers, perform logic such as redirection
Lambda Pricing
- Two pricing components: # requests, and duration
-
# Requests
- $0.20 / 1M requests
- Free Tier: first 1M requests
-
Duration
- Pay per ms of execution time, rounded up to 100ms
-
$16.64 / 1M GB-second
- = $0.208 / 1M 128MB-100ms (min RAM & shortest time)
-
Free Tier: 400,000 GB-seconds
- you could run a 128MB Lambda function continuously for an entire month under the free tier
Lambda: Optimizations
- Containers can be re-used. Move as much re-useable logic outside of the handler. Global state will be preserved.
- For example: database connections; avoid variable re-initialization
- Minimize the size of your deployment package (code, etc.)
- Prefer small frameworks and libraries
- Test your Lambda code on various memory configurations
- May reduce your overall costs, depending on your task
- Load test your Lambda function to determine the minimum necessary timeout value
- Lambda functions that fail will terminate sooner, saving on duration costs
Lambda Demo
Lambda Worksheet
Complete the worksheet section on Lambda.
Final Project Details
(Get pumped)
Final Project
-
Goal: Build a meaningful project that proves your AWS skillset to employers.
- Develop experience interconnecting different AWS services to build a project in a scalable fashion.
-
Requirements:
- Must use at least 3 different AWS services
-
At least one of which is a service we have not covered in class
- SaaS: Polly (text to speech)
- IaaS: RDS (MySQL, PostgreSQL, etc.)
- Batch: AWS MapReduce (big data processing)
- Other: AWS IoT (programmable Dash buttons!)
Deliverables
-
Proposal
- Formatted as slides. Submit online, demo in-person to a facilitator.
- Due this Sunday
-
Checkpoints
- Status update on progress, roadblocks
- First Checkpoint: by 4/13
-
Code + Demos
- Public GitHub repo
- 3-5m YouTube demo video (I recommend using Loom)
- In-person demo with a facilitator
Final Project: Examples
- Alexa App
- Build an Alexa app that can play you in chess.
- Use AWS Alexa Skill Kit and Lambda to respond to user requests.
- Store user data and game data in DynamoDB.
- Optionally, build a front-end for it on top of Elastic Beanstalk or S3!
- Some other options:
- AWS IoT: You can access hardware for free through Sandbox!
- Chatbot: Check out AWS Lex!
- etc.
Wrapping Up
Proposal due this Sunday
Proposal meetings next week
Short ALB
Feedback form: ter.ps/feedback9
CMSC389L Week 9
By Colin King
CMSC389L Week 9
- 961