AWS Lambda
Run bknd inside AWS Lambda
Installation
To get started with AWS Lambda and bknd you can either install the package manually and follow the descriptions below, or use the CLI starter:
Create a new Bun CLI starter project by running the following command:
Serve the API
To serve the API, you can use the serveLambda
function of the AWS Lambda adapter.
Although the runtime would support database as a file, we don’t recommend it. You’d need to also bundle the native dependencies which increases the deployment size and cold start time. Instead, we recommend you to use LibSQL on Turso.
Serve the Admin UI
Lambda functions should be as small as possible. Therefore, the static files for the admin panel should not be served from node_modules like with the Node adapter.
Instead, we recommend to copy the static files and bundle them with the lambda function. To copy the static files, you can use the copy-assets
command:
This will copy the static files to the static
directory and then serve them from there:
Deployment
To deploy a lambda function, you could follow these steps:
- Create an IAM role with a trust policy that allows lambda to assume the role.
- Attach the
AWSLambdaBasicExecutionRole
policy to the role. - Bundle the lambda function with the static files (e.g. using esbuild)
- Create a zip file with the bundled lambda function
- Create a lambda function
- Create a function URL for the lambda function & make it publicly accessible (optional)
Depending on your use case, you may want to skip step 6 and use the AWS API Gateway to serve the lambda function. Here is an example deployment script which creates the AWS resources described above, bundles the lambda function and uploads it.
Using the CLI starter
The CLI starter example includes a basic build script that creates the required AWS resources, copies the static files, bundles the lambda function and uploads it. To deploy the lambda function, you can run:
To make adjustments to the lambda function created (e.g. architecture, memory, timeout, etc.) you can edit the head section of the deploy.sh
script.
To clean up AWS resources created by the deployment script, you can run: