Lambda Flashcards

1
Q

How much RAM can you provision for a function?

A

Up to 10GB

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What affect does RAM have on CPU and network?

A

If you increase the amount of RAM, you will also improve CPU and network.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you run a container on Lambda? What should you do for other non-Lambda containers?

A

Container must implement the Lambda Runtime API
- Other containers should be run on ECS/Fargate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does Lambda pricing work?

A

Pay per calls:
- First 1,000,000 requests are free, 20 cents per million after
Pay per duration:
- First 400,000 GB-seconds of compute time free (this means 400,000 seconds if RAM is 1GB, 3,200,000 seconds if 128MB)
- 1 dollar per 600,000 GB seconds after

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the key sections of the request payload when converting HTTP to JSON for ALB to Lambda?

A
  • Query String Parameters as Key/Value pairs
  • Headers as Key/Value pairs
  • Body (for POST, PUT…) & isBase64Encoded
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the key sections of the response when converting HTTP to JSON for ALB to Lambda?

A
  • Headers as Key/Value pairs
  • Body & isBase64Encoded
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you use ALB Multi-Header Values with Lambda?

A

Enable the ALB setting for Multi-Header values
- This will allow you to pass in multiple HTTP headers or query string parameters with the same key, which will be shown as an array within the AWS Lambda event

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does Lambda handle Asynchronous Invocations? What considerations should be made because of this?

A

Events are placed in an Event Queue
- Lambda attempts to retry on errors up to 3 times
- Make sure processing is idempotent
- Use a DLQ for failed processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What services invoke Lambda Asynchronously?

A

S3, SNS, CloudWatchEvents/EventBridge

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What sources does Lambda have to poll data from? Is this sync or async?

A

Kinesis Data Stream, SQS & FIFO queue, DynamoDB Streams
- Sync

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does an event source mapping work for Streams (Kinesis and DDB)?

A

Creates an iterator for each shard and processes items in order
- processed items aren’t removed from the stream (other consumers can read them)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How could you set up event source mapping for streams at low and high traffic?

A

Low: use batch window to accumulate records before processing
High: process multiple batches in parallel - up to 10 batches per shard, with in-order processing still guaranteed for each partition key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does an error affect batch processing for event source mapping of streams?

A

By default, an error causes the entire batch to be reprocessed until the function succeeds or the items in the batch expire
- to maintain in-order processing, processing for the affected shard is paused until the error is resolved

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you configure event source mapping re. errors for streams?

A
  • Discard old events
  • Restrict number of retries
  • Split batch on error (to work around Lambda timeout issues)
    Discarded events can go to a Destinatation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you set up event source mapping for SQS?

A
  • ESM will pool SQS (Long polling)
  • Specify batch size
  • Recommended to set queue visibility timeout to 6x the timeout of the lambda function
  • To uses a DLQ, set it up in SQS, NOT in Lambda (Lambda DLQ is only for async invocations)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does Lambda scale with FIFO SQS Queues?

A

Scales up to the number of active message groups

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does an error for Lambda and Queues affect batches?

A

Errors cause batches to be returned to the queue as individual items that might be processed in a different grouping than the original batch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How many stream batches can be processed per shard?

A

If you use parallelization, up to 10 batches per shard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the difference between the Lambda Event and Context Objects?

A

Event Object:
- JSON formatted document contains data for the function to process

Context Object:
- Metadata around the invocations, function and runtime environment, provided by Lambda at runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the purpose of Lambda Destinations? Why are they recommended over DLQ?

A

Configure Lambda to send result (can decide for both success or failure) of function to a Destination (useful for asynchronous invocations)
- SQS, SNS, Lambda and EventBridge
DLQ is only for SQS, but Destinations have a range of options

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Where can you route failed event batches to from an Event Source mapping?

A

SQS or SNS
- Can send to DLQ directly from SQS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Why might a failed S3 event processing in a Lambda function not appear in the destination straight away?

A

S3 events are sent asynchronously - Lambda retries processing 3 times for asynchronous events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the differences in IAM roles regarding Lambda?

A
  • Resource-based policies allow the Lambda function to be invoked by other services
  • Policies attached to the Lambda execution IAM role allow the Lambda function to invoke other services
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What three environment variables allow Lambda to communicate with X-Ray?

A

_X_AMZN_TRACE_ID - contains the tracing header
AWS_XRAY_CONTEXT_MISSING: by default, LOG_ERROR
AWS_XRAY_DAEMON_ADDRESS: the X-Ray Daemon IP_ADDRESS:PORT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is an Edge function? What are the two varieties?

A

Code that you write and attach to CloudFront distributions
- CloudFront runs these functions close to your users to minimize latency
- CloudFront functions and Lambda@Edge

26
Q

What is a use-case of a CloudFront function?

A
  • Cache key normalizations (transform request attributes) to create an optimal cache key
  • Header manipulation (insert/delete HTTP headers)
  • URL rewrites or redirects
  • Request authentication/authorization (create and validate user-generated tokens)
27
Q

Why might you want to use a Lambda@Edge function over a CloudFront function?

A
  • Longer execution time (up to seconds)
  • Adjustable CPU or memory
  • Code can depend on 3rd Party libraries)
  • Network access to use external services for processing
  • File system access or access to the body of HTTP requests
28
Q

Describe CloudFront functions.

A
  • Lightweight functions written in JavaScript
  • For high-scale, latency sensitive CDN customizations
  • Sub-ms startup times, millions of requests/second
  • Used to change Viewer requests and responses (i.e., those between client and CloudFront, as opposed to those between CloudFront and Origin)
  • Native feature of CloudFront (manage code entirely within CloudFront)
29
Q

