DVA-C02 Question 226
Single answerYou are developing an AWS Lambda function to process images uploaded to an S3 bucket. The function requires a third-party image processing library that is not included in the AWS Lambda runtime environment. The library is over 50 MB in size and is required during runtime. How can you package and deploy your Lambda function to ensure the library is available?
- A
Package the library along with your Lambda function code into a single .zip file and upload it directly to the Lambda console.
- B
Create a Lambda layer containing the third-party library and attach it to your Lambda function.
- C
Use the AWS Management Console to install the third-party library directly into the Lambda runtime environment.
- D
Store the third-party library in an S3 bucket and access it dynamically during the Lambda function execution.
Show answer and explanation
Correct answer: B
Explanation
When a Lambda function requires large external dependencies, such as third-party libraries exceeding the deployment package size limit, the best practice is to package those dependencies into a Lambda layer. Layers allow you to store and manage dependencies separately from the function code, making deployment and versioning easier. In this scenario, creating a Lambda layer ensures the library is available to the function without exceeding the size limits or adding unnecessary runtime overhead.
- A. Incorrect.
This approach is not feasible for larger libraries (over 50 MB), as the combined deployment package size has a limit of 50 MB for direct uploads via the Lambda console. The correct approach for larger dependencies is to use Lambda layers.
- B. Correct.
This is the correct answer. Lambda layers allow you to separate external dependencies from your function code and manage them independently. It is an efficient way to package and reuse libraries, especially for dependencies exceeding the direct deployment package size limits.
- C. Incorrect.
This is not a valid option. The AWS Management Console does not provide a feature to install libraries directly into the Lambda runtime environment.
- D. Incorrect.
This is not recommended because it would increase the execution time of the Lambda function due to the additional overhead of downloading the library during each invocation. Dependencies should be packaged with the function code or provided via Lambda layers.