我试图使用CodeBuild操作在AWS CodePipeline上部署一个AWS CDK应用。
构建和部署在本地完美地工作(因为它将!),但当在CodeBuild上运行时。cdk
命令失败
Cannot find module './index'
Subprocess exited with error 1
这很可能是一些琐碎的事情,但我却绞尽脑汁地想知道是什么!
项目结构是自动生成的(有了 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
对于 Build
阶段是
phases:
build:
commands:
- cd ${CODEBUILD_SRC_DIR}/cdk
- yarn install
- yarn build
artifacts:
base-directory: ${CODEBUILD_SRC_DIR}/cdk
files:
- '**/*'
buildspec.yml
对于 Deploy
阶段是(该阶段的输入目录是来自于 Build
阶段,即 cdk
目录)
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
该 Deploy
舞台上 Cannot find module './index'
错在 cdk ls
步骤。由于上述builddeploy步骤在本地工作(在一个干净的检查中),我怀疑这可能与将工件从 Build
到 Deploy
阶段是什么原因导致的问题,但又不能确定是什么原因。有什么排除故障的建议吗?
CodeBuild有一个已知的问题,当它创建你的工件时,它会破坏你所有的符号链接=>。https:/forums.aws.amazon.comthread.jspa?threadID=245780。
错误 Cannot find module './index'
是因为你的 cdk.json
有一个命令可以使用 ts-node
而当cdk试图从 node-modules/.bin/ts-node
链接已断。
为了达到你的目的,我建议你在构建工作中自己压缩代码。比如说
- yarn build
- tar -czf /tmp/mycode.tar.gz .
artifacts:
files:
- 'mycode.tar.gz'
discard-paths: true
base-directory: '/tmp'
然后在部署工作中解压。
...
- cd ${CODEBUILD_SRC_DIR} # this is cdk directory
- tar -zxvf mycode.tar.gz
- cdk ls
- cdk deploy app
你在cdk里有Lambda代码吗?你可以检查一下使用的处理程序,以及它是否存在于该路径,例如
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')),
});