|
| 1 | +import * as cdk from "aws-cdk-lib"; |
| 2 | +import * as lambda from "aws-cdk-lib/aws-lambda"; |
| 3 | +import * as apigateway from "aws-cdk-lib/aws-apigateway"; |
| 4 | +import * as path from "path"; |
| 5 | +import { Construct } from "constructs"; |
| 6 | + |
| 7 | +export class ChatStack extends cdk.Stack { |
| 8 | + constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
| 9 | + super(scope, id, props); |
| 10 | + |
| 11 | + // Lambda function for handling chat and models requests |
| 12 | + const chatHandler = new lambda.Function(this, "ChatHandler", { |
| 13 | + runtime: lambda.Runtime.NODEJS_20_X, |
| 14 | + handler: "index.handler", |
| 15 | + code: lambda.Code.fromAsset(path.join(__dirname, "../lambda"), { |
| 16 | + bundling: { |
| 17 | + image: lambda.Runtime.NODEJS_20_X.bundlingImage, |
| 18 | + command: [ |
| 19 | + "bash", |
| 20 | + "-c", |
| 21 | + [ |
| 22 | + "npm install --prefix /asset-input", |
| 23 | + "npx esbuild /asset-input/index.ts --bundle --platform=node --target=node20 --outfile=/asset-output/index.js", |
| 24 | + ].join(" && "), |
| 25 | + ], |
| 26 | + local: { |
| 27 | + tryBundle(outputDir: string) { |
| 28 | + const { execSync } = require("child_process"); |
| 29 | + try { |
| 30 | + execSync( |
| 31 | + `cd ${path.join(__dirname, "../lambda")} && npm install && npx esbuild index.ts --bundle --platform=node --target=node20 --outfile=${outputDir}/index.js` |
| 32 | + ); |
| 33 | + return true; |
| 34 | + } catch { |
| 35 | + return false; |
| 36 | + } |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }), |
| 41 | + environment: { |
| 42 | + WIREMOCK_BASE_URL: "http://wiremock.localhost.localstack.cloud:4566", |
| 43 | + }, |
| 44 | + timeout: cdk.Duration.seconds(30), |
| 45 | + memorySize: 256, |
| 46 | + }); |
| 47 | + |
| 48 | + // REST API Gateway |
| 49 | + const api = new apigateway.RestApi(this, "ChatApi", { |
| 50 | + restApiName: "WireMock Chat API", |
| 51 | + description: "API for chat backend using WireMock OpenAI mocks", |
| 52 | + deployOptions: { |
| 53 | + stageName: "dev", |
| 54 | + }, |
| 55 | + defaultCorsPreflightOptions: { |
| 56 | + allowOrigins: apigateway.Cors.ALL_ORIGINS, |
| 57 | + allowMethods: apigateway.Cors.ALL_METHODS, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + // Lambda integration |
| 62 | + const lambdaIntegration = new apigateway.LambdaIntegration(chatHandler, { |
| 63 | + requestTemplates: { "application/json": '{ "statusCode": "200" }' }, |
| 64 | + }); |
| 65 | + |
| 66 | + // GET /models endpoint |
| 67 | + const modelsResource = api.root.addResource("models"); |
| 68 | + modelsResource.addMethod("GET", lambdaIntegration); |
| 69 | + |
| 70 | + // POST /chat endpoint |
| 71 | + const chatResource = api.root.addResource("chat"); |
| 72 | + chatResource.addMethod("POST", lambdaIntegration); |
| 73 | + |
| 74 | + // Outputs |
| 75 | + new cdk.CfnOutput(this, "ApiEndpoint", { |
| 76 | + value: api.url, |
| 77 | + description: "API Gateway endpoint URL", |
| 78 | + }); |
| 79 | + |
| 80 | + new cdk.CfnOutput(this, "ModelsEndpoint", { |
| 81 | + value: `${api.url}models`, |
| 82 | + description: "GET /models endpoint", |
| 83 | + }); |
| 84 | + |
| 85 | + new cdk.CfnOutput(this, "ChatEndpoint", { |
| 86 | + value: `${api.url}chat`, |
| 87 | + description: "POST /chat endpoint", |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments