2
votes

I'm trying to deploy a AWS CDK app on AWS CodePipeline using CodeBuild actions.

The build and deploy works perfect locally (as it would!) but when running on CodeBuild, cdk command fails with

Cannot find module './index'
Subprocess exited with error 1

This is most likely something trivial but scratching my head trying to figure out what!

The project structure is auto-generated (with cdk init --language typescript)

<>/cdk$ ls
README.md  app  cdk.context.json  cdk.json  cdk.out  jest.config.js  lib  node_modules  package.json  test  tsconfig.json  yarn.lock

buildspec.yml for the Build stage is

phases:
  build:
    commands:
      - cd ${CODEBUILD_SRC_DIR}/cdk
      - yarn install
      - yarn build
artifacts:
  base-directory: ${CODEBUILD_SRC_DIR}/cdk
  files:
    - '**/*'

buildspec.yml for the Deploy stage is (the input directory to this stage is the artifact from Build stage i.e. the cdk directory)

phases:
  install:
    commands:
      - npm install -g aws-cdk
      - cdk --version

  build:
    commands:
      - cd ${CODEBUILD_SRC_DIR} # this is cdk directory
      - cdk ls
      - cdk deploy app

The Deploy stage throws the Cannot find module './index' error at the cdk ls step. Since the above build/deploy steps work locally (in a clean checkout) I suspect it could be something to do with copying artifacts from Build to Deploy stages is what's causing the problem, but can't pinpoint what. Any suggestions for troubleshooting?

3
Try to add ls before cdk ls command to verify that all files are thereAmit Baranes
Yeah, tried and that and all the files seem to be there..ubi
Weird.. can you share the steps you doing manually? I know it's the same as the buildspec.yaml but there might be some small changes that need to be madeAmit Baranes
Yes, this is indeed weird. May be I'm missing something simple.. The steps I run manually are npm install && npm run build && cdk deployubi
Replicated with a minimal app github.com/IndikaUdagedara/CDKSampleubi

3 Answers

2
votes

There is a known issue with CodeBuild that it breaks all your symlinks when it creates your artifact => https://forums.aws.amazon.com/thread.jspa?threadID=245780

The error Cannot find module './index' is because your cdk.json has a command to use ts-node and when cdk tries to run it from node-modules/.bin/ts-node the link is broken.

In order to do what you want, I suggest you to compress the code yourself on the build job. Something like:

      - yarn build
      - tar -czf /tmp/mycode.tar.gz .
artifacts:
  files:
    - 'mycode.tar.gz'
  discard-paths: true
  base-directory: '/tmp'

and decompress on the deploy job:

...
      - cd ${CODEBUILD_SRC_DIR} # this is cdk directory
      - tar -zxvf mycode.tar.gz
      - cdk ls
      - cdk deploy app
0
votes

Do you have Lambda code in cdk? Can you check the handler used and if it is present at that path, e.g.

import * as lambda from '@aws-cdk/aws-lambda';
import * as path from 'path';

const fn = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_10_X,
  handler: 'dist/index.handler',  <======= Check index.js file is inside dist directory
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});
0
votes

I had the same issue and resolved it by passing in enable-symlinks: yes in my buildspec.yml

artifacts:
  enable-symlinks: yes