My goal is to set up a cdk cicd pipeline for an app that already has a cdk stack and deploys successfully manually with cdk deploy. I want the cdk pipeline to automatically deploy the stack whenever changes are pushed to the main branch so that it's not a manual process.
I've been following both the AWS tutorial and this blog, which have helped me progress quite far.
My folder structure looks like this:
https://i.sstatic.net/wDFnB.png
The stack I want to deploy via the pipeline is located in lib/tagworx-rfid-cdk-stack.ts
Running cdk deploy from the root folder deploys this stack without any issues.
The CICD Pipeline stack is in /infra/lib.
When I try to deploy from there, it works for a simple test. However, when I try to add the main stack that I want the CICD pipeline to deploy, I encounter the following error:
Error: Cannot find asset at root/infra/lib/resources/layers/helper. I believe this error is related to the layer I have in the main stack that I want the CICD pipeline to deploy. When it tries to execute lambda.Code.fromAsset('resources/layers/helper'), the process is still in the cicd location instead of the main stack location, causing it to not find the reference.
lib/tagworx-rfid-cdk-stack.ts - this is the stack I want the CICD pipeline to deploy
export class TagworxRfidCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const layer = new lambda.LayerVersion(this, 'HelperLayer', {
code: lambda.Code.fromAsset('resources/layers/helper'),
description: 'Axios and other helper functions',
compatibleRuntimes: [lambda.Runtime.NODEJS_16_X],
});
...
/infra/lib/infra-stack.ts
export class InfraStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: PipelineAppProps) {
super(scope, id, props);
const buildCodeArtifactPolicy: PolicyStatement[] = [
// some policies removed for brevity
];
const buildEnvironmentForStep: BuildEnvironment = {
computeType: ComputeType.SMALL,
buildImage: LinuxBuildImage.STANDARD_6_0,
privileged: true,
};
const branch = 'main'
const codeBuildStepSynth = new CodeBuildStep(`${props.name}-build-step`, {
input: CodePipelineSource.connection(`${props.ghRepository}`, branch, {
connectionArn: props.codestarConnectionArn,
}),
installCommands: ["npm install -g npm@latest"],
commands: ["npm install", "npm run build", "npx cdk synth" ],
rolePolicyStatements: buildCodeArtifactPolicy,
buildEnvironment: buildEnvironmentForStep,
primaryOutputDirectory: "cdk.out",
});
const codeBuildStepArtifact = new CodeBuildStep(
`${props.name}-artifact-publish-step`,
{
commands: [
"npm ci",
],
rolePolicyStatements: buildCodeArtifactPolicy,
buildEnvironment: buildEnvironmentForStep,
}
);
const pipeline: CodePipeline = new CodePipeline(
this,
`${props?.name}-code-pipeline`,
{
pipelineName: `${props.name}-pipeline`,
selfMutation: true,
synth: codeBuildStepSynth,
}
);
const env = { account: '1111111111', region: 'eu-west-1' };
const stackName = `dev-rfid-stack-${env.region}`;
// THIS 👇 is what has broken the cdk deploy, without this it deploys the cicd pipeline,
// but I think I need this to actually deploy the stack?
pipeline.addStage(new InfraAppStage(this, stackName, {
env: env
}));
pipeline.addWave(`${props.name}-code-artifact-publish`, {
post: [codeBuildStepArtifact],
});
pipeline.buildPipeline();
}
}
/infra/lib/infra-app-stage
export class InfraAppStage extends cdk.Stage {
constructor(scope: Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);
const lambdaStack = new TagworxRfidCdkStack(this, 'my-test-stack');
}
}
When I add the additional app stage:
pipeline.addStage(new InfraAppStage(this, stackName, {
env: env
}));
and attempt to deploy, I encounter the error.
It seems to be looking in the cicd folder for references in the actual stack I want to deploy?
I expected the references to be aware of their location?