typescriptaws-cdkaws-codepipelineaws-codebuild

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. My project has many instances of the 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.

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.

Note: Everything works and deploys using cdk deploy --all.

Code Pipeline 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:

"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. Full Error


Solution

  • Solution:

    1. There is no absolute requirement to go cd into each NodejsFunction folder and install the dependencies in the build command.
    2. All NodejsFunction dependencies should be stored in the root package.json file
    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).

    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. (DOCS)