Search code examples
aws-cdkaws-codebuild

Why Won't My AWS CDK Code Build in AWS Code Pipelines?


Problem: In the AWS Code Pipeline Build Stage, I am receiving the error Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm ci. Reason: exit status 254

Desired Outcome: To deploy new changes to the Code Commit Repository, and for the Cloud Formation Stacks to deploy successfully, just like cdk deploy --all.

I have followed this AWS tutorial. Everything deploys successfully.

The Code Pipeline Stack:

export class CodePipelineStack extends Stack {
    private readonly codePipeline: CodePipeline;
    private readonly codeRepository : Repository;

    constructor(scope: Construct, id: string, props: StackProps, context: CDKContext){
        super(scope, id, props);

        this.codeRepository = new Repository(this, "Repo-CDK", { repositoryName: "Repo-CDK", description: "Building A Repo using CDK Methodology" });

        this.codePipeline = new CodePipeline(this, "pipeline", {
            pipelineName: "pipeline",
            selfMutation: true,
            synth: new ShellStep('DeploymentStep', {
              input: CodePipelineSource.codeCommit(this.codeRepository, environment),
              commands: ['npm ci', 'npm run build', 'npx cdk synth'],
            })
          });
    }
}

I also have app.synth(); at the bottom of the function that builds the stacks.


Solution

  • The reason that npm ci does not work is that CodeBuild uses an old version of npm.

    Updating npm before running npm ci fixed the issue:

    const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
      synth: new pipelines.ShellStep('Synth', {
        ...
        // Update npm before running commands
        installCommands: ['npm i -g npm@latest'],
        commands: [
          'npm ci',
          'npm run build',
          'npx cdk synth',
        ],
      }),
    });