Serverless And Lambda Flashcards
when you use serverless services
it’s not that you don’t have servers anymore, it’s just that you don’t manage them.
You just deploy code and originally, you just deploy it functions.
So initially serverless meant Function as a Service,
or FaaS.
But now serverless means a lot more. So in the beginning, serverless was pioneered by AWS Lambda
but now also includes anything that is remotely managed.
So databases, messaging, storage, as long as you don’t provision servers.
serverless in AWS
- we have our users and they would get for example,
static content for from our S3 buckets delivered as a website’s or CloudFront plus S3. - Then we would login with Cognito, this is where our users would have their identity stored.
- they would invoke your REST API through the API gateway
- the API gateway would invoke Lambda functions
- Lambda functions would store and retrieve data from DynamoDB.
reference architecture for serverless applications.
Lambda DynamoDB, Cognito, API Gateway, Amazon S3, but also SNS and SQS, Kinesis Data Firehose, because again it scales based on the throughput you have you just pay for what you use and you don’t provision servers, Aurora Serverless, when your Aurora database scales on demand without you managing servers,
Step functions, Fargate
Lambda
virtual functions, no servers to manage.
we just provision the code and the functions run.
Lambda execution period
This is limited by time,
short executions of up to 15 minutes,
Lambda on-demand
they run on-demand. When you don’t use Lambda, your Lambda function is not running, and you only are going to be billed when your function is running
and it will run on-demand when it gets invoked,
Lambda scaling
scaling is automated.
If you need more Lambda functions, occurrences,
then automatically, AWS will provision for you
more Lambda functions.
Lambda payment
You’re going to pay for the number of requests
Lambda receives, so the number of invocations,
and your compute time, so how long Lambda was running for.
free tier on Lambda is 1 million Lambda requests,
and 400,000 gigabyte-seconds of compute time.
Lambda resources per function
if you want to provision more resources per function,
you can provision up to three gigabytes of RAM per function
if you increase the RAM of your function, then it will also improve the quality and performance of your CPU and network.
Lambda language support
Node.js, so JavaScript, Python, Java, and this is Java 8 compatible, C# or .NET Core, Golang, C# and Powershell, Ruby, and there is a new API called the Custom Runtime API, where you can run pretty much any language on Lambda, but it has to be community supported, and one of these example is, for example,
the Rust language.
The one thing that does not run on Lambda is
Docker
if in the exam you are asked, I want to run a Docker container on AWS, you have to think ECS or Fargate.
Lambda - API Gateway integration
to create a REST API, and they will invoke our Lambda functions.
Lambda - Kinesis integration
Kinesis will be using Lambda to do some data transformations on the fly.
Lambda - DynamoDB integration
DynamoDB will be used to create some triggers,
so whenever something happens in our database,
a Lambda function will be triggered.
Lambda - S3 integration
a Lambda function will be triggered anytime, for example,
a file is created in S3.
Lambda - CloudFront integration
this will be Lambda@Edge,
Lambda - CloudWatch integration
CloudWatch Events or EventBridge, this is whenever things happen in our infrastructure on AWS and want to be able to react to things, for example,
say, we have a CodePipeline state changes, and we want to do some automations based on it, we can use a Lambda function.
CloudWatch Logs to stream these logs wherever you want.
Lambda - SNS integration
SNS to react to notifications
Lambda - SQS integration
SQS to process messages from your SQS queues.
Lambda - Cognito integration
to react to whenever, for example, a user logs into your database.
serverless thumbnail creation
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, which 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 new app, new images being created in S3.
serverless CRON job
if you run CRON on a virtual server, so an EC2 instance, while your instance is not running,
or at least your CRONs are not doing anything,
then your instance time is wasted.
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.
Lambda pricing example
you pay per calls, the first 1 million requests are free,
and then you’re going to pay 20 cents for extra 1 million requests,
Then you’re going to pay per duration in increments of 100 milliseconds. So you get the first 400,000 gigabyte-seconds of compute time per month for free.
And then what that means is gigabyte-seconds that,
it means you get 400,000 seconds of execution,
if the function has one gigabyte of RAM.
That means you get eight times as more seconds,
if the function has eight times as less RAM,
so 128 megabyte of RAM.
And after that, you’re going to pay $1 for 600,000 gigabyte-seconds.
Lambda limitations in execution (EXAM)
- memory allocation is between 128 megabytes to 3000 megabytes. And this is in 64 megabytes increments. When we increase the memory,
then we have more vCPU - maximum execution time is 900 seconds, which is 15 minutes,
- we can only have up to four kilobytes of environment variables
- disk capacity in “the function container” (/tmp): 512 MB
- We can have up to 1000 concurrent executions
other than the functions. (can be increased)