-
1. Comparison between EC2 and Lambda
1.1 EC2
- Virtual Servers in the Cloud
- Limited by RAM and CPU
- Continuously running
- Scaling means interventions to add/remove servers
1.2 Lambda
- Virtual functions - no servers to manage
- Limited by time - short executions
- Run on-demand
- Scaling is automated
2. Benefits of AWS Lambda
2.1 Easy Pricing
- Pay per request and compute time
- Free-tier of 1,000,000 AWS Lambda requests and 400,000 GBs of computing time
2.2 Usability
- Integrated with the whole AWS suite of services
- Integrated with many programming languages
- Easy monitoring through AWS CloudWatch
- Easy to get more resources per function (up to 3GB of RAM!)
- Increasing RAM will also improve CPU and network
3. AWS Lambda language support
- Node.js (Javascript)
- Python
- Java
- C# (.NET Core)
- Golang
- C#/Powershell
- Ruby
- Custom Runtime API (Community supported, for example, Rust)
- Important: Docker is not for AWS Lambda, it's for ECS/Forgate
4. Application
4.1 AWS Lambda Integrations Main Ones
- API Gateway: Create RESTFUL API and invoke a lambda function
- Kinesis: Kinesis using Lambda to do some data transformations on the fly
- DynamoDB: DynamoDB will be used to create some triggers which a Lambda function will be triggered.
- S3: a Lambda function will be triggered anytime, for example, a file is created in S3
- CloudFront:
- CloudWatch Events(EventBridge): This is whenever things happen in our infrastructure on AWS and want to be able to react to things, for example, we have CodePipeLine state changes and we want to do some automation based on it. In this case, we can use the Lambda function.
- CloudWatch: CloudWatch logs to stream these logs whenever you want.
- SNS: SNS to react to notifications and your SNS topics
- SQS: SQS to process messages from your SQS queues
- Cognito: Cognito to react to whenever, for example, a user logs into your database.
4.2 Example: Serverless Thumbnail creation
So let's say we have an S3 bucket, and we want to create thumbnails on the fly, so there will be an event, which is, that the new image will be uploaded in Amazon S3. This will trigger through an S3 event notification, a Lambda function. And that lambda function will have code to generate a thumbnail.
That thumbnail may be pushed and uploaded into another S3 bucket or the same S3 bucket, which would be a smaller version of that image, and also, our Lambda function may want to insert some data into DynamoDB, around some metadata for the image, for example, the image name, size, creation date, et cetera, et cetera. And so thanks to Lambda, we've automated and had a reactive architecture to the event of a new app, new images being created in S3.
4.3 Example: Serverless CRON Job
4.3.1 Motivation
CRON is way, on your EC2 instances, for example, to generate jobs every five minutes, or every Monday at 10:00 a.m., et cetera, et cetera. But you need to run CRON on a virtual server, so an EC2 instance, and so on. And so while your instance is not running, or at least your CRONs are not doing anything, then your instance time is wasted.
4.3.2 Solution
You can create a CloudWatch event rule or an EventBridge rule, that will be triggered every one hour. And every one hour, it will be integrated with a Lambda function that will perform your task. So this is a way to create a serverless CRON, because in this example, CloudWatch Events is serverless, and Lambda functions are serverless too.
5. Pricing
- You can find overall pricing information here:
- Pay per calls
- First 1,000,000 request are free
- $0.20 per 1 million requests thereafter ($0.0000002 per request)
- Pay per duration: (in an increment of 100ms)
- 400,000 GB-seconds of compute time per month if FREE
- == 400,000 seconds if function is 1GB RAM
- == 3,200,000 seconds if function is 128MB RAM
- after that $1.00 for 600,000 GB-seconds
- It is usually very cheap to run AWS Lambda so it's very popular
6. Limits of Lambda per region
6.1 Execution
- Memory allocation: 128MB ~ 3008MB (64MB increments)
- Maximum execution time: 900 seconds (15 Minutes)
- Environment variables: up to 4KB
- Disk capacity in the "function container" in /tmp: up to 512MB
- Concurrency executions: 1000 (can be increased)
6.2 Deployment
- Lambda function deployment size (compress.zip): 50MB
- Size of uncompressed deployment (code + dependencies): 250MB
- Can use the /tmp directory to load other files at startup
- Size of environment variables: 4KB
7. Lambda@Edge
7.1 Motivation
- You have deployed a CDN using CloudFront
- What if you wanted to run a global AWS Lambda alongside
- Or how to implement request filtering before reaching your application
7.2 Solution
- Deploy Lambda functions alongside your CloudFront CDN
- Build more responsive applications
- You don't manage servers, Lambda is deployed globally
- Customize the CDN content
- Pay only for what you use
7.3 Example
You can use Lambda to change CloudFront requests and response
7.3.1 Timing
- After CloudFront receives a request from a viewer (viewer request)
- Before CloudFront forwards the request to the origin (origin request)
- After CloudFront receives the response from the origin (origin response)
- Before CloudFront forwards the response to the viewer (viewer response)
- You can also generate responses to viewers without ever sending the request to the origin.
7.3.2 Integration with other services
7.3.3 Use Cases
- Website Security and Privacy
- Dynamic Web Application at the Edge
- Search Engine Optimization (SEO)
- Intelligently Route Access Origins and Data Centers
- Bot Mitigation at the Edge
- Real-time Image Transformation
- A/B Testing
- User Authentication and Authorization
- User Prioritization
- User Tracking and Analytics
8. Reference
'Cloud > AWS' 카테고리의 다른 글
AWS Redshift (0) 2022.06.05 Lake Formation (0) 2022.04.26 Choosing the right database on AWS (0) 2021.03.08 DynamoDB (0) 2021.03.08 Simple Storage Service (S3) (0) 2020.11.24