I use TypeScript in my project, which is organized as a monorepo with two main directories:
infrastructure
: Contains all the CDK code.lambda
: Contains the handlers for each Lambda function.
The CDK code dynamically reads the file system to discover and configure the Lambdas in the lambda
directory, setting up all the necessary infrastructure accordingly.
Problem:
For body validation using API Gateway, I need to use JsonSchema
from the CDK package. However, I can’t directly use CDK as a dependency in the lambda
directory. Initially, I redefined the JsonSchema
type in the lambda
directory. When using the exported model from the lambda
directory in the CDK, I just cast it to JsonSchema
.
The issue with this approach is that if the JsonSchema
interface in the CDK package is updated, my redefined type in the lambda
directory won’t reflect those changes, potentially causing issues that go unnoticed.
Considered Solution:
I thought about exporting the JsonSchema
interface after importing it in the CDK (under the infrastructure
directory) and then importing it again under the lambda
directory. This way, I can ensure consistency without redefining the interface.
However, I’m not sure if this is the correct approach. Specifically:
- Will this approach work as intended?
- Will importing
JsonSchema
from theinfrastructure
directory into thelambda
directory only import the interface, or will it also include the entire CDK package?
Note: My goal is to always keep my lambda function as lightweight as possible
Example Code:
infrastructure/schema.ts
import { JsonSchema } from 'cdk-package';
export { JsonSchema };
*lambda/handler.ts*
import { JsonSchema } from '../infrastructure/schema';
// Use JsonSchema for validation in Lambda handler
const schema: JsonSchema = {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
},
required: ['id', 'name'],
};