Introduction
- Serverless is a new paradigm which the developers don't have to manage servers anymore.
- They just deploy code.
- They just deploy functions.
- At the beginning, Serverless is function as a Service
- Lambda is AWS pioneer serverless but now includes anything managed: databases, messaging, storage, etc... can be considered as serverless service.
Some services that are serverless:
- AWS lambda
- DynamoDB
- AWS Cognito
- AWS API gateway
- AWS S3
- AWS SNS / SQS
- Kinesis Data Firehose
- Aurora serverless
- Step function
- Fargate
In this article we mainly focus on AWS Lambda
Lambda
- It provides easy pricing: Pay per request and compute time, Free tier of 1M requests and 400,000 GB-seconds per month
- Can integrate with almost all AWS services
- Can develop with many programming languages
- Easy monitoring through AWS CloudWatch
- Easy to get more resources per function (up to 10GB of RAM)
- Increasing RAM will also increase CPU and network bandwidth
- Pay per calls. The first 1 M requests are free then $0.20 per 1M requests
- Pay per duration
- First 400,000 GB-seconds are free
- Then $0.00001667 for every GB-second
- After that 1$ for 600000 GB seconds.
- It is usually very cheap to run AWS lambda so it's very popular.
- The timeout is up to 15 minutes. But it is recommended to keep it short. When the timeout is reached, the function will be terminated.
Lambda limits - per region
- Execution
- Memory: 128MB to 10GB
- Timeout: 15 minutes
- Environment variables: 4KB
- Disk capacity in the /tmp directory: 512MB to 10GB
- Concurrent executions: 1000 can be increased through a support ticket.
- Deployment
- Lambda function deployment size: 50MB (zipped, for direct upload) / 250MB (unzipped, including layers)
- We can use the /tmp directory to load other files at startup
- Size of environment variables: 4KB
Lambda SnapStart
- SnapStart is a feature that allows you to start your Lambda function faster up to 10x at no extra cost for Java 11 and above.
- When enabled, the function is invoked from the pre-initialized state and it will be faster.
-
- When you publish a new version:
* Lambda initialize your function
* Take a snapshot of the memory and disk state of the initialized function
* Snapshot is cached for low-latency access.
Some notes about Lambda
- By default your lambda function is launched outside your VPC (in AWS-managed VPC)
- Therefore it does not have access to your VPC resources (RDS, ElasticCache, internal ELB).
- If you need to access your VPC resources, you need to launch your lambda function within your VPC.
- Lambda will create an ENI in your subnets.
- If the lambda function connects directly access to your database, it may open too many connections under high load.
- RDS Proxy improves scalability by pooling and sharing database connections.
- Improve availability by automatically failover.
- Improve availability by reducing 66% the failover time and preserving the existing connections.
- Improve security by enforcing IAM authentication.
- The Lambda function must be deployed within a VPC. Because RDS Proxy is never publicly accessible.
Lambda Concurrency Limits:
- Default Limit: AWS Lambda imposes a regional concurrency limit, which is the maximum number of concurrent function executions across all functions within a region. The default limit is 1,000, but it can be increased by requesting a quota adjustment.
- Reserved Concurrency: You can reserve concurrency for critical functions to ensure they always have capacity, but this reduces the overall concurrency available for other functions.
Invoke Lambda from RDS & Aurora
- You can invoke you lambda functions from RDS or Aurora using the AWS Lambda extension.
- Allow you to process data events from RDS or Aurora.
- This is supported for RDS for PostgreSQL and Aurora MySQL.
- When setting up this feature you must allow outbound traffic to the Lambda service from your DB instance via Public, NAT Gateway or VPC Endpoint.
- DB instance must have the required permission to invoke the Lambda function. We need to configure Lambda resource-based policy & IAM policy.
- DB Events can be:
* Tell you information about the DB instance itself (created, stop, start).
* You DO NOT have information about the data itself.
* Subscribe to the following event categories: DB instance, DB snapshot, DB parameter group, DB security group, RDS proxy, and Custom engine version.
* Near real-time events (delay up to 5 minutes)
* You can send notifications to SNS or subscribe to events using EventBridge.