typescriptamazon-web-servicesaws-cdkaws-codebuild

CDK: Codebuild project not able to find buildspec.yml file


I am trying to load plan-buildspec.yml file in my codebuild Project, however it's not able to find my buildspec.yml no matter which path I give.

I did refer to this https://github.com/aws/aws-cdk/issues/7329, but no luck

YAML_FILE_ERROR Message: stat /codebuild/output/srcXXXXXXX/buildspec/plan-buildspec.yml: no such file or directory

here is my CodeBuild pipeline definition

 const TFplan = new codebuild.PipelineProject(this, 'tf-plan', {
      projectName: 'tf-cicd-plan',
      description: 'Plan stage for terraform',
      environment: {
        computeType: ComputeType.SMALL,
        buildImage: LinuxBuildImage.AMAZON_LINUX_2_4
      },
      buildSpec: codebuild.BuildSpec.fromSourceFilename('../buildspec/plan-buildspec.yml')
    })



const TFplanbBuildAction = new codepipeline_actions.CodeBuildAction({
      actionName: 'Build',
      project: TFplan,
      input: sourceOutput,
    });

my tree structure, buildspec files are present in buildspec directory.

.
├── README.md
├── bin
├── buildspec
├── cdk.json
├── cdk.out
├── jest.config.js
├── lib
├── node_modules
├── package-lock.json
├── package.json
├── test
└── tsconfig.json

Solution

  • buildspec/plan-buildspec.yml

    The path is relative to the project root.


    Note: When you use fromSourceFilename, CodeBuild looks for your buildspec file only at runtime. It expects a file in the pipeline artefact (= *from a file in the source* = your repo). The template the CDK creates has the file name only:

    "Type": "AWS::CodeBuild::Project",
    "Properties": {
        "Source": {
            "BuildSpec": "buildspec/plan-buildspec.yml",
    

    If instead you want the CDK to embed the buildspec itself with the pipeline definition, you must use Buildspec.fromObject, passing key-value pairs. CDK puts the buildspec in the template at synth-time:

    "Type": "AWS::CodeBuild::Project",
    "Properties": {
        "Source": {
            "BuildSpec": "{\n  \"version\": \"0.2\",\n  \"phases\": {\n    \"build\": {\n   ...",