5

Hello I would greatly appreciate any comments of the possible source of this error I am getting when using the AWS Cloud Development Kit (CDK) to deploy a Code Pipeline

I have a Typecript CDK project (cdk init --language typescript). This uses a GitHub hook to deploy on each update of a branch in my GitHub repository. This step works fine. However, the post build step happens fails with the errors

"Phase context status code: CLIENT_ERROR Message: no matching base directory path found for cdk.out"

From the code below you can see I use @aws-cdk/pipelines , SimpleSynthAction.standardNpmSynth and I think this isn't generating the cdk.out file (?). Any comments to help me resolve this issue would be much appreciated

import { Construct, SecretValue, Stack, StackProps } from '@aws-cdk/core';
import * as cp from '@aws-cdk/aws-codepipeline';
import * as cpa from '@aws-cdk/aws-codepipeline-actions';
import * as pipelines from '@aws-cdk/pipelines';
import { WebServiceStage } from './webservice_stage';


    export class MyPipelineStack extends Stack {
      constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);
    
        const sourceArtifact = new cp.Artifact();
        const cloudAssemblyArtifact = new cp.Artifact();
        const sourceAction = new cpa.GitHubSourceAction({
            actionName: 'GitHub',
            output: sourceArtifact,
            oauthToken: SecretValue.secretsManager('github-token'),
            owner: 'owner',
            repo: 'repo-name',
            branch:'branch-name'
        });
    
        const synthAction = pipelines.SimpleSynthAction.standardNpmSynth({
            sourceArtifact,
            cloudAssemblyArtifact,
            buildCommand: 'npm install && npm run build && npm test',
            synthCommand: 'npm run synth'
        });
    
        const pipeline = new pipelines.CdkPipeline(this, 'Pipeline', {
            cloudAssemblyArtifact,
            sourceAction,
            synthAction
        });
        // Dev stage
        const development = new WebServiceStage(this, 'Development');
        pipeline.addApplicationStage(development);
    
      }
    }

The npm 'npm run syth' just calls 'cdk synth'

I have tried running app.synth() in my /bin/my_pipeline.ts file but that doesn't fix the error.

Once again , I have googled the hell out of this and cannot resolve it so any help much appreciated. If its any use here is the buildSpec section of my output in the build logs

BuildSpec: >-
          {
            "version": "0.2",
            "phases": {
              "pre_build": {
                "commands": [
                  "npm ci"
                ]
              },
              "build": {
                "commands": [
                  "cd partnerportal_cicd_pipeline && npm install && npm run build && npm test",
                  "npm run synth"
                ]
              }
            },
            "artifacts": {
              "base-directory": "cdk.out",
              "files": "**/*"
            }
          }
fedonev
  • 20,327
  • 2
  • 25
  • 34
confusedpunter
  • 137
  • 1
  • 11

3 Answers3

19

I ran into the same error while on CDK v2.

In my case, I had to specify the subdirectory where CodePipeline could expect the cdk.out file via the primaryOutputDirectory option.

By default CodePipeline expects this dir to be in the root of the project, which is why it failed.

Example config with a subdirectory

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
  synth: new pipelines.ShellStep('Synth', {
    input: source,
    commands: [
      'cd mysubdir',
      'npm ci',
      'npm run build',
      'npx cdk synth',
    ],
    primaryOutputDirectory: 'mysubdir/cdk.out',
  }),
});

Related docs.

mcls
  • 9,071
  • 2
  • 29
  • 28
  • 1
    Thank you so much for this answer. This property is an extremely important consideration for those working with monorepos. – 55 Cancri May 15 '22 at 13:39
  • I ran into the same issue with my monorepo app which has `/backend` and `/frontend` subdirs in the root dir. Codebuild kept throwing errors about how it can't find the cdk.out dir so adding the `primaryOutputDirectory` prop points codebuild to where it should be looking. – asifm Oct 06 '22 at 03:04
3

For me the problem was that the cdk.out must be in the root folder of the repository, so I added a new command: mv cdk.out ../ .

karel
  • 5,489
  • 46
  • 45
  • 50
0

I got it working , I had to cdk destroy the deployed stack, then run 'cdk bootstrap --cloudformation-execution-policies'arn:aws:iam::aws:policy/AdministratorAccess', then make sure everything was pushed to the repo and finally run 'cdk deploy' again. I think since I hadn't done that my 'synthCommand' did not take effect.

confusedpunter
  • 137
  • 1
  • 11