Bun on Lambda: customer-managed runtime by default, 85 kB instead of 51 MB
In the last post I wrote that I am going to update @beesolve/lambda-bun-runtime to use customer-managed runtimes (CMR). It is done. BunFunction now runs on a Bun runtime that I publish for every release, and the npm package went from about 51 MB to about 85 kB unpacked, because the layer zip is no longer inside it.
This is a major version (v4) with breaking changes, so here is what changed, what stayed, and what your options are.
This article was put together by an LLM agent, not written by me word for word. The idea, the opinions and all the technical content are mine - I just did not have the time to write it up, so I asked an agent to assemble it from my earlier posts and from the notes of the work. The agent did not come up with any of this. As with every generated article on this blog, I have proofread and edited it myself before publishing.
What changed
BunFunctionruns on the customer-managed runtime. No layer is involved. By default it uses the runtime I publish, in the region of your stack.- The layer moved to
BunFunctionWithLayer. Same props as the oldBunFunction, just under an explicit name. - The package no longer ships the layer zip. That is where the 51 MB went. The layer is published once per region and referenced by ARN, and if you want the zip you can build it yourself.
aws-cdk-lib2.278.0 or newer is required. The CDK support for CMR landed as thelambda.mixins.FunctionRuntimeConfigmixin.- The stack needs a concrete region. A function can only use a runtime from its own region, so
BunFunctionpicks the published runtime for the region of your stack. Set it in the stack'senv.1
The runtime and the layer are published in five regions: Stockholm (eu-north-1), Frankfurt (eu-central-1), Ireland (eu-west-1), N. Virginia (us-east-1) and Oregon (us-west-2). If you need another region, open an issue and I will add it. Or publish your own runtime, see below.
Quick start
import { BunFunction } from "@beesolve/lambda-bun-runtime";
import { Duration } from "aws-cdk-lib";
new BunFunction(this, "ApiHandler", {
entrypoint: `${__dirname}/api.ts`,
memorySize: 1024,
timeout: Duration.seconds(10),
});
That is all. Make sure the stack has env: { region: "eu-central-1" } (or any of the other four regions). The handler signature, response streaming and the rest are the same as before, see the README.
Your options
1. BunFunction with the published runtime (default)
You write the code above and you are done. Updates reach you without anything else, more on that below.
- Pros: nothing to set up, no layer to manage, lower cold start than the layer, updates without redeploying.
- Cons: five regions only, and your function depends on a runtime living in an AWS account you don't own. I never delete published versions, but it is still a third party dependency and you should decide if you are fine with that. If you are not, option 3 is for you.
2. BunFunctionWithLayer with a layer
The layer did not go away. If you cannot use CMR (it is new, it needs a recent aws-cdk-lib, and in some accounts tenancy settings are restricted), or you simply prefer a layer, use BunFunctionWithLayer:
import { BunFunctionWithLayer, BunLambdaLayer } from "@beesolve/lambda-bun-runtime";
const bunLayer = new BunLambdaLayer(this, "BunLayer");
new BunFunctionWithLayer(this, "ApiHandler", {
entrypoint: `${__dirname}/api.ts`,
bunLayer,
});
By default BunLambdaLayer references the layer I publish for your package version in the same five regions. The description of every layer version carries the git SHA and the SHA-256 of the zip, and every GitHub release shows the same commit and the SHA-256 of the zip.
For any other region, or if you do not want to depend on my account, build the layer zip yourself. It downloads the Bun binary from the official GitHub release and needs no AWS access:
bunx bun-lambda-runtime build-layer --out lib
const bunLayer = new BunLambdaLayer(this, "BunLayer", {
assetZipPath: "lib/bun-lambda-layer-1.4.5.zip",
});
CDK publishes it as a layer in your own account during cdk deploy. If you already have a layer, pass layerVersionArn instead.
- Pros: works everywhere, you control the layer, no new AWS feature to rely on.
- Cons: slower cold start than CMR (see the benchmark), and a Bun update means redeploying every function that uses the layer.
3. BunFunction with your own runtime
The package is built so that anybody can host their own CMR. If the five regions are not enough, or you want to own the runtime, the same command I use for releases works for you:
bunx bun-lambda-runtime publish-runtime --region eu-west-3 --alias latest
It builds the runtime image, pushes it to a private ECR repository in your account, and creates the runtime, its version and the alias. It is safe to rerun. You need a recent AWS CLI with the aws lambda-core commands, and Docker with buildx. Add --public if other accounts should use it too. Then point the function at it:
new BunFunction(this, "ApiHandler", {
entrypoint: `${__dirname}/api.ts`,
bunRuntime: {
runtimeArn: "arn:aws:lambda:eu-west-3:123456789012:runtime/bun",
runtimeAlias: "latest",
},
});
The full guide is in Publish your own Bun runtime.
Two IAM mistakes I made while setting up the publishing role
If you build your own publishing pipeline, these two cost me a failed run each. Both are in the CMR permissions reference, I just did not read it carefully enough the first time.
- Creating a runtime version needs
ecr:BatchGetImageandecr:GetDownloadUrlForLayer, even when the ECR repository already trusts the Lambda service principal. - To read the resource policy of a runtime use
lambda:GetResourcePolicy(get-resource-policy).lambda:GetPolicyis the function policy API, a different resource type.
- Pros: any region, your own account, you decide when Bun updates, and you can add whatever else the image needs.
- Cons: you maintain it. A new Bun version means publishing a new runtime version yourself.
4. Stay on v3
v3 is the last version that works in every region without extra steps. It still ships the layer zip in the package, so you also keep the 51 MB. v3 will not get any new updates, all the work goes to v4 from now on.
Updates and pinning
The reason I wanted CMR is updates. You publish a new runtime version, move an alias, and functions pick it up on their next cold start. No redeploy.
Node.js managed runtimes get one alias per major, like nodejs24.x. I did not copy that. Bun is pre-2.0 and its minor releases do break things, so one alias per major is too coarse. Every published runtime version gets these aliases:
| Alias | Moves to |
|---|---|
1.4.5 | the exact Bun version, only for internal patch releases of this package |
1.4 | the latest patch of the minor line, this is the default of BunFunction |
1 | the latest 1.x release |
latest | the newest release |
So with the default, a new Bun patch (1.4.5 to 1.4.6) reaches your function on its next cold start, and a new minor (1.5) does not, until you update the package. Only the newest minor line is patched automatically. Bun does not maintain release branches for old minors, so there would be nothing to patch an old line with.
I move the aliases only after the integration test passes against the new runtime version, and an alias never moves to an older version. Still, moving an alias changes running functions, and that is a trade-off you should be aware of.
To pin an exact Bun version, pass the alias:
new BunFunction(this, "ApiHandler", {
entrypoint: `${__dirname}/api.ts`,
bunRuntime: { runtimeAlias: "1.4.5" },
});
To follow the whole major, pass "1". To get a Bun version that is not published as an alias any more, publish your own runtime (option 3) or use a layer (option 2).
The package version tells you which Bun it was built for: 4.<bun_major * 10000 + bun_minor * 100 + bun_patch>.<internal patch>. The current release is 4.10405.0, built with Bun 1.4.5.
Upgrading from v3
- Want the runtime? Remove the
bunLayerprop and theBunLambdaLayer, keepBunFunction. - Want to stay on a layer? Rename
BunFunctiontoBunFunctionWithLayer. Everything else stays. - Update
aws-cdk-libto 2.278.0 or newer and setenv.regionon the stack. - If you were deploying the layer from the zip in the package,
BunLambdaLayernow references the published layer. For other regions uselayerVersionArnorassetZipPath.
How it is tested and distributed
I wanted to be sure that a release that reaches your function works, so the release workflow does this in order:
- Build the layer and the runtime image. Both use the Bun binary from the same GitHub release zip, so they run byte-identical Bun.
- Publish to
eu-central-1first, under an internal alias that is used for nothing else. - Run the integration test against that. It deploys the sample app with a
BunFunctionand layer functions and invokes them. - Only when it passes, publish to all five regions, move the aliases, make the runtime public and
npm publish.
The publishing is idempotent, so a retry of the workflow is safe. The release workflow runs from GitHub Actions with OIDC and a role that trusts only the main branch, there are no long-lived keys in the account.
Pull requests are tested too. The integration test deploys a BunFunction against the runtime from the latest published release, so a PR is checked against the real thing and not only against the layer.
I also deployed a BunFunction from a different AWS account than the one that publishes the runtime, to make sure the cross-account sharing really works. It did.
Conclusion
If you use @beesolve/lambda-bun-runtime, update to v4 and you get Bun on a customer-managed runtime with no extra setup and an npm package that is a few kB instead of 51 MB. If CMR does not work for you, the layer is still there, and so is the option to publish your own runtime.
Try it, and if your region is missing or something does not work, open an issue. The package is on npm, the code and the v4.10405.0 release are on GitHub, and the reasoning behind v4 is in the ADR.
