Faith
Faith

Reputation: 101

AWS CDK Pipeline Construct: Codebuild error 254

I am fairly new to AWS CDK and I'm trying to follow the tutorial here regarding the Cdk Pipeline construct to build my own pipeline https://docs.aws.amazon.com/cdk/latest/guide/cdk_pipeline.html

I tried to start with creating a basic pipeline (see code below) - however I keep getting the error in CodeBuild saying npm ERR! enoent ENOENT: no such file or directory, open '/codebuild/output/src503090835/src/package.json'. I'm not entirely sure why this is the case as I'm using a CdkPipeline construct so I'm not making a CodeBuild resource directly myself.

Has any one experienced this before, and able to shed some light?

lib/pipeline-stack.ts

import { Stack, StackProps, Construct, SecretValue } from '@aws-cdk/core';
import { CdkPipeline, SimpleSynthAction } from '@aws-cdk/pipelines';

import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';

import { GITHUB_VALUES} from '../lib/constants'

export class PipelineStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const sourceArtifact = new codepipeline.Artifact();
    const cloudAssemblyArtifact = new codepipeline.Artifact();

    const pipeline = new CdkPipeline(this, 'pipeline', {
      pipelineName: 'cherry-client-pipeline',
      cloudAssemblyArtifact,

      sourceAction: new codepipeline_actions.GitHubSourceAction({
        actionName: 'GitHub',
        output: sourceArtifact,
        oauthToken: SecretValue.secretsManager(GITHUB_VALUES.OAUTH_TOKEN_NAME),
        trigger: codepipeline_actions.GitHubTrigger.POLL,
        owner: GITHUB_VALUES.OWNER,
        repo: GITHUB_VALUES.REPO,
        branch: 'main'
      }),

      synthAction: SimpleSynthAction.standardNpmSynth({
        sourceArtifact,
        cloudAssemblyArtifact,
        buildCommand: 'npm run build'
      }),
    });
  }
}

bin/pipeline.ts

import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { PipelineStack } from '../lib/pipeline-stack';
import { ENVIRONMENTS } from '../lib/constants'

const app = new cdk.App();
new PipelineStack(app, 'pipeline-stack', {
  env: ENVIRONMENTS.DEV
});

app.synth();

cdk.json

{
  "app": "npx ts-node --prefer-ts-exts bin/pipeline.ts",
  "context": {
    "@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
    "@aws-cdk/core:enableStackNameDuplicates": "true",
    "aws-cdk:enableDiffNoFail": "true",
    "@aws-cdk/core:stackRelativeExports": "true",
    "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
    "@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
    "@aws-cdk/aws-kms:defaultKeyPolicies": true,
    "@aws-cdk/aws-s3:grantWriteWithoutAcl": true,
    "@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount": true,
    "@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
    "@aws-cdk/aws-efs:defaultEncryptionAtRest": true,
    "@aws-cdk/aws-lambda:recognizeVersionProps": true,
    "@aws-cdk/core:newStyleStackSynthesis": true
  }
}

package.json

{
  "name": "pipeline",
  "version": "0.1.0",
  "bin": {
    "pipeline": "bin/pipeline.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@aws-cdk/assert": "1.115.0",
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "jest": "^26.4.2",
    "ts-jest": "^26.2.0",
    "aws-cdk": "1.115.0",
    "ts-node": "^9.0.0",
    "typescript": "~3.9.7"
  },
  "dependencies": {
    "@aws-cdk/aws-codebuild": "^1.115.0",
    "@aws-cdk/aws-codepipeline": "^1.115.0",
    "@aws-cdk/aws-codepipeline-actions": "^1.115.0",
    "@aws-cdk/aws-s3-deployment": "^1.115.0",
    "@aws-cdk/core": "1.115.0",
    "@aws-cdk/pipelines": "^1.115.0",
    "source-map-support": "^0.5.16"
  }
}

Upvotes: 3

Views: 1923

Answers (2)

In my case the error was in the cdk.json file. In the value of the app:

{
  "app" : "npx ts-node --prefer-ts-exts directory/function-name-stack.ts",

The error was the stack word at the final of the function, the correct way:

{
  "app" : "npx ts-node --prefer-ts-exts directory/function-name-.ts",

Upvotes: 0

Faith
Faith

Reputation: 101

I've managed to find an answer (at least to my situation) posting in case this helps anyone in the future.

The CdkPipeline construct has two main actions

  • sourceAction which grabs the code from a repository when triggered
  • synthAction which does the building

synthAction has a few defaults which weren’t immediately obvious to me from the docs/tutorial. e.g. - it assumes that it will find the cdk.json and (cdk)package.json at the root of the code repo

I had my cdk application inside of a subdirectory called 'pipeline' so I needed to make use of the property subdirectory

synthAction: SimpleSynthAction.standardNpmSynth({
        sourceArtifact,
        cloudAssemblyArtifact,
        subdirectory: 'pipeline',
        buildCommand: 'cd ../services/website && npm run build'
      })

Upvotes: 1

Related Questions