Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make lambda work with API using bun layer #1

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"acknowledged-issue-numbers": [
29420
]
}
13 changes: 13 additions & 0 deletions lambda/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
async handler(request: Request): Promise<Response | undefined> {
console.log(JSON.stringify(request));
const localeTime = new Date().toLocaleTimeString();
const response = JSON.stringify({ time: localeTime });
return new Response(response, {
status: 200,
headers: {
"content-type": "application/json; charset=UTF-8",
}
})
},
}
File renamed without changes.
31 changes: 23 additions & 8 deletions lib/bun-cdk-stack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as cdk from "aws-cdk-lib";
import { LambdaIntegration, RestApi } from "aws-cdk-lib/aws-apigateway";
import { LayerVersion, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Code, LayerVersion, Runtime, Function, Architecture } from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
// import * as sqs from 'aws-cdk-lib/aws-sqs';
import path = require("node:path");

export class BunCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
Expand All @@ -12,19 +11,35 @@ export class BunCdkStack extends cdk.Stack {
const layer = LayerVersion.fromLayerVersionArn(
this,
"BunLayer",
// Follow https://github.com/oven-sh/bun/tree/main/packages/bun-lambda#setup
// and deploy the layer to your account
// then get the ARN from the AWS Console under Lambda -> Layers
"arn:aws:lambda:us-east-1:648568751601:layer:bun:2"
);

const nodejsFunction = new NodejsFunction(this, "BunCdkFunction", {
entry: "lambda/index.ts",
handler: "handler",
runtime: Runtime.NODEJS_20_X,
const helloHandler = new Function(this, "HelloFn", {
runtime: Runtime.PROVIDED_AL2,
handler: "hello.handler",
code: Code.fromAsset(path.join(__dirname, "../", "lambda")),
architecture: Architecture.ARM_64,
layers: [layer],
});

const dateHandler = new Function(this, "DateFn", {
runtime: Runtime.PROVIDED_AL2,
handler: "date.handler",
code: Code.fromAsset(path.join(__dirname, "../", "lambda")),
architecture: Architecture.ARM_64,
layers: [layer],
});


const api = new RestApi(this, "bun-api");
api.root.addMethod("GET", new LambdaIntegration(nodejsFunction));
api.root.addMethod("GET", new LambdaIntegration(helloHandler));

const date = api.root.addResource("date");
date.addMethod("GET", new LambdaIntegration(dateHandler));

new cdk.CfnOutput(this, "BunApiUrl", {
value: api.url ?? "Something went wrong with the deployment",
});
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"typescript": "~5.3.3"
},
"dependencies": {
"@types/aws-lambda": "^8.10.136",
"@types/bun": "^1.0.8",
"aws-cdk-lib": "2.132.0",
"aws-lambda": "^1.0.7",
"bun": "^1.0.30",
"bun-types": "^1.0.30",
"constructs": "^10.0.0",
Expand Down