Lambda Flashcards
(36 cards)
What’s Serverless ? Tell name of services which are serverless in AWS ?
- Serverless is a new paradigm in which we don’t need to manage servers anymore. We just deploy our code or functions.
- For example Lambda, Cognito, S3, API Gateway, DynamoDB, Kinesis, Aurora, SNS and SQS etc are managed services in AWS
Why do we need AWS Lambda ? Why it’s preferred over EC2 ?
- EC2 has some drawbacks. EC2 are virtual servers on the cloud. We require manual intervention to add/remove servers. They are limited by CPU and RAM. They are continuously running.
- On the other hand Lambda are virtual functions. We don’t need to manage servers. They run on demand. Even scaling is automated.
What are some salient features of Lambda ?
- Easy Pricing - Pay per request and compute time. Free tier of 10,00,000 requests and 4,00,000 GBs of compute time
- Integrated with whole suite of services and many programming languages
- Easy monitoring with AWS CloudWatch
- Easy to get more resources per Lambda function (up to 10 GBs of RAM)
- INCREASING RAM WILL ALSO IMPROVE CPU & NETWORK
What languages are supported by Lambda ?
- NodeJS
- Python
- Java
- C#
- Ruby
- Custom Runtime API. Community supported, for example Rust & Golang
How can we expose a Lambda function as an HTTPS endpoint ?
We can integrate a Lambda function with an Application Load Balancer or API Gateway. The Lambda function must be registered in a target group.
How can we synchronously and asynchronously invoke a Lambda function ?
- Synchronous Invocation - CLI, SDK, API Gateway, Application Load Balancer. Results are returned right away and error handling must happen client side.
- Asynchronous Invocation - S3, SNS & CloudWatch Events, Event Bridge, Code Commit, Code Pipeline. The events are placed in an Event Queue. Lambda retries 3 times on errors. 1 min wait after first retry. 2 mins wait after second retry. We have to ensure our function is idempotent because of retries. We can define SQS or SNS as a Dead Letter Queue for failed processing.
- We can invoke Lambda function from a cron job trigger using EventBridge
- We can invoke Lambda function on STATE change in CodePipeline
How can we deliver S3 event notifications to Lambda functions
- S3 event notifications - We can generate events every time an object is created, removed, restored or replicated in S3 bucket. (APIs - S3: ObjectCreated, S3: ObjectRemoved, S3:ObjectRestored, S3:Replication)
- Use case - Create thumbnails of images uploaded in S3
3.Typically events are delivered in seconds, but sometimes it could take more than a minute or two
- For ensuring successful event notifications for every write, we can enable versioning on the S3 bucket
What is Lambda event source mapping ?
Lambda Event Source Mapping is a feature in AWS Lambda that allows your Lambda function to automatically consume events from supported AWS services without needing to write polling or event-handling logic manually.
Source of events
1. Kinesis Data Streams
2. SQS & SNS FIFO Queue
3. DynamoDB Streams
What it does:
It creates a mapping between an event source (like a stream or queue) and a Lambda function, so that events from the source are automatically sent to the Lambda function for processing.
How does Lambda handles streams, like from Kinesis & DynamoDB ?
- An event source mapping creates iterators for each shard, processing each item in order.
- We have options to configure how we need to consume the data from stream. Options are to start with new items, or from the beginning or from a particular timestamp
- Processed items are not removed from the stream
- Low traffic can lead to accumulation of records and then process them in batches
- We can process multiple batches in parallel (upto 10 batches per shard and in ordered processing based on each partition key)
How does Lambda handles errors while consuming records from a stream ?
1.By default if our function returns an error, entire batch is reprocessed until the function succeeds.
2. To ensure in order processing, processing of affected shard is paused till error is resolved
3. We can configure Event Source Mapping to discard old events and restrict the number of retries.
4. Discarded events can go to a configured Lambda Destination or DLQ
What are Lambda event and context objects
In AWS Lambda, event and context objects are the two main parameters passed to your Lambda function when it is invoked.
⸻
- event object
The event object contains all the input data for your function. This includes:
• Request body, headers, path parameters, etc. (for HTTP/API Gateway triggers)
• Records from event sources like S3, DynamoDB, Kinesis, etc.
- context object
The context object provides runtime information about the Lambda function and the invocation.
context.functionName // Name of the Lambda function
context.functionVersion // Version of the function
context.invokedFunctionArn // ARN used to invoke the function
context.memoryLimitInMB // Memory allocated
context.awsRequestId // Unique request ID for the invocation
context.logGroupName // CloudWatch Logs group name
context.logStreamName // CloudWatch Logs stream name
context.getRemainingTimeInMillis() // Time left before timeout (in ms)
What are Lambda Destinations?
AWS Lambda Destinations are a feature that lets you asynchronously send the result of a Lambda function execution to another AWS service — without writing additional code to handle success or failure routing.
It’s especially useful for asynchronous invocations, such as when Lambda is triggered by EventBridge, S3, or direct asynchronous calls.
What are lambda environment variables?
Lambda environment variables are key-value pairs that you can define and use to configure your function without changing code.
They help you manage settings like database connection strings, secrets (ideally via AWS Secrets Manager), feature flags, or other configuration parameters across different environments (e.g., dev, test, prod).
How does Lambda do logging, monitoring, and tracing?
AWS Lambda provides logging, monitoring, and tracing through tight integration with Amazon CloudWatch and AWS X-Ray.
- Every Lambda function automatically streams console.log, console.error, or equivalent language logging output to CloudWatch Logs.
- Lambda automatically publishes metrics to Amazon CloudWatch
- When enabled, X-Ray traces requests through your Lambda and connected services (e.g., DynamoDB, HTTP calls).
It automatically runs the XRay daemon for us. We need to to use XRay SDK in our code.
We also need to ensure Lambda function has correct IAM execution role (managed policy is called AWsXRayDaemonWriteAccess)
What are environment variables used to communicate with XRay?
- AWS_XRAY_DAEMON_ADDRESS
IP address and port of the local X-Ray daemon (e.g., 169.254.79.2:2000) - AWS_XRAY_CONTEXT_MISSING
Defines behavior when trace context is missing (LOG_ERROR or RUNTIME_ERROR) - LAMBDA_TASK_ROOT
Path to the Lambda task root, used by SDKs - LAMBDA_RUNTIME_DIR
Path to the Lambda runtime, used by X-Ray SDKs
What are Lambda@Edge and CloudFront Functions?
Lambda@Edge and CloudFront Functions are two ways to run custom logic at Amazon CloudFront edge locations (closer to your users)
Lambda@Edge allows you to run AWS Lambda functions at CloudFront edge locations in response to CDN events, such as viewer request, origin request, etc.
Use Cases:
• URL rewrites or redirects
• Authentication and authorization
• Header manipulation
• Content customization based on location or cookies
• Origin selection (multi-origin routing)
CloudFront Functions are lightweight JavaScript functions that run synchronously at CloudFront edge locations with ultra-low latency.
Use Cases:
• URL rewrites and redirects
• Header manipulation
• Simple bot filtering
• Access control via cookies, headers, or query params
How can we access resources that are deployed inside our own VPC from Lambda?
By default Lambda is deployed inside a separate VPC owned by AWS. Lambda cannot access resources like RDS that are deployed inside a private subnet in our own VPC.
However this is possible if we deploy the Lambda function inside our own VPC. We need to create a VPC Id, a subnet and security group.
Behind the scene Lambda will create an ENI (Elastic Network Interface) inside our subnet. For this our Lambda will require AWSLambdaVPCAccessExecutionRole
Can we access internet if we deploy Lambda function inside our own VPC?
By default Lambda functions deployed inside our own VPC don’t have access to internet or any public IP.(Even if it’s deployed inside a public subnet)
However we can deploy our Lambda functions inside a private subnet and we use a NAT Gateway to provide access to internet .
What are some of the Lambda configurations you can do wrt RAM & CPU ?
- We can increase RAM from 128 MB to 10 GB in 1 MB increments
- The more RAM we add, more vCPU credits we can get
- At 1792 MB RAM, a function has compute power equivalent to full 1 vCPU
- After 1792 MB we get more than 1 CPU and can utilise multithreading to benefit from it
If our application is CPU bound ie computation heavy, then we should increase RAM
Default timeout of a Lambda function is 3 seconds and and maximum is 900 seconds (15 minutes)
If our function is taking more than 15 minutes to execute then instead of Lambda we should use Fargate, ECS or EC2
What is Lambda Execution Context?
The Lambda Execution Context refers to the temporary runtime environment that AWS Lambda creates to run your function code. It’s crucial for understanding performance and optimization of AWS Lambda functions.
When you invoke a Lambda function for the first time (or after a cold start), AWS:
• Creates a new execution environment (a lightweight sandbox).
• Downloads your code and initializes the runtime (e.g., Node.js, Java, Python).
• Executes the initialization code (outside the handler function).
• Runs the handler logic.
• For subsequent invocations, the same execution environment may be reused, allowing:
• Faster response times (known as a warm start).
• Reuse of resources (e.g., database connections, SDK clients).
What Persists Between Lambda Invocations (Warm Start) ?
- Initialized global variables.
- Open database or network connections.
- Files stored in the /tmp directory (up to 512 MB).
What is a Lambda Layers?
A Lambda Layer is a ZIP archive that contains libraries, custom runtimes, or other function dependencies. When you include a layer in your Lambda function, it is mounted at /opt inside the execution environment.
Use Cases
• Share common libraries (e.g., AWS SDK, NumPy, pandas).
• Include binaries or native libraries (e.g., FFmpeg, Chromium).
• Distribute custom runtimes.
• Centralize and version your utilities across functions.
How does we mount files to be accessed from Lambda?
Lambda functions can access EFS file systems if they are running in a VPC.
We have to configure Lambda to mount EFS file systems to local directory during initialisation
There is a limit of one connection per Lambda function
What is a Lambda Layer?
A Lambda Layer is a ZIP archive that contains libraries, custom runtimes, or other function dependencies. When you include a layer in your Lambda function, it is mounted at /opt inside the execution environment.
Use Cases
• Share common libraries (e.g., AWS SDK, NumPy, pandas).
• Include binaries or native libraries (e.g., FFmpeg, Chromium).
• Distribute custom runtimes.
• Centralize and version your utilities across functions.