如何正确使用工作区设置 TypeScript monorepo 并使用 CDK 部署 AWS Lambda 函数?

huangapple go评论73阅读模式
英文:

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)

如何正确使用工作区设置 TypeScript monorepo 并使用 CDK 部署 AWS Lambda 函数?

答案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",
    });    

huangapple
  • 本文由 发表于 2023年4月10日 21:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定