'How do I build an existing CDK module in a new code pipeline stage?

I have an old CDK module that I would like to build with a new module using an AWS code pipeline. So far I've followed this workshop and ended up with a working code pipeline that fetches the source code from CodeCommit, builds the project, and updates the pipeline. The next stage I would like in the pipeline is one where the old CDK module is built, and this is where finding examples that use CDK 2 is proving to be difficult

In addition to the workshop I've found examples like this video, and this video.

These are the files I'm working with:

cdk-codepipeline:

#!/usr/bin/env node
import 'source-map-support/register'
import * as cdk from 'aws-cdk-lib'
import { PipelineStack } from '../lib/pipeline-stack'

const app = new cdk.App();
new PipelineStack(app, 'PipelineStack');

pipeline-stack.ts:

import { Stack, StackProps } from 'aws-cdk-lib'
import { Construct } from 'constructs'
import {IRepository, Repository} from "aws-cdk-lib/aws-codecommit";
import {CodeBuildStep, CodePipeline, CodePipelineSource} from "aws-cdk-lib/pipelines";
import {exec} from "child_process";
import {BuildArtifactsStage} from "./stages/build-artifacts-stage";


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

    const self = this
    const branchName: string = 'my-branch-name'

      const repository: IRepository = Repository.fromRepositoryName(self, 'SourceCode','my-repo-name')

      const pipeline = new CodePipeline(self, 'Pipeline', {
        pipelineName: `my-repo-name-${branchName}`,
        crossAccountKeys: false,
        selfMutation: true,
        synth: new CodeBuildStep('Synth', {
          input: CodePipelineSource.codeCommit(repository, branchName),
          installCommands: ['npm install -g aws-cdk'],
          commands: ['cd cdk-codepipeline','npm ci', 'npm run build', 'npx cdk synth', 'cd ..'],
          primaryOutputDirectory: 'cdk-codepipeline/cdk.out',
        }),
      })

      const buildArtifactsStage = pipeline.addStage(new BuildArtifactsStage(self, 'BuildArtifacts'))

  }
}

build-artifacts-stage.ts:

import {Stage, StageProps} from "aws-cdk-lib"
import {Construct} from "constructs"
import {BuildArtifactsStack} from "./build-artifacts-stack";


export class BuildArtifactsStage extends Stage {
    constructor(scope: Construct, id: string, props?: StageProps) {
        super(scope, id, props);

        const buildArtifactsStack = new BuildArtifactsStack(this, 'BuildArtifactsStack')
    }
}

Below in the commands: is the logic I would like to be executed in the fourth Stage of my code pipeline. build-artifacts-stack.ts:

import {Stack, StackProps} from "aws-cdk-lib";
import {Construct} from "constructs";
import {CodeBuildStep} from "aws-cdk-lib/pipelines";

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

        new CodeBuildStep('BuildOldCdkModule', {
            commands: ['cd ../old-cdk-module', 'npm ci', 'npm run build'],
        })
    }
}


Solution 1:[1]

We are exploring very similar use cases. I am no cdk expert, but hopefully this can be useful.

The example in the workshop seems to have just one cdk project that is changed from deploying the app directly (old version) to deploying the pipeline which deploys the application (same cdk app, just new version):

Next, since the purpose of our pipeline is to deploy our application stack, we no longer want the main CDK application to deploy our original app. Instead, we can change the entry point to deploy our pipeline, which will in turn deploy the application. To do this, edit the code in bin/cdk-workshop.ts

I stumbled upon the following example which seems to be more in line with what you are doing, since it has the codepipeline in a separate cdk app. Hopefully it can give you some pointers. However, note that the example is cdk1:

https://medium.com/andy-le/building-a-dynamic-aws-pipeline-with-cdk-5d5426fc0493

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wab