英文:
How to properly set up a monorepo in TypeScript using workspaces to deploy AWS lambda functions with CDK?
问题
I'm trying to understand how to set up a monorepo using yarn workspaces and TypeScript. I want to deploy the "tasks" as lambda functions to AWS.
Here's a simple monorepo that I've created: https://github.com/chribben/monorepo-test
It has two workspaces, one called my-task and one called my-shared. The former has a dependency on the latter.
These are the steps I take to transpile and install the dependencies.
yarn build
cp tasks/my-task/package.json dist/tasks/my-task/
cp my-shared/package.json dist/my-shared/
cp package.json dist
yarn --cwd dist/tasks/my-task/ install
Which results in this structure:
- dist
- my-shared
- node_modules
- tasks
- my-task
The lambda function is created in CDK like so:
const handler = new lambda.Function(this, "HelloMonorepo", {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset("../dist/tasks/my-task"),
handler: "index.hello",
});
This will not include necessary dependencies in the deployment. So my question is how to properly include the dependent artifacts (i.e. my-shared and node_modules) in the deployment?
Note that the idea is to have multiple lambdas, each with its own deployment artifact (containing only the code relevant for it) so I don't want to have one deployment artifact containing all the code and dependencies for all lambdas (i.e. the whole dist folder).
英文:
I'm trying to understand how to set up a monorepo using yarn workspaces and TypeScript. I want to deploy the "tasks" as lambda functions to AWS.
Here's a simple monorepo that I've created: https://github.com/chribben/monorepo-test
It has two workspaces, one called my-task and one called my-shared. The former has a dependency on the latter.
These are the steps I take to transpile and install the dependencies.
yarn build
cp tasks/my-task/package.json dist/tasks/my-task/
cp my-shared/package.json dist/my-shared/
cp package.json dist
yarn --cwd dist/tasks/my-task/ install
Which results in this structure:
- dist
- my-shared
- node_modules
- tasks
- my-task
The lambda function is created in CDK like so:
const handler = new lambda.Function(this, "HelloMonorepo", {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset("../dist/tasks/my-task"),
handler: "index.hello",
});
This will not include necessary dependencies in the deployment.
So my question is how to properly include the dependent artefacts (i.e. my-shared and node_modules) in the deployment?
Note that the idea is to have multiple lambdas, each with its own deployment artefact (containing only the code relevant for it) so I don't want to have one deployment artefact containing all the code and dependencies for all lambdas (i.e. the whole dist folder)
答案1
得分: 1
CDK只会将Lambda函数创建时在code
属性中指定的目录中的代码压缩并发送到S3存储桶。
在你的示例中,"my-task" 只包含了 /src 和 package.json。
你应该引用整个目录并修改你的Lambda函数,类似于:
const handler = new lambda.Function(this, "HelloMonorepo", {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset("../dist"),
handler: "/tasks/my-tasks/index.hello",
});
英文:
CDK will only zip and send to S3 bucket the code inside the directory your indicate in your code
property when creating the Lambda function.
In your example, my-task only contains /src and package.json.
You should reference the whole directory and modify your lambda function to something like :
const handler = new lambda.Function(this, "HelloMonorepo", {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset("../dist"),
handler: "/tasks/my-tasks/index.hello",
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论