Werner Vogels, CTO of Amazon, introduced Lambda at re:Invent conference (November 2014) — “The focus here is on the events. Events may be driven by Web services that would trigger these events. You’ll write some code say, in JavaScript and this will run without any hardware that you have to provision for it.”
What is AWS Lambda?
- Event driven and server-less computing service platform.
- Offers function as a service.
- Lambda functions can perform any kind of computing tasks e.g. Serving web pages, Processing streams of data, Acting as REST Api, Integrating with other web services
- Serverless architecture means no servers involved. Just focus on writing business logic.
Why AWS Lambda? — Advantages of using AWS Lambda
1. Serverless Architecture:
No servers involved.
Typically a server-less stack needs — A computing service, a database service and an HTTP gateway service. Lambda serves the role of computing service and then it integrates with other AWS services like DynamoDB and API gateway.
2. Fully managed infrastructure
No need to manage infrastructure.
AWS perform operational and administrative activities on your behalf like-
- Provisioning capacity
- Monitoring fleet health
- Offering a balance of memory, CPU, network and other resources.
- Operating system maintenance
- Applying security patches
- Deploying your code
- Monitoring and logging Lambda functions
3. Pay per use:
Pay for only what you use.
Charged based on number of requests for your functions and duration of time it takes for your code to execute.
4. Scalability
AWS lambda creates instances of function as they are requested.
Concurrent execution of many instances of same function or of different functions from same AWS account can be achieved.
5. Integration with other AWS services:
Easily integrates with services like DynamoDB, s3 and API gateway.
Allows build functionally complete applications within the Lambda function
6. Language support and runtime:
Supports multiple languages through the use of runtimes like-
- Node.js
- Python
- Ruby
- Java
- Go
- C#
- Custom runtime
7. Offers wide range of features -
- Programming Model
- Deployment Package Layers
- Scaling
- Concurrency controls
- Asynchronous invocations
- Event source mappings
- Destinations
- Function Blueprints
- Application Templates
“It’s easy to run without managing infrastructure. It will take care of managing, scaling, monitoring and logging for you. Your applications run milliseconds after the event has been triggered and thousands of functions can work in parallel. You run the code only when needed.” — Werner Vogels added.
When should we use AWS Lambda?
AWS lambda being a server-less architecture can be a good choice over traditional cloud computing setups in use cases —
- Individual tasks run for shorter time.
- Each task is self-contained
- Event based data processing
- Task automation — Automating business tasks where no server needed like running scheduled jobs.
- Image or object uploads to s3
- Updates to DynamoDB table
- Responding to website clicks
- Reacting to sensor readings from AWS IoT connected device
- Creating web hooks to read or publish messages
How does AWS Lambda works?
Each Lambda function runs in its own container. When a function is created, Lambda packages it into a new container and then executes that container on a multi-tenant cluster of machines managed by AWS. Before the functions start running, each function’s container is allocated its necessary RAM and CPU capacity. Once the functions finish running, the RAM allocated at the beginning is multiplied by the amount of time the function spent running. The customers then get charged based on the allocated memory and the amount of run time the function took to complete.
AWS Lambda Concepts
Functions
A resource you can invoke to run your code in AWS Lambda.
It consists of
- Code — that process events and
- Runtime — that passes requests and responses between the code and Lambda service.
Runtime
Allows functions in different languages to run in the same base execution environment.
Acts as a bridge between Lambda service and the code relaying — invocation events, context information and responses between the two.
Event
JSON formatted data that is passed to the function code. Consider it as JSON request for an API.
Concurrency
Number of requests that your function is serving at any given time.
Every time a lambda function is invoked, Lambda provisions an instance of it to process that event. If a function is invoked again while it was processing the request then it provides another instance of it to process this new event thus, increasing the function’s concurrency.
Trigger
Resource or configuration that invokes a Lambda function.
It includes -
- AWS services that can be configured to invoke a function.
- Applications that you develop.
- Event source mappings — A resource that reads items from stream or queue and invokes a function.
Connecting Lambda with other AWS services
AWS Lambda easily integrates with other AWS services. Each service that integrates with lambda sends data to the function as an event document which contains event type, data about request that triggered the Lambda. Lambda runtimes convert event into an object and passes it to the main handler function of Lambda.
Services that Lambda reads events from -
- Amazon DynamoDB
- Amazon Kinesis
- Amazon Simple Queue Sevice
Services that invoke Lambda functions -
Synchronously -
- Elastic Load Balancing
- Amazon Cognito
- Amazon Lex
- Amazon API Gateway
- Amazon Alexa
- Amazon CloudFront
- Amazon Kinesis Data Firehose
- AWS Step Functions
- Amazon Simple Storage Service Batch
Asynchronously-
- Amazon Simple Storage Service
- Amazon Simple Notification Service
- Amazon Simple Email Service
- AWS CloudFormation
- AWS CloudWatch Logs
- AWS CloudWatch Events
- AWS CodeCommit
- AWS Config
- AWS IoT
- AWS IoT Events
- AWS CodePipeline
Limitations of AWS Lambda
Cold start time:
There may be small amount of latency between the event and when the function runs.
Hard to rely on lambda for latency-critical applications because it can be as high as 5–10 sec in cases when lambda has not been used for long time,
Function limits:
- Execution time — Lambda time out after running for 15 minutes.
- Memory available — Amount of RAM available to lambda function ranges from 128 MB to 3008MB
- Code package size — Zipped lambda package size should not exceed 50 MB and unzipped version should not exceed 250 MB.
- Concurrency — Concurrent execution of lambda function in an AWS account is throttled to 1000.
- Payload size — The maximum payload size that API gateway can handle is 10 MB, while using API gateway to trigger lambda functions.
Not always cost effective
When load of application increases, Lambda costs increase proportionally and might end up higher than cost of infrastructure on AWA EC2 instance.
Limited number of supported runtime
Although it supports most of the popular runtimes but if it isn’t supporting then creating custom runtimes can be cumbersome.