![[EN] ChDB or DuckDB on AWS Lambda: A Technical Comparison — #1 Environment Setup](https://prod-assets.cosmic.aws.dev/a/3JZKYVdXLPyZyTE2iYJzgYTfkrR/46e2.webp)
[EN] ChDB or DuckDB on AWS Lambda: A Technical Comparison — #1 Environment Setup
Reviewing and understanding the different ways to install DuckDB and ChDB, along with their respective extensions, within AWS Lambda, with the goal of conducting a technical comparison to understand how these options impact the use of AWS Lambda when developing data pipelines.
There are several articles demonstrating how AWS Lambda, together with either DuckDB or ChDB, can serve as the foundation for building lightweight data pipelines and/or processing low data volumes, with the goal of making analyzed data available for downstream consumption.
As a data engineer, I really like this approach because (1) it is cost-efficient for the amount of data that can be processed with AWS Lambda, (2) it is easy to maintain since development is based on SQL and Python, and (3) it has a relatively short learning curve for people transitioning into data engineering.
However, there are two options to use with AWS Lambda: DuckDB or ChDB. The key is understanding when to choose one over the other. Among the many questions we can ask when evaluating and choosing between them, let's start by answering the basics:
🤔💭 How can I install DuckDB/ChDB on AWS Lambda, and how does this affect my day-to-day activities as a data engineer when dealing with this scenario?
And this is highly important because it determines what the process is, what knowledge I need to use in the process, what problems I may encounter and how to solve them, and ultimately, how this impacts the AWS Lambda environment when developing the solution.
Ways to Create an AWS Lambda Function
When we talk about AWS Lambda, we are usually referring to functions, since they are the core component of the service responsible for executing the code we write—typically, our application logic.
With this in mind, the key question for this service is: How can I create an AWS Lambda function with my code and its dependencies? There are generally three approaches:
- ZIP file archive
- AWS Lambda Layer
- Container image
Although AWS CDK could be considered a fourth option, it generally relies partially or entirely on the approaches listed above.
Each option is designed around a different approach to developing code within AWS Lambda and creating the function. Here is a summary of some key points for each option that you should keep in mind.
| ZIP File Archive | AWS Lambda Layer | Container Image |
|---|---|---|---|
Approach | Code + Dependencies | Dependencies | Code + Dependencies |
Required knowledge | Package manager and/or language virtual environment + Linux commands | Package manager and/or language virtual environment + Linux commands | Docker + package manager and/or language virtual environment + Linux commands |
Artifacts | ZIP file | ZIP file | Container image |
Size limit | 250 MB unzipped | Up to 5 layers per function; 250 MB unzipped + code | 10 GB uncompressed |
Version management | Uploaded ZIP file | Versions specified for the Lambda Layer | Tags |
Reusability | Reuse the same ZIP file | A Lambda Layer can be shared | Reuse the same image |
Lambda development editor | Depends on the ZIP file size | Yes | No |
Based on this summary, my personal guideline as a data engineer for choosing each function creation approach is as follows:
- If I want to test new environments that are not yet production-ready and are relatively small, ZIP File Archive is the option to use.
- If I need package management in production environments, AWS Lambda Layers are the way to go.
- If the dependencies are large or complex, or if I need greater control over the runtime environment, Container Image is the answer.
With this context in mind, the question is: 🤔💭 Which method should I use to create an AWS Lambda function with DuckDB and ChDB?
Let's Start with DuckDB 🦆
import json
import time
import shutil
import duckdb
def lambda_handler(event, context):
start = time.time()
print(duckdb.__version__)
con = duckdb.connect(database=':memory:')
con.execute("SET memory_limit = '140MB';")
con.execute("SET home_directory='/tmp';")
/***** LA CONFIGURACION PARA LEER****/
end_setup = time.time()
print("Elapsed time (setup):", end_setup - start, "seconds")
# Query leyendo múltiples archivos parquet
query = """
SELECT count(1) as counting
FROM read_parquet('s3://dataset--test--structured-data/parquet_files/25MB/*.parquet')
"""
result = con.execute(query)
end_execution = time.time()
print("Elapsed time (execution):", end_execution - end_setup, "seconds.")
row = result.fetchone()
print(row[0])
con.close()The purpose of the code to be executed is quite simple: read data from a table stored as Parquet files in an S3 bucket and check how many records the table contains.
The challenge of setting up the DuckDB environment lies in the following points:
- The following DuckDB extensions must be installed to run the code: HTTPFS and Parquet (although Parquet is already included by default in DuckDB).
- Use an appropriate DuckDB version. At the time of writing, version 1.4.5 will be used, since older versions may have their own installation quirks (and I will discuss this later).
For reference, the AWS Lambda function used in the tests was configured with the following settings:
AWS Lambda Parameter | Value |
|---|---|
Memory | 256 MB |
Timeout | 2 minutes |
Runtime | Python 3.13 |
Permissions | AmazonS3FullAccess (temporarily) |
Processing architecture | x86_64 |
Approach 1️⃣ — ZIP Files or ZIP File Archives
The structure of the ZIP file should be as follows:
duckdb-zip-lambda.zip/
├── package/
│ └── duckdb/
│ └── ...
│ └── extensions/
├── httpfs.duckdb_extension
└── parquet.duckdb_extensionAWS CloudShell was used to run the test, and the sequence of commands used to set up the environment is shown below:
### Step 1: Define the workspace
mkdir duckdb_lambda
cd duckdb_lambda
### Step 2: Create the function that AWS Lambda will use
nano lambda_function.py ##poner codigo base
### Step 3: Create the required directories
## Step 3.1: Where the packages that AWS Lambda will use are stored
mkdir package
## Step 3.2: Where the DuckDB extensions will be located in AWS Lambda
mkdir package/extensions/
## Step 3.3: Temporary storage for DuckDB extensions
mkdir extensions
### Step 4: Install the packages to be used in AWS Lambda
## Step 4.1: Install the required Python packages for AWS Lambda development
pip install --target ./package duckdb==1.4.5
## Step 4.2: Install the required DuckDB extension: HTTPFS
curl -L \
https://extensions.duckdb.org/v1.4.5/linux_amd64/httpfs.duckdb_extension.gz \
-o extensions/httpfs.duckdb_extension.gz
gunzip extensions/httpfs.duckdb_extension.gz
mv extensions/httpfs.duckdb_extension package/extensions/
## Step 4.3: Install the required DuckDB extension: Parquet
curl -L \
https://extensions.duckdb.org/v1.4.5/linux_amd64/parquet.duckdb_extension.gz \
-o extensions/parquet.duckdb_extension.gz
gunzip extensions/parquet.duckdb_extension.gz
mv extensions/parquet.duckdb_extension package/extensions/
### Step 5: Build the ZIP File Archive to create the AWS Lambda function
cd package
zip -r ../duckdb_zip_lambda.zip .
cd ..
zip duckdb_zip_lambda.zip lambda_function.py
### Step 6: Upload it to an S3 bucket
aws s3 cp duckdb_zip_lambda.zip s3://code-lambda-zip-package/duckdb/With this process, the environment configuration within AWS Lambda required to use the extensions needed by DuckDB should be as follows:
EXTENSIONS_DIR = "/var/task/extensions"
con.execute(f"SET extension_directory = '{EXTENSIONS_DIR}'")
# Loading extensions
for ext in ("httpfs", "parquet"):
con.execute(f"LOAD '{EXTENSIONS_DIR}/{ext}.duckdb_extension'")While running the test, I learned a lot of details that can help you better understand and control the process, especially if you are new to it:
⚠️ When using the
--only-binary parameter with pip , you should check which DuckDB version is actually being installed. In my case, the default version installed was 1.2.2, which led me to download the extensions based on the linux_amd64_gcc4 architecture because they are compatible with the installed DuckDB version, rather than linux_amd64 , which is intended for newer DuckDB versions.
⚠️ Using the
--platform parameter with pip helps you select packages based on the target processor architecture. Keep this in mind if you need to use the ARM64 architecture with AWS Lambda or encounter an architecture-related issue. Since I was using AWS CloudShell, I did not run into any problems because it uses x86_64 by default.⚠️ Using
pip together with --only-binary=:all: tells pip to find for precompiled packages that are available for installation. This can add complexity to the download process because a compatible package may not be available for the specified configuration. My personal recommendation is to install the Python package without the --only-binary option to avoid unnecessary issues. However, if you do need to use it, make sure to verify package availability for your target configuration.
⚠️ The
extensions folder inside the package folder is required to make DuckDB extensions available to AWS Lambda. Don't forget to create it.⚠️ You should use S3 to upload the ZIP file because the compressed file is approximately 56 MB, which does not allow it to be uploaded directly from your local computer. When using code storage mode with S3, Copy mode (default) is recommended for retrieving the code.
⚠️ If the ZIP file is too large, AWS Lambda may prevent you from enabling the code editor. Make sure to check whether this affects your development workflow!

⚠️ Always check the size and number of each DuckDB extension you plan to install, as this directly affects the final size of the ZIP file.
☝️ Bonus: If you don't need to install any DuckDB extensions, you can skip steps 4.2, 4.3, 3.2, and 3.3 from the installation sequence. If you are only installing DuckDB for use with AWS Lambda, you can upload the ZIP file directly from your local computer to AWS Lambda.
Approach 2️⃣ — AWS Lambda Layers
The process here is very similar to Approach #1, but there is one critical difference: the structure inside the ZIP file. Although both methods use the same basic mechanism—packaging the required packages and dependencies into a ZIP file—they serve different purposes and provide different functionality for managing development in AWS Lambda.
The ZIP file must have the following structure:
duckdb-layer.zip/
├── python/
│ └── duckdb/
│ └── ...
└── duckdb/
└── extensions/
└── v1.4.5/
└── linux_amd64/
├── httpfs.duckdb_extension
└── parquet.duckdb_extensionTo create this structure inside the ZIP file, use the following sequence of commands. Although these commands were run in AWS CloudShell, they can serve as a starting point for building the process you need.
### Step 1: Create build directories
## Step 1.1: Create a directory for Python packages
mkdir ./asset/python
## Step 1.2: Create a directory for DuckDB extensions
mkdir -p ./asset/duckdb/extensions/v1.4.5/linux_amd64
### Step 2: Install DuckDB into the python/ directory
pip install duckdb==1.4.5 --target ./asset/python
### Step 3: Download DuckDB extensions
## Step 3.1: Install the required DuckDB extension: HTTPFS
curl -L \
https://extensions.duckdb.org/v1.4.5/linux_amd64/httpfs.duckdb_extension.gz \
-o ./asset/duckdb/extensions/v1.4.5/linux_amd64/httpfs.duckdb_extension.gz
gunzip ./asset/duckdb/extensions/v1.4.5/linux_amd64/httpfs.duckdb_extension.gz
## Step 3.2: Install the required DuckDB extension: Parquet
curl -L \
https://extensions.duckdb.org/v1.4.5/linux_amd64/parquet.duckdb_extension.gz \
-o ./asset/duckdb/extensions/v1.4.5/linux_amd64/parquet.duckdb_extension.gz
gunzip ./asset/duckdb/extensions/v1.4.5/linux_amd64/parquet.duckdb_extension.gz
### Step 4: Create the ZIP file from the Layer root directory
cd asset
## Step 4.1: Compress DuckDB
zip -r ../asset/duckdb-layer.zip python
## Step 4.2: Compress the DuckDB extensions
zip -r ../asset/duckdb-layer.zip duckdb
### Step 5: Upload the ZIP file to S3
aws s3 cp duckdb-layer.zip s3://code-lambda-zip-package/duckdb/aws_lambda_layer/This ZIP file is used to create an AWS Lambda Layer, which can then be attached to AWS Lambda functions. Once the AWS Lambda function is integrated with the AWS Lambda Layer, the code configuration is as follows:
EXTENSIONS_DIR = "/opt/duckdb/extensions"
con.execute(f"SET extension_directory = '{EXTENSIONS_DIR}'")
# Loading extensions
con.execute("LOAD httpfs")
con.execute("LOAD parquet")While running the test, I identified some additional details to keep in mind when building with this approach:
⚠️ Although the ZIP file structure shows only the main package,
python , additional components may be installed inside the python folder. This is normal, as they are supporting components required by the main package and therefore need to be included.
⚠️ If you are going to change the DuckDB version or the platform architecture used by AWS Lambda, always adjust the DuckDB extensions directory structure inside the ZIP file. Think of it as follows:
duckdb/extensions/v<VERSION_TO_USE>/<PLATFORM_TO_USE>/ , and then make sure the structure inside the ZIP file matches accordingly.⚠️ You should use S3 to upload the ZIP file because the compressed file is approximately 57 MB, which does not allow it to be uploaded directly from your local computer. When using code storage mode with S3, Copy mode (default) is recommended for retrieving the code.
⚠️ The recommendation regarding the use of
--platform and --only-binary with pip also applies to this approach. Whether you need them depends on your specific use case.⚠️ If you only want to install DuckDB without any extensions, you do not need to perform steps 3, 1.2, and 4.2.
Approach 3️⃣ — Container Image
The folder structure for building the container image should be as follows:
container_image_duckdb/
├── Dockerfile
├── requirements.txt
└── lambda_function.pyTo achieve this, you need to understand three key components: the Dockerfile, requirements.txt, and the code in the lambda_function.py file.
For requirements.txt, the following content is essential. This file defines all the Python packages that will be installed using
pip when building the image, so you can add any packages you need to this file.duckdb==1.4.5The following is the lambda_function.py file containing the code to be executed. Based on the template we defined, the code to be added would be as follows:
# Loading extension
extensions_dir = "/opt/duckdb/extensions"
for ext in ("httpfs", "parquet"):
ext_path = os.path.join(
extensions_dir,
f"v{duckdb.__version__}",
"linux_amd64",
f"{ext}.duckdb_extension"
)
con.execute(f"LOAD '{ext_path}'")And finally, the Dockerfile, which is responsible for building the container image.
FROM public.ecr.aws/lambda/python:3.13
ARG DUCKDB_VERSION=1.4.5
ARG DUCKDB_PLATFORM=linux_amd64
ENV HOME=/tmp
ENV DUCKDB_EXTENSION_DIRECTORY=/opt/duckdb/extensions
COPY requirements.txt ${LAMBDA_TASK_ROOT}/requirements.txt
RUN pip install --no-cache-dir \
-r ${LAMBDA_TASK_ROOT}/requirements.txt
RUN dnf install -y gzip && \
dnf clean all
RUN mkdir -p \
${DUCKDB_EXTENSION_DIRECTORY}/v${DUCKDB_VERSION}/${DUCKDB_PLATFORM} && \
for ext in httpfs parquet; do \
echo "Descargando extension: ${ext}" && \
curl -fsSL --retry 3 \
"https://extensions.duckdb.org/v${DUCKDB_VERSION}/${DUCKDB_PLATFORM}/${ext}.duckdb_extension.gz" \
| gzip -d > \
"${DUCKDB_EXTENSION_DIRECTORY}/v${DUCKDB_VERSION}/${DUCKDB_PLATFORM}/${ext}.duckdb_extension" \
&& test -s \
"${DUCKDB_EXTENSION_DIRECTORY}/v${DUCKDB_VERSION}/${DUCKDB_PLATFORM}/${ext}.duckdb_extension" \
|| exit 1; \
done && \
ls -lh ${DUCKDB_EXTENSION_DIRECTORY}/v${DUCKDB_VERSION}/${DUCKDB_PLATFORM}/
COPY lambda_function.py ${LAMBDA_TASK_ROOT}/lambda_function.py
CMD ["lambda_function.lambda_handler"]Finally, you need to build the container image and push it to Amazon ECR so that you can create the AWS Lambda function. Keep in mind that you should first create the repository where you will manage the different versions of the container image.
The following sequence of commands, which I used to run the tests in AWS CloudShell, can be used to accomplish this:
## Paso 1: Crear el container image desde el folder de trabajo
docker build -t <CONTAINER_IMAGE_NOMBRE> .
## Paso 2: Autenticarse para utilizar AWS ECR
aws ecr get-login-password --region <REGION_ECR_REPOSITORIO> | docker login --username AWS --password-stdin <CODIGO_CUENTA_AWS>.dkr.ecr.<REGION_ECR_REPOSITORIO>.amazonaws.com
## Paso 3: Tagger el container image con datos del repositorio de AWS ECR
docker tag <CONTAINER_IMAGE_NOMBRE>:latest <ARN_ECR_REPOSITORIO>:latest
## Paso 4: Subir el container image hacia AWS ECR
docker push <ARN_ECR_REPOSITORIO>:latestWhile running the test, I identified some additional details to keep in mind when using this approach that may help guide you:
⚠️ A recurring issue I encountered was related to downloading and locating the DuckDB extensions. This can happen if the
extensions folder is not created in the Dockerfile or if the AWS Lambda code does not correctly reference the extension when loading it. Make sure to carefully check where the extensions are located throughout the entire process. If necessary, start over with a clean build and try again.
⚠️ Always verify that the AWS Lambda function handler is correctly configured and referenced. Otherwise, AWS Lambda will not be able to invoke the function correctly.

⚠️ The recommendation regarding the use of
--platform and --only-binary with pip also applies to this approach. Whether you need to use them depends on your specific use case.
What About ChDB? ❓
In the case of ChDB, there is one clear and straightforward issue: its dependencies make it quite large.
This means that options such as ZIP files and AWS Lambda Layers are not viable because of the 250 MB size limitation. As a result, the only option is to install it using a container image.
The good news is that ChDB already includes the dependencies we need to perform the same test we ran with DuckDB, so there is no need to install them separately. The code for this exercise is as follows:
import time
import chdb
import json
def lambda_handler(event, context):
start = time.time()
print("chDB version:", chdb.__version__)
end_setup = time.time()
print("Elapsed time (setup):", end_setup - start, "seconds")
query = """
SELECT count(1) as couting
FROM s3(
's3://dataset--test--structured-data/parquet_files/25MB/*.parquet',
'Parquet'
)
"""
result = chdb.query(query, 'CSV')
end_execution = time.time()
print("Elapsed time (execution):", end_execution - end_setup, "seconds.")
print(result)
The contents of the requirements.txt and Dockerfile files needed to build the image, respectively, are as follows:
chdb==4.3.0FROM public.ecr.aws/lambda/python:3.13
# Lambda working directory
WORKDIR ${LAMBDA_TASK_ROOT}
# Copy dependencies
COPY requirements.txt ${LAMBDA_TASK_ROOT}/
# Install chDB
RUN pip install -r requirements.txt
# Copy Lambda code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}/
# Lambda handler
CMD [ "lambda_function.lambda_handler" ]The container image recommendations for DuckDB also apply here.
Tips, Evaluation, and Conclusion
Here are some key recommendations that apply across all the installation approaches covered in this article:
✅ Always specify the version you want to install explicitly.
✅ Install the latest versions to avoid dependency issues. Keep in mind that the versions of any dependencies you install should also be compatible with the main package.
Based on everything discussed, reviewed, and demonstrated throughout this article, we can draw the following conclusions:
✨ DuckDB offers more flexibility than ChDB when developing solutions for AWS Lambda. It provides more deployment options that can be aligned with the different stages of the AWS Lambda development lifecycle.
✍🏻 DuckDB has a lower learning curve for this use case. With ChDB, the learning curve can be steeper because it requires additional knowledge of technologies and concepts that may be more complex, such as Docker.
👀 With DuckDB and AWS Lambda Layers, you can continue using the AWS Lambda code editor to develop your code, whereas this is not possible with ChDB when using a container image.
When it comes to managing the runtime environment, my personal choice is: 🏆 DuckDB is the better option — Congratulations! 🎉🦆🎉
Reference Documentation
These are the reference documents I used to build this article. If you want to explore the topic in more detail, you can find them here:
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article