简体   繁体   English

AWS CDK 部署在 CodePipeline/CodeBuild

[英]AWS CDK deploy in CodePipeline/CodeBuild

I'm trying to deploy a AWS CDK app on AWS CodePipeline using CodeBuild actions.我正在尝试使用 CodeBuild 操作在 AWS CodePipeline 上部署 AWS CDK 应用程序。

The build and deploy works perfect locally (as it would,) but when running on CodeBuild, cdk command fails with构建和部署在本地完美运行(因为它会),但是在 CodeBuild 上运行时, cdk命令失败并显示

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 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 Build阶段的buildspec.yml

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 ie the cdk directory) Deploy阶段的buildspec.yml是(此阶段的输入目录是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

The Deploy stage throws the Cannot find module './index' error at the cdk ls step. Deploy阶段在cdk ls步骤中引发Cannot find module './index'错误。 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.由于上述构建/部署步骤在本地工作(在干净的结帐中),我怀疑这可能与从BuildDeploy阶段复制工件有关是导致问题的原因,但无法查明是什么。 Any suggestions for troubleshooting?有什么故障排除建议吗?

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 CodeBuild 存在一个已知问题,它在创建工件时会破坏所有符号链接 => 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.错误Cannot find module './index'是因为您的cdk.json具有使用ts-node的命令,并且当 cdk 尝试从node-modules/.bin/ts-node运行它时,链接已断开。

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

Do you have Lambda code in cdk? cdk 中有 Lambda 代码吗? Can you check the handler used and if it is present at that path, eg您能否检查所使用的处理程序以及它是否存在于该路径中,例如

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')),
});

I had the same issue and resolved it by passing in enable-symlinks: yes in my buildspec.yml我遇到了同样的问题并通过传入enable-symlinks: yes在我的 buildspec.yml 中解决了它

artifacts:
  enable-symlinks: yes

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

相关问题 cfn 模板和管道都是使用 AWS CDK 创建的,如何使用 CodePipeline 部署 CloudFormation 堆栈? - How to deploy a CloudFormation stack using CodePipeline when both, the cfn template and the pipeline where created using the AWS CDK? AWS CDK 管道构造:代码构建错误 254 - AWS CDK Pipeline Construct: Codebuild error 254 使用不同参数从 CodePipeline 调用 AWS CodeBuild 项目 - Call AWS CodeBuild project from CodePipeline with different parameters @aws-cdk/pipelines 和 @aws-cdk/aws-codepipeline 有什么区别? - What is the difference between @aws-cdk/pipelines and @aws-cdk/aws-codepipeline? AWS CDK CodePipeline 在 Source 和 Build 之间添加一个阶段 - AWS CDK CodePipeline add a stage between Source and Build 无法将 Nodejs 和 Typescript 部署到 AWS cdk + lambda - Cannot deploy Nodejs and Typescript to AWS cdk + lambda 如何在CodeBuild动作之间传递CodePipeline中的工件 - How to pass artifacts in CodePipeline between CodeBuild Actions AWS cdk deploy --all 创建 ECS 服务失败 - AWS cdk deploy --all fails to create ECS service 如何使用 AWS CDK TYPESCRIPT 引用现有 VPC 来部署 Beanstalk 应用程序 - How to refer exsisting VPC to deploy Beanstalk app using AWS CDK TYPESCRIPT 使用 AWS cdk 部署停靠的 .NET6 lambda 时如何自定义 docker 构建上下文? - How to customize docker build context when deploy a dockized .NET6 lambda using AWS cdk?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM