aws / aws/aws-cdk

Creating a codepipeline.Pipeline with a codebuild.PipelineProject in a different stack results in a circular reference

Open
#3,300 30 comments 6 reactions 0 assignees View on GitHub
@aws-cdk/aws-codepipeline bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

**Note: for support questions, please first reference our [documentation](https://docs.aws.amazon.com/cdk/api/latest), then use [Stackoverflow](https://stackoverflow.com/questions/ask?tags=aws-cdk)**. This repository's issues are intended for feature requests and bug reports.

* **I'm submitting a ...**
- [X] :beetle: bug report

* **What is the current behavior?**
*If the current behavior is a :beetle:bug:beetle:: Please provide the steps to reproduce*

I am trying to follow the example on this feature https://github.com/aws/aws-cdk/pull/1924 to create a codepipeline with codebuild actions that are created in a separate stack. When I do, I get this error:

```
Error: 'BuildStack' depends on 'PipelineStack' (BuildStack/CodeBuildProject/Role/DefaultPolicy/Resource -> PipelineStack/Pipeline/ArtifactsBucket/Resource.Arn). Adding this dependency (PipelineStack/Pipeline/ArtifactsBucketEncryptionKey/Resource -> BuildStack/CodeBuildProject/Role/Resource.Arn) would create a cyclic reference.
```

Here is my sample code that I am working with

```typescript
#!/usr/bin/env node
import 'source-map-support/register';
import cdk = require('@aws-cdk/core');
import commit = require('@aws-cdk/aws-codecommit');
import pipeline = require('@aws-cdk/aws-codepipeline');
import pipeline_actions = require('@aws-cdk/aws-codepipeline-actions');
import codebuild = require ('@aws-cdk/aws-codebuild');

const region = 'us-east-2';
const account = '12345678';

const app = new cdk.App();

const pipelineStack = new cdk.Stack(app, `PipelineStack`, {
env: {
account: account,
region: region
}
});

const repo = new commit.Repository(pipelineStack, 'Repo', {
repositoryName: 'test'
});

const sourceOutput = new pipeline.Artifact();
const sourceAction = new pipeline_actions.CodeCommitSourceAction({
actionName: 'CodeCommit',
repository: repo,
output: sourceOutput,
branch: 'master'
});

const codePipeline = new pipeline.Pipeline(pipelineStack, 'Pipeline', {
pipelineName: 'test',
stages: [
{
stageName: 'Source',
actions: [sourceAction],
}
],
});

const buildStack = new cdk.Stack(app, `BuildStack`, {
env: {
account: account,
region: region
}
});

const buildProject = new codebuild.PipelineProject(buildStack, 'CodeBuildProject', {
projectName: 'test-build',
});

const buildAction = new pipeline_actions.CodeBuildAction({
project: buildProject,
actionName: 'CodeBuild',
input: sourceOutput,
});

codePipeline.addStage({
stageName: `Deploy-Environment`,
actions: [buildAction]
});
```

* **What is the expected behavior (or behavior of feature suggested)?**

Based off the feature https://github.com/aws/aws-cdk/pull/1924, I would expect to be able to create a codepipeline in one stack and then add a codebuild project created in a separate stack as a stage. Currently I am trying everything within the same account.

* **What is the motivation / use case for changing the behavior or adding this feature?**

The use case is to be able to create a codepipeline in one account with build/deploy actions
in a separate account. Most of the motivation is described in the feature https://github.com/aws/aws-cdk/pull/1924

* **Please tell us about your environment:**

- CDK CLI Version: 1.0.0
- Module Version: 1.0.0
- OS: [OSX Mojave ]
- Language: [TypeScript ]

* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)

Full error:
```
/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/stack.ts:272
throw new Error(`'${stack.node.path}' depends on '${this.node.path}' (${dep.join(', ')}). Adding this dependency (${reason}) would create a cyclic reference.`);
^
Error: 'BuildStack' depends on 'PipelineStack' (BuildStack/CodeBuildProject/Role/DefaultPolicy/Resource -> PipelineStack/Pipeline/ArtifactsBucket/Resource.Arn). Adding this dependency (PipelineStack/Pipeline/ArtifactsBucketEncryptionKey/Resource -> BuildStack/CodeBuildProject/Role/Resource.Arn) would create a cyclic reference.
at Stack.addDependency (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/stack.ts:272:15)
at CfnReference.consumeFromStack (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/private/cfn-reference.ts:125:22)
at Stack.prepare (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/stack.ts:489:23)
at Function.prepare (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/construct.ts:84:28)
at Function.synth (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/construct.ts:41:10)
at App.synth (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/app.ts:128:36)
at process. (/Users/user/cdk/demo/node_modules/@aws-cdk/core/lib/app.ts:111:45)
at Object.onceWrapper (events.js:284:20)
at process.emit (events.js:196:13)
at process.EventEmitter.emit (domain.js:471:20)
```

Contributor guide

Open the contributing guide

Research direction

The reproduction is the TypeScript sample in the issue body; start by running it and inspect core/lib/stack.ts:272 and core/lib/private/cfn-reference.ts:125, where the circular stack dependency is reported. Done means a pipeline in PipelineStack can use the PipelineProject in BuildStack without synthesis failing, with the cross-stack case covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
ci-cd, cloud, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.