Concepts
A deployment package in AWS Lambda contains your function’s code and any dependencies it may have. The package can be uploaded to AWS Lambda either as a .zip file or through an Amazon S3 bucket. Lambda supports deployment packages created as .zip files for functions that are up to 50 MB (zipped, for direct upload) and up to 250 MB (unzipped, including layers).
When writing a function, your package should include the function code and any dependencies which are not included in the AWS Lambda execution environment. For example, if you are writing a Python function that uses the requests
library, your deployment package would need to include that library.
Here’s a simple file structure of a deployment package for a Python Lambda function:
/my-function
|– app.py # Your Lambda function code
|– requests/ # The folder containing the requests module
|– urllib3/ # Any other dependency for requests
|– certifi/
|– chardet/
|– idna/
You can create a deployment package by installing the required libraries within the project directory and zipping the contents. Here’s an example using the command line:
pip install requests -t /path/to/project-dir
cd /path/to/project-dir
zip -r my-deployment-package.zip .
Using Layers in AWS Lambda
Lambda layers are a way to manage function dependencies separately from the function code itself. They can include libraries, a custom runtime, or other dependencies. Layers allow you to keep your deployment package small, which makes development easier, and they can be shared across multiple functions.
The structure of a Lambda layer is similar to that of a function package and should be arranged so that it can be extracted into the /opt
directory. Here is how you might structure the requests
module as a layer:
/requests-layer
|– python/ # Language directory
|– requests/ # Actual dependency files
|– urllib3/
|– certifi/
|– chardet/
|– idna/
To create a layer from the command line, you would navigate to the requests-layer
directory and run:
zip -r requests-layer.zip .
You can then publish this layer to AWS Lambda and include it in a function’s configuration.
Configuration Options for Lambda Functions
Lambda functions have several configuration options that control their execution. These include:
- Memory: The amount of memory available to the function during execution. This is set in increments of 64 MB, from 128 MB to 10,240 MB.
- Timeout: The time that Lambda allows a function to run before stopping it. The maximum timeout is 900 seconds (15 minutes).
- Execution role: The IAM role that Lambda assumes when it executes your function.
- Environment variables: Key-value pairs that you can use to provide dynamic configuration to your function.
- VPC settings: If your function needs to access resources within a VPC, you need to configure its VPC settings.
- Concurrency: The number of instances that are allowed to run your function at the same time. You can set reserved concurrency to make sure that a function has enough instances to handle its load.
- Dead-letter queues (DLQ): Where to send information about failed event source mappings or invocations that couldn’t be retried.
Here’s a configuration example using AWS CLI for setting memory and timeout:
aws lambda update-function-configuration \
–function-name my-function \
–memory-size 512 \
–timeout 60
In summary, when preparing for the AWS Certified Developer – Associate exam, it’s important to understand how to package and deploy your Lambda functions effectively, how to leverage layers for managing dependencies, and the various configuration options that can be tweaked to fine-tune function execution. These concepts are critical for the efficient use of AWS Lambda and the AWS ecosystem as a whole.
Answer the Questions in Comment Section
True or False: When deploying a Lambda function, you can include libraries in your deployment package or use a Lambda Layer.
- True
- False
Answer: True
Explanation: You can include libraries in your deployment package or use a Lambda Layer to share common components across multiple functions.
True or False: AWS Lambda allows you to deploy functions as containers with sizes up to 10GB.
- True
- False
Answer: True
Explanation: Lambda now supports container images of up to 10GB, providing more flexibility in packaging and deploying function code.
Which of the following files is used by AWS SAM to define the Lambda function’s properties?
- deploy.json
- function.json
- template.yaml
- config.json
Answer: template.yaml
Explanation: AWS Serverless Application Model (SAM) uses the template.yaml file to define the properties of serverless resources, including Lambda functions.
True or False: Environment variables in AWS Lambda are encrypted using AWS KMS.
- True
- False
Answer: True
Explanation: Environment variables can be encrypted with AWS KMS, and you can configure them to be encrypted at rest and decrypted automatically when the Lambda function is invoked.
What is the maximum execution timeout for an AWS Lambda function?
- 5 minutes
- 15 minutes
- 30 minutes
- 60 minutes
Answer: 15 minutes
Explanation: AWS Lambda functions have a maximum execution timeout of 15 minutes.
Which AWS service allows you to automate the deployment of Lambda functions?
- AWS CloudFormation
- AWS CodeDeploy
- Both AWS CloudFormation and AWS CodeDeploy
- None of the above
Answer: Both AWS CloudFormation and AWS CodeDeploy
Explanation: Both AWS CloudFormation and AWS CodeDeploy support the automated deployment of AWS Lambda functions.
Which AWS CLI command is used to update Lambda function code?
- aws lambda publish-version
- aws lambda create-function
- aws lambda update-function-configuration
- aws lambda update-function-code
Answer: aws lambda update-function-code
Explanation: The aws lambda update-function-code
command is used to update the code of an existing Lambda function.
True or False: When using Lambda Layers, the size of the deployment package is counted towards the Lambda limits.
- True
- False
Answer: False
Explanation: When using Lambda Layers, the size of the layer does not count towards the deployment package size limit of the function.
Multiple Select: Which of the following are valid methods for deploying Lambda functions? (Select TWO)
- Manually uploading a ZIP file through the AWS Lambda console
- Using an AWS SDK to directly upload code
- Sending code via email to AWS Support for deployment
- Using an Amazon S3 presigned URL to upload the code package
Answer: Manually uploading a ZIP file through the AWS Lambda console, Using an AWS SDK to directly upload code
Explanation: Lambda functions can be deployed by manually uploading a ZIP file through the AWS Lambda console or programmatically using an AWS SDK to directly upload the code.
True or False: When you create an alias for a Lambda function, you can assign different IAM roles to the alias.
- True
- False
Answer: False
Explanation: An alias is a pointer to a specific Lambda function version. IAM roles are attached to the function, not the alias, so you cannot assign different IAM roles to aliases.
What is the purpose of the ‘Dead Letter Queue’ (DLQ) configuration in AWS Lambda?
- To temporarily store messages that cannot be delivered to the destination queue or topic
- To automatically retry failed Lambda function invocations
- To execute Lambda functions without provisioning or managing servers
- To keep track of successful Lambda function executions
Answer: To temporarily store messages that cannot be delivered to the destination queue or topic
Explanation: A Dead Letter Queue (DLQ) is used to hold messages that cannot be processed, allowing you to analyze the messages and take corrective actions.
True or False: AWS Lambda functions have unlimited concurrency by default.
- True
- False
Answer: False
Explanation: AWS Lambda functions have a default safety throttle for concurrency at the account level per region, but you can request a limit increase or set a reserve concurrency limit on a per-function basis.
Great blog post! Really helped clarify Lambda deployment packaging for me.
Does anyone have any tips for optimizing Lambda layers? I’m struggling with keeping the size down.
Can someone explain the difference between inline code deployment and using a deployment package?
Thanks for the walkthrough on Lambda configurations, it made everything so much clearer!
I noticed some performance issues when using Lambda layers, any advice on how to tackle this?
Fantastic post, it was really comprehensive!
How do you manage environment variables in different deployment stages?
Thanks, this blog helped me a lot in understanding Lambda deployment!