official aws lambda layer for @duckdb/node-api #184
Replies: 11 comments 5 replies
-
There is not. The lambda layer for the old Node client isn't "official", either; it's provided by a member of the community. I'm not very familiar with lambda layers so I don't know what's involved. I don't understand enough about the lambda environment to know why you got the error above, but my guess is there's something different about the Node version or configuration. I'd welcome any concrete suggestions or PRs to make this work. I have a fair amount of other items on my list right now, so I'm not likely to have time to look into this anytime soon. |
Beta Was this translation helpful? Give feedback.
-
figured it out actually. It was due to lambda function running on a different chip to my local machine and the .node binding dependency not being installed when the function was deployed on aws. I wouldn't want to use a random lambda layer as abit of a security risk but these are the steps i did to get it working (for others wanting to fix the same issue):
![]()
Now duckdb will be executable inside a lambda function without using a custom docker image. |
Beta Was this translation helpful? Give feedback.
-
Nice! I can confirm that those three packages ( |
Beta Was this translation helpful? Give feedback.
-
Probably worth documenting this and adding it to the README. I'll keep this issue open to track that. |
Beta Was this translation helpful? Give feedback.
-
good idea! I think this would allow alot more people who are just using front end frameworks like aws amplify to use duckdb in their lambda functions :) |
Beta Was this translation helpful? Give feedback.
-
When bundling a lambda function with SST v2, the installation of the native binary can be can be configured as follows:
|
Beta Was this translation helpful? Give feedback.
-
That's much better !🤗 (if your using sst) |
Beta Was this translation helpful? Give feedback.
-
What's the use case for DuckDB in lambdas? Given they throw their memory and filesystem away after each call. You could mount a filesystem into the lambda but that wouldn't be safe unless invocation concurrency was hard coded to 1. Asking because I'm porting from DuckDB to DynamoDB for AWS deployment into lambdas myself as I couldn't see a way to use DuckDB in lambdas |
Beta Was this translation helpful? Give feedback.
-
The primary use is if your data is already in S3 or s3 tables and you have a complex analytical query your running on every request you don't want a separate database for (or your trying to reduce athena use), you can use the /tmp folder to go out of memory if you really need too. Using dynamodb for analytical queries would be a world of pain. You would be better with aurora but then there is more to manage and has its own problems. |
Beta Was this translation helpful? Give feedback.
-
Hello; After struggling for almost 2 days to deploy that on a AWS lambda + serverless context; I feel like it's worth it to share experience, potentially being the base of a future layer. A bit of context :
FailsI made an initial attempt first, having in mind "not deploying a layer at all"; and faced several troubles, with one of them actually being unresolvable.
Of course, we could go crazy and configure the lambda runtime to match the OS one or build inside a VM or something; but that feels hacky as hell and is not scalable by any means.
I discovered that npm can do it, with
SuccessAfter hours of researches and testing, I succeeded on deploying everything with the bindings inside a layer, with the following setup : serverless.yml frameworkVersion: '4'
provider:
name: aws
runtime: nodejs20.x
# ...
environment:
# Needed despite what AWS documentation says
NODE_PATH: './:/opt/nodejs/node_modules'
layers:
duckdb:
# The local path of the layer folder to build
path: ./duckdb-layer
description: DuckDB bindings for Node.js
package:
individually: true
patterns:
# Those ones are DuckDB bindings for specific functions
# they are extracted in ./duckdb-layer
- '!node_modules/@duckdb/node-bindings'
- '!node_modules/@duckdb/node-bindings-darwin-arm64'
- '!node_modules/@duckdb/node-bindings-darwin-x64'
- '!node_modules/@duckdb/node-bindings-linux-arm64'
- '!node_modules/@duckdb/node-bindings-linux-x64'
- '!node_modules/@duckdb/node-bindings-win32-x64'
build:
esbuild:
# See below
configFile: ./esbuild.config.js
functions:
sync-subpage-user-aggregate:
# ... Function configuration
layers:
# Reference the layer to use it
- !Ref DuckdbLambdaLayer esbuild.config.js /**
* @import {BuildOptions} from "esbuild"
*/
/** @type{() => BuildOptions} */
export default () => ({
bundle: true,
minify: true,
sourcemap: false,
keepNames: true,
format: 'esm',
platform: 'node',
banner: {
// This hack will fix actual lambda ESM environment
// versus esbuild require-wrapping logic (basically make `require` work)
js: "import { createRequire as topLevelCreateRequire } from 'module';\n const require = topLevelCreateRequire(import.meta.url);",
},
// Mark bindings as external (not embedded inside the function code)
external: [
'@duckdb/node-bindings',
'@duckdb/node-bindings-darwin-arm64',
'@duckdb/node-bindings-darwin-x64',
'@duckdb/node-bindings-linux-arm64',
'@duckdb/node-bindings-linux-x64',
'@duckdb/node-bindings-win32-x64',
],
// And actually exclude them totally from the build
exclude: [
'@duckdb/node-bindings',
'@duckdb/node-bindings-darwin-arm64',
'@duckdb/node-bindings-darwin-x64',
'@duckdb/node-bindings-linux-arm64',
'@duckdb/node-bindings-linux-x64',
'@duckdb/node-bindings-win32-x64',
]
}) And now here comes the funny part; creating the layer. Create base layer folderWe're going to create the layer inside I actually used npm install --prefix ./duckdb-layer @duckdb/node-bindings @duckdb/node-bindings-linux-x64 --force With Inside
[Optional] Remove dev-platform specific bindingsFrom there, I had to clean that from having unwanted bindings for
Fix file architectureDespite what AWS configuration docs says, if you deploy like this, your lambda won't actually find the module inside You would end up with
The exact file architecture that works for me is :
So basically cut-paste Fix NODE_PATHDespite what AWS configuration docs says, if you deploy like so your lambda still won't find the module. You would still end up with
For this to work, you have to adjust the |
Beta Was this translation helpful? Give feedback.
-
For what it's worth for anyone landing here; I created a request inside duckdb-nodejs-layer in the hope to make happen a new public layer with the official new duckdb neo bindings. If anyone feel to give that a starting point or help on that, that would be awesome. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, is there an official aws lambda layer for @duckdb/node-api or we must include as part of our own deployment pipeline?
Currently getting this error on a clean lambda function. I guess most people would also get this:
Many thanks!
Beta Was this translation helpful? Give feedback.
All reactions