Describe Lambda@Edge functions.

A
  • Lambda functions written in NodeJs or Python
  • Scales to 1000s of requests/second
  • Can change both View and Origin requests/responses
  • Author your functions in one AWS Region, then CloudFront replicates to its locations
30
Q

How do you give a Lambda function VPC access?

A
  • Define VPC ID, Subnets and Security groups
  • Lambda will create an ENI (Elastic Network Interface) in your subnets
  • Lambda function must have the AWSLambdaVPCAccessExecutionRole
31
Q

How can you give a Lambda function internet access (or a public IP)?

A

Cannot simply deploy to a public subnet
- Deploy to a private subnet that has a NAT Gateway / Instance

32
Q

How can you access AWS services from a Lambda within a VPC / private subnet?

A

Use VPC endpoints

33
Q

How would you improve the performance of a computation heavy Lambda function?

A

Computation heavy means CPU-bound - increase RAM to gain more vCPU.

34
Q

Why should you declare database connection code outside of a function call?

A

If it is within the function, DB connection will be established at every invocation.
If is is outside the function, the connection will be established once and reused in subsequent invocations, due to the execution context of lambda functions (temporary runtime environment).

35
Q

How can you improve performance of a lambda function that requires large files?

A

Use the /tmp directory - 10GB directory (can be used as a transient cache for multiple invocations - helpful to checkpoint your work)

36
Q

How can you encrypt content within the /tmp directory?

A

Generate KMS Data Keys in order to encrypt content.

37
Q

What are Lambda layers?

A

Externalized dependencies that can be used across multiple functions (dependencies do not have to be packaged with the code).

38
Q

How can a Lambda function access EFS file systems within a VPC?

A

Configure Lambda to mount EFS file systems to local directory during initialization
- use EFS Access Points

39
Q

What are the limitations of connecting Lambda to EFS?

A

EFS connection limits: One function instance = one connection
Connection burst limits: may be hit if many functions request connections at once

40
Q

How can you limit the number of concurrent Lambda executions?

A

Set the “reserved concurrency” at the function level.

41
Q

What is the Lambda behaviour if the reserved concurrency limit is exceeded?

A

Synchronous invocation: return ThrottleError 429
Asynchronous invocation: retry automatically and then go to DLQ

42
Q

What can happen if you don’t reserve concurrency and you exceed the default limit?

A

If you exceed 1000 (new default may be 50) concurrent executions, all executions of all functions in your account get throttled.

43
Q

How can you reduce the impact of cold starts?

A

Use Provisioned Concurrency:
- Concurrency is allocated before the function is invoked (i.e., lambda execution environments are created in advance)
- Can use application auto scaling to manage concurrency (schedule or target utilization)

44
Q

How do you include external dependencies in a lambda function (for example AWS X-Ray SDK, Database Clients, etc.)?

A

Install the packages alongside you code and zip it together
-> Upload the zip straight to Lambda if less than 50mb (otherwise to S3)

45
Q

How would you allow multiple accounts to access Lambda code stored in S3 for use in CloudFormation?

A

Use a bucket policy (using the Allow Principal: [Accounts ID…]), and create a CloudFormation execution role to allow it to access S3.

46
Q

How can you deploy a Lambda function from ECR?

A
  • Can deploy a docker container image from ECR up to 10GB large
  • Can use base image or custom as long as it implements the Lambda Runtime API
  • Pack complex/large dependencies into the container
47
Q

What are some strategies for optimizing container images?

A
  • Use AWS-provided base images (cached by Lambda service)
  • Use multi-stage builds (code is built in larger preliminary images - then copy only the artifacts you need in your final container image and discard the preliminary steps)
  • Build from Stable to Frequently Changing (make frequently occurring changes as late in Dockerfile as possible)
  • Use a single repo for functions with large layers (avoid uploading and storing duplicate layers)
48
Q

How are Lambda aliases useful for deployments?

A

Can enable Canary deployment by assigning weights to lambda function aliases (e.g., 95% to v1, 5% to v2)

49
Q

How do aliases work (and what can they not do)?

A

‘Pointers’ to Lambda function versions (NOT to other aliases)

50
Q

What file would you use to deploy a lambda function via CodeDeploy, and what parameters are required?

A

AppSpec.yml
- Name, Alias, CurrentVersion, TargetVersion

51
Q

What is the memory allocation limit for lambda?

A

128MB to 10GB (1MB increments)

52
Q

What is the maximum execution time limit for lambda?

A

15 minutes

53
Q

What is the environment variables size limit?

A

4KB

54
Q

What is the max disk capacity in the function container (/tmp)?

A

512MB to 10GB

55
Q

What is the max number of concurrent executions?

A

1000

56
Q

What is the max lambda function deployment size for compressed zip files?

A

50MB

57
Q

What is the max lambda function deployment size for the uncompressed zip files?

A

250MB

58
Q

What are best practices for lambda initialization?

A

Perform heavy-duty work outside of your function handler
- Connect to databases
- Initialize AWS SDK
- Pull in dependencies or datasets

59
Q

What are best practices for environment variables?

A

Use for data connection strings, S3 bucket, etc.
- Passwords, sensitive values, etc. can be encrypted with KMS

60
Q

What are best practices for minimizing deployment package size?

A

Reduce to runtime necessities
- Break down function if need be
- Remember AWS Lambda limits
- Use layers where necessary

61
Q

How do you declare a Lambda function in a CloudFormation template?

A

Upload all the code as a zip file to an S3 bucket and refer to the object in AWS::Lambda::Function block

62
Q

How would you allow a Lambda function to be used with an Application Load Balancer?

A

Register the Lambda function with a Target Group