简体   繁体   English

AWS CDK 代码管道开发——NodejsFunction Package.json 依赖项如何安装?

[英]AWS CDK Code Pipeline Development - How Do I Install NodejsFunction Package.json Dependencies?

I'm trying to organise a new CodePipeline following the documentation from the CDK V2 .我正在尝试按照 CDK V2 的文档组织一个new CodePipeline My project has many instances of the NodejsFunction .我的项目有很多NodejsFunction实例。 Whilst Code Build can install the root package.json dependencies, I am struggling to find an organised solution for installing each package.json for each Lambda Function.虽然 Code Build 可以安装根 package.json 依赖项,但我正在努力寻找一个有组织的解决方案来为每个 Lambda Function 安装每个 package.json。

Example Folder Structure:示例文件夹结构:

-Root
--src
---lambdaHandlers

----lambdaOne
-----index.ts
-----package.json
-----package-lock.json

----lambdaTwo
-----index.ts
-----package.json
-----package-lock.json

I require Code Build to go into each of these folders and install the NodejsFunction dependencies, to remove the Code Build Error Message of: error TS2307: Cannot find module '@aws-sdk/client-sfn' or its corresponding type declarations .我需要将代码构建到 go 到每个文件夹中并安装NodejsFunction依赖项,以删除代码构建错误消息: error TS2307: Cannot find module '@aws-sdk/client-sfn' or its corresponding type declarations

Note: Everything works and deploys using cdk deploy --all.注意:一切正常并使用 cdk deploy --all 进行部署。

Code Pipeline CDK:代码管道 CDK:

this.codePipeline = new CodePipeline(this, `${environment}-${appName}-`, {
  pipelineName: `${environment}-${appName}-`,
  selfMutation: true,
  crossAccountKeys: false,
  role: this.codePipelineRole,
  dockerEnabledForSynth: true,
  synth: new ShellStep("Deployment", {
    input: CodePipelineSource.codeCommit(this.codeRepository, environment, {  codeBuildCloneOutput: true }),
    installCommands: ["npm i -g npm@latest", "npm install -g typescript"],
    commands: [
      "npm ci",
      "npm run build",
      "cdk synth",
    ],
  })
});

One solution I tried was using the code in the commands section of this.codePipeline below for each Lambda Function. Whilst this worked for installing the packages, I was left with typescript errors:我尝试的一种解决方案是使用下面this.codePipeline的命令部分中的代码来处理每个 Lambda Function。虽然这对安装包有效,但我仍然遇到 typescript 错误:

"cd src/lambda-handlers/api-gateway-entry-points/entryPointMagentoCredits", "npm ci"

Error Received: intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,18): error TS1005: ']' expected.收到错误: intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,18): error TS1005: ']' expected. Full Error 完全错误

The NodejsFunction construct doesn't require package.json for the lambdas. NodejsFunction 构造不需要 package.json 作为 lambda。 It can assemble the packages and code part itself from the root package.json.它可以从根 package.json 组装包和代码部分。 That makes it so powerful IMO.这使得它如此强大的 IMO。

Solution:解决方案:

  1. There is not a requirement to go cd into each NodejsFunction folder and install the dependencies in the buld command.不需要 go cd进入每个NodejsFunction文件夹并在 buld 命令中安装依赖项。
  2. All NodejsFunction dependencies should be stored in the root package.json file所有NodejsFunction依赖项都应存储在根 package.json 文件中
  3. All the NodejsFunction handlers can be stored anywhere in the code, and referred to either with the entry key value pair, or be stored in the lib folder ( DOCS ).所有NodejsFunction处理程序都可以存储在代码中的任何位置,并通过entry键值对引用,或者存储在 lib 文件夹 ( DOCS ) 中。

New File Structure:新文件结构:

-Root
--lib
---lambda-Stack
----index.ts
----lambdaOne.ts
----lambdaTwo.ts

--package.json
--package-lock.json

I added the depsLockFilePath with reference to the package-lock.json file, but I am not sure whether this is necessary.我参考package-lock.json文件添加了depsLockFilePath ,但不确定是否有必要。 ( DOCS ) ( 文档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS CDK 代码管道开发 - Typescript 错误 TS2307 - AWS CDK Code Pipeline Development - Typescript Error TS2307 AWS CDK 代码管道开发 - 错误 TS1005:预期为“]” - AWS CDK Code Pipeline Development - error TS1005: ']' expected AWS CDK NodejsFunction:导入导出为“export =”的模块 - AWS CDK NodejsFunction: import a module exported as "export =" 如何使用 AWS CDK 中的 InvokeLambda 将 JSON 传递给 AWS StepFunction 中的 lambda - How do I pass JSON to lambda in AWS StepFunction using InvokeLambda in AWS CDK CDK Synth With NodejsFunction Bundling Error - 请在继续之前使用 `npm install` 更新您的锁定文件 - CDK Synth With NodejsFunction Bundling Error - Please update your lock file with `npm install` before continuing 如何指定目录来触发代码管道cdk? - How to Specify directory to trigger code pipeline cdk? 如何将 package AWS CDK 转化为 Lambda 层? - How to package AWS CDK into Lambda layer? AWS CDK - 如何有条件地创建 ECR 存储库 - AWS CDK - How do I create ECR repo conditionally 如何使用 AWS-CDK 设置 AWS Cloud9 实例? - How do I set up a AWS Cloud9 instance using AWS-CDK? AWS CDK:如何在没有代码的情况下创建 lambda - AWS CDK: How to create lambda without code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM