Lambda Flashcards
What is Serverless?
- Serverless is a new paradigm in which the developers don’t have to manage servers anymore…
- They just deploy code
- They just deploy… functions!
- Initially… Serverless == FaaS (Function as a Service)
- Serverless was pioneered by AWS Lambda but now also includes anything that’s managed: “databases, messaging, storage, etc.”
- Serverless does not mean there are no servers… it means you just don’t manage / provision / see them
What are the Serverless services in AWS?
- AWS Lambda
- DynamoDB
- AWS Cognito
- AWS API Gateway
- Amazon S3
- AWS SNS & SQS
- AWS Kinesis Data Firehose
- Aurora Serverless
- Step Functions
- Fargate
What platform is not for Lambda?
Docker
Apart from the supported programming languages by Lambda, what else can you do?
Custom Runtime API (community supported, example Rust)
What is Lambda pricing by call?
Pay per calls:
o First 1,000,000 requests are free
o $0.20 per 1 million requests thereafter ($0.0000002 per request)
What is Lambda pricing by duration?
Pay per duration: (in increment of 100ms, round up)
o 400,000 GB-seconds of compute time per month if FREE
o == 400,000 seconds if function is 1GB RAM
o == 3,200,000 seconds if function is 128 MB RAM
o After that $1.00 for 600,000 GB-seconds
How is calculated the duration of your Lambda?
To the nearest multiple of 100 ms, rounded up. This means that if your function ran in 1 ms you are billed for 100 ms
What is the Lambda Handler?
Handler = name of the file + “.” + name of the function
What is Lambda Execution Role?
A role to interact with the necessary services, it should allow you at least to upload logs to CloudWatch logs because each function invocation will create a new log stream in CloudWatch
What is best practice regarding to Lambda functions and the Role executions?
It is best practice to maintain 1 role per function.
How are Lambda invocations?
Sync and Asyc
What are the main service that invoke Lambda sync?
o ALB o API Gateway o CloudFront (Lambda@Edge) o CLI o SDK o Cognito o Step Functions
How can you expose a Lambda function as an HTTP(S) endpoint?
Using an ALB or API Gateway
What you must do to expose Lambda as an HTTP(S) endpoint using ALB?
The Lambda function must be registered in a target group
What particularity has ALB health checks for Lambda?
Health checks if enabled count as a Lambda function request
How is the comunication between the ALB and Lambda?
HTTP request is translated to JSON
What is the main information sent by an ALB to Lambda?
- ELB information
- HTTP method
- Path
- Query String Params (Key/Value)
- Headers (Key/Value)
- Body (for POST, PUT…)
- isBase64Encoded (flag)
What is the main information sent by Lambda to an ALB?
- Status Code
- Description
- Headers (Key/Value)
- Body
- isBase64Encoded (flag)
What is ALB multi-header setting?
/path?name=foo&name=bar
What happens when you enable ALB multi-headers in Lambda?
When you enable multi-headers, HTTP headers and query string parameters that are sent with multiple values are shown as arrays within the AWS Lambda event and response objects
What is Lambda@Edge?
Run a global AWS Lambda alongside your CDN
What is useful for Lambda@Edge?
You can use Lambda to change CloudFront requests and responses:
o After CloudFront receives a request from a viewer (viewer request)
o Before CloudFront forwards the request to the origin (origin request)
o After CloudFront receives the response from the origin (origin response)
o 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
What are Lambda@Edge use cases?
Lambda@Edge: Use Cases • Website Security and Privacy • Dynamic Web Application at the Edge • Search Engine Optimization (SEO) • Intelligently Route Across 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
What are the main services that invoke Lambda async?
- S3 Event Notifications
- SNS
- CloudWatch Events / EventBridge
- CodeCommit (CodeCommit Trigger: new branch, new tag, new push)
- CodePipeline (invoke a Lambda function during the pipeline, Lambda must callback)
Where are async calls placed in Lambda?
The events are placed in an Event Queue
What happens when Lambda fails processing an async call?
Lambda attempts to retry on errors 3 times
o 1-minute wait after 1st, then 2 minutes wait
How can you realize that Lambda has retryied an async call?
If the function is retried, you will see duplicate logs entries in CloudWatch Logs with the same request id
What you need to make sure in your Lambda function to handle retries successfuly?
Make sure the processing is idempotent, that way in case of retries the result is the same
What can you use for failed async invoked Lambda functions?
Can define a DLQ (dead-letter queue) – SNS or SQS – for failed processing (need correct IAM permissions)
What you must use from the CLI when invoking a Llambda function async?
From the CLI you must use –invocation-type Event
What is the response received in the CLI when invoking a Lambda function async?
the response StatusCode will be 202, synonym of asynchronous invocation
What you need to do in EventBridge to async invoke a Lambda function?
You need to create a rule and define an Event pattern or a Schedule
What services do Lambda polls records from them?
- Kinesis Data Streams
- SQS & SQS FIFO queue
- DynamoDB Streams
What is used by Lambda to poll records?
Event Source Mapping
How is your Lambda function invoked by the Lambda Event Source Mapping?
Your Lambda function is invoked synchronously
How does work the Lambda Event Source Mapping with Kinesis?
An event source mapping creates an iterator for each shard, processes items in order.
Start with new items, from the beginning or from timestamp
What happens to items in a kinesis stream that were read by the Lambda Event Source Mapping?
Processed items aren’t removed from the stream (other consumers can read them)
What can you do if the traffic in Kinesis is low and a Lambda Event Source Mapping is poling it?
Use batch window to accumulate records before processing.
How is error handling in Lambda when polling data from streams?
By default, if your function returns an error, the entire batch is reprocessed until the function succeeds, or the items in the batch expire
What does Lambda do to ensure in order processing while polling data from streams?
To ensure in-order processing, processing for the affected shard is paused until the error is resolved
How can you configure the Lambda Event Source Mapping for error handling?
o discard old events
o restrict the number of retries
o split the batch on error (to work around Lambda timeout issues)
Where can discarded events by the Lambda Event Source Mapping go?
to a Destination
How would Lambda Event Souce Mapping poll from SQS and SQS FIFO?
Using Long Polling, specifying a batch size in the range (1-10)
What is the recommended visibility timeout for SQS when a Lambda Event Source Mapping is polling from it?
Set the queue visibility timeout to 6x the timeout of your Lambda function