diff --git a/bun.lockb b/bun.lockb index 837c8bb..4496703 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/cdk.context.json b/cdk.context.json new file mode 100644 index 0000000..9fc017e --- /dev/null +++ b/cdk.context.json @@ -0,0 +1,5 @@ +{ + "acknowledged-issue-numbers": [ + 29420 + ] +} diff --git a/lambda/date.ts b/lambda/date.ts new file mode 100644 index 0000000..daf04e1 --- /dev/null +++ b/lambda/date.ts @@ -0,0 +1,13 @@ +export default { + async handler(request: Request): Promise { + 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", + } + }) + }, + } \ No newline at end of file diff --git a/lambda/index.ts b/lambda/hello.ts similarity index 100% rename from lambda/index.ts rename to lambda/hello.ts diff --git a/lib/bun-cdk-stack.ts b/lib/bun-cdk-stack.ts index a5b8992..7ba63df 100644 --- a/lib/bun-cdk-stack.ts +++ b/lib/bun-cdk-stack.ts @@ -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) { @@ -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", }); diff --git a/package.json b/package.json index ddb9428..66ea18e 100644 --- a/package.json +++ b/package.json @@ -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",