Use bun lambda runtime with Authorizer function using a local fork of bun, using cloudformation as a base for the definition of the resources.
- I open an issue in bun requesting this change.
- this project is an example of the code working.
- majority of the tests were made locally using SAM.
main functions are defined in functions/
folder but were built for use in the template.
Important
- you need to run
sam init
in the root folder and specify1 - AWS Quick Start Templates
with the template locationtemplate.yaml
. - you need to add the bun runtime from
bun/packages/bun-lambda/bun
to./bun-layer
.
you can define whatever you need in .env file
some useful scripts to run the project
bun start
bun build
bun deploy
bun release
just call the api from your local with the correct authorizer method:
run:
bun run build && bun run start
make sure you're running docker
then:
curl --header "Authorization: Bearer valid-token" \
--request POST \
http://<api-gateway-endpoint>/baselambda
bun-layer/runtime.ts # this is a copy from packages/bun-lambda/runtime.ts
the main issue with the runtime with the authorizer is the default authorizer function needs to return a policy as an object and not as a response as is responded today in the fetch api: example
the solution to support this:
- add types for the response
AuthorizerResultContext
,APIGatewayAuthorizerResult
. - Extend fetch API to support those types.
- check in
#acceptRequest
if the request hasevent.event?.methodArn
this means the function is an authorizer. - create a new handler for this kind of request
authorizerFetch
this response the need it policy.
I ❤️ Bun