How to handle Typescript lambdas using modules embedded in a CDK project

0

I have a question that's kind of a CloudFormation question but I guess is really a CDK question. My project layout looks like this:

work\.
├───cdk.out
├───bin
├───lib
├───node_modules
├───src
│   ├───common
│   ├───ep
│   ├───sites
│   └───static
├───package.json
├───tsconfig.json
└───test

The directories ep, sites, and static are all individual lambdas; the constructor script calls them out like this:

entry: path.join(__dirname, "../src/sites/sites.ts"),

so that works great. In the package.json I am very careful about what goes into 'dependencies' vs. 'devDependencies' to avoid conflicts and bloat.

It occurred to me, though, that some day I might end up with other problems, like conflicts, so I was searching around to see if I can give each individual lambda (ep, sites, static) their own package.json and maybe even tsconfig.json. I found a few things saying it works, but a few others saying it won't. yarn build only shows it running tsc once. I think the answer to this is to use modules but because of the way you define an entry point (as a path to a single file) I don't get the idea this would work.

I'm not sure how to make the lambdas within the CDK project more independent. Do I need to build them into their own, separate projects (outside of the CDK project), bundle them (say with webpack), then have the CDK project reference the build outputs for each? That sounds a little messier, and I like having the entire stack (lambdas and all) in one place. Is there a more self-contained way to do this?

Generally speaking, my lambdas are classes. This means that each one has an entry point defined that's just a regular function. If I knew how modules work, could I make each lambda be a module, that's its own class, and have 'entry' point to just a plain typescript function that's part of the CDK project that does something like:

import { SpecialHandler } from ../xyz;

export async function handleSpecialRequest(request: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
    return new SpecialHandler.handleRequest(request);
}
profile picture
wz2b
질문됨 일 년 전280회 조회
1개 답변
0
수락된 답변

Hi there! I've seen others tackle this problem with tooling like Learn (https://lerna.js.org/). However, I would be wary of going the monorepo direction as the CDK team has baked in a lot of tooling around packaging and deploying JS/TS lambda's. If you were to go this route you would be reinventing the wheel, I could only recommend it if the problem is prevalent and not just a hammer in search of a nail.

답변함 일 년 전
profile picture
전문가
검토됨 한 달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