'Installing AWS CDK as step in AWS CodeBuild

As part of an automated build pipeline I'm trying to deploy a cdk stack in AWS CodeBuild. However the build already fails in the pre_build step when I'm running a sanity check and printing the cdk version cdk --version with the error message:

Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: cdk --version. Reason: exit status 127

So apparently cdk - which was supposed to be installed in one of he commands before (npm ci)- was not found. What am I doing wrong?

The buildspec.yml looks like this:

version: 0.2

env:
  shell: bash

phases:
  pre_build:
    commands:
      - cd ${CODEBUILD_SRC_DIR}/cdk
      - npm ci
      - npm run build
      - cdk --version

  build:
    commands:
      - cd ${CODEBUILD_SRC_DIR}/cdk
      - cdk deploy --all

What I tried so far:

  • Trying npm install instead of npm ci → No change
  • Running npm install aws-cdk -g as a separate command → Works, but it doesn't install the version specified in the package.json
  • Adding an install phase and declaring a nodejs version → No change
  • Tried both bashand sh → No change


Solution 1:[1]

CDK consists of two parts: the CDK library that you import in your code, and the CDK CLI that does the synthesizing and deployment. Both are required.

The requirements in your package.json refer to the CDK library, not the CLI.

You need to install the CLI manually separately, like you did:

npm install aws-cdk -g

You can modify this command to specify the version, too, if needed (it is recommended to use the latest version, it is backwards compatible).

P.S. If you haven't already, look into CDK Pipelines for deploying CDK apps, they make it a lot easier (not this specific part, though).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gshpychka