Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codepipeline-actions): cross stack reference causes stack cycle in sources that use CloudWatch Events #20149

Merged
merged 10 commits into from
Aug 9, 2022

Conversation

skinny85
Copy link
Contributor

@skinny85 skinny85 commented Apr 29, 2022

When using a newly-created, CDK-managed resource, such as an S3 Bucket or a CodeCommit Repository,
as the source of a CodePipeline, when it's in a different Stack than the pipeline
(but in the same environment as it),
and we use CloudWatch Events to trigger the pipeline from the source,
that would result in a cycle:

  1. Because the Event Rule is created in the source Stack, that Stack needs the name of the pipeline to trigger from the Rule, and also the Role to use for the trigger, which is created in the target (in this case, the pipeline) Stack. So, the source Stack depends on the pipeline Stack.
  2. The pipeline Stack needs the name of the source resource. So, the pipeline Stack depends on the source Stack.

The only way to break this cycle is to move the Event Rule to the target Stack.
This PR adds an API to the Events module to make it possible for event-creating constructs to make that choice,
and uses that capability in the CodePipeline CodeCommitSourceAction and S3SourceAction.

Fixes #3087
Fixes #8042
Fixes #10896


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@skinny85 skinny85 requested a review from rix0rrr April 29, 2022 18:04
@gitpod-io
Copy link

gitpod-io bot commented Apr 29, 2022

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Apr 29, 2022
@github-actions github-actions bot added bug This issue is a bug. effort/large Large work item – several weeks of effort p1 labels Apr 29, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team April 29, 2022 18:04
@skinny85
Copy link
Contributor Author

@rix0rrr would be curious to get your opinion on whether you like this approach!

*
* @default - none (the main scope will be used, even for cross-stack Events)
*/
readonly scopeIfCrossStack?: Construct;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name definitely sucks, so I'm very open to suggestions for a better one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just crossStackScope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll update to crossStackScope.

@skinny85
Copy link
Contributor Author

Note that, before this PR is merged, we should make a similar change that we have now in CodeCommitSourceAction also in S3SourceAction.

@TheRealAmazonKendra TheRealAmazonKendra changed the base branch from v1-main to main July 13, 2022 00:07
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I think I like this but I'd definitely want to see more until testing and some integration tests.

*
* @default - none (the main scope will be used, even for cross-stack Events)
*/
readonly scopeIfCrossStack?: Construct;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just crossStackScope?

@@ -119,7 +88,7 @@ export class Rule extends Resource implements IRule {
private readonly _xEnvTargetsAdded = new Set<string>();

constructor(scope: Construct, id: string, props: RuleProps = { }) {
super(scope, id, {
super(determineRuleScope(scope, props), id, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is verified by every single test that uses any Rule, of which there are hundreds, and also the new "allows using a new Repository from another Stack as a source of the Pipeline, with Events" test that was added in this PR.

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I can let that go. I suppose I'm reasonable every once in a while about my testing demands 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get an integration test on this, though? It seems like exactly the sort of thing that we'd want integ tests for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 🙂.

@TheRealAmazonKendra TheRealAmazonKendra removed the contribution/core This is a PR that came from AWS. label Aug 3, 2022
@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 3, 2022 05:11

Pull request has been modified.

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just putting this in changes requested so I see when an update has been made. You mentioned that this sam change should be made for S3SourceAction. Is that a change you'd like to make or should I go ahead and take that on?

@TheRealAmazonKendra TheRealAmazonKendra changed the title fix(codepipeline): break cycle for cross-Stack sources using Events fix(codepipeline-actions): cross stack reference causes cyclic reference error in CodeCommitSourceAction Aug 3, 2022
@skinny85
Copy link
Contributor Author

skinny85 commented Aug 4, 2022

Just putting this in changes requested so I see when an update has been made. You mentioned that this sam change should be made for S3SourceAction. Is that a change you'd like to make or should I go ahead and take that on?

I'll do it. I'll combine it with the integration test.

@skinny85
Copy link
Contributor Author

skinny85 commented Aug 7, 2022

Just putting this in changes requested so I see when an update has been made. You mentioned that this sam change should be made for S3SourceAction. Is that a change you'd like to make or should I go ahead and take that on?

I'll do it. I'll combine it with the integration test.

Done in the latest revision. @TheRealAmazonKendra would appreciate another look!

@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 7, 2022 05:48

Pull request has been modified.

@skinny85 skinny85 changed the title fix(codepipeline-actions): cross stack reference causes cyclic reference error in CodeCommitSourceAction fix(codepipeline-actions): cross stack reference causes cyclic reference error in sources that use CloudWatch Events Aug 7, 2022
@skinny85 skinny85 changed the title fix(codepipeline-actions): cross stack reference causes cyclic reference error in sources that use CloudWatch Events fix(codepipeline-actions): cross stack reference causes stack cycle in sources that use CloudWatch Events Aug 7, 2022
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one more issue in here. Looks like this isn't using the new IntegTest construct to run the integration test. Can you please make that update? Besides that, this looks good to go!

@skinny85
Copy link
Contributor Author

skinny85 commented Aug 9, 2022

Just one more issue in here. Looks like this isn't using the new IntegTest construct to run the integration test. Can you please make that update? Besides that, this looks good to go!

Done!

@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 9, 2022 05:01

Pull request has been modified.

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@mergify
Copy link
Contributor

mergify bot commented Aug 9, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@skinny85
Copy link
Contributor Author

skinny85 commented Aug 9, 2022

Is main broken right now...?

@aws-cdk/integ-runner: PASS test/runner/snapshot-test-runner.test.js
@aws-cdk/integ-runner: PASS test/runner/integ-test-runner.test.js
@aws-cdk/integ-runner: FAIL test/workers/integ-worker.test.js
@aws-cdk/integ-runner:   � test runner › has snapshot
@aws-cdk/integ-runner:     expect(received).toEqual(expected) // deep equality
@aws-cdk/integ-runner:     - Expected  - 1
@aws-cdk/integ-runner:     + Received  + 6
@aws-cdk/integ-runner:     - Array []
@aws-cdk/integ-runner:     + Array [
@aws-cdk/integ-runner:     +   Object {
@aws-cdk/integ-runner:     +     "discoveryRoot": "test/test-data",
@aws-cdk/integ-runner:     +     "fileName": "test/test-data/xxxxx.test-with-snapshot.js",
@aws-cdk/integ-runner:     +   },
@aws-cdk/integ-runner:     + ]
@aws-cdk/integ-runner:       150 |     ]));
@aws-cdk/integ-runner:       151 |
@aws-cdk/integ-runner:     > 152 |     expect(results).toEqual([]);
@aws-cdk/integ-runner:           |                     ^
@aws-cdk/integ-runner:       153 |   });
@aws-cdk/integ-runner:       154 |
@aws-cdk/integ-runner:       155 |   test('deploy failed', () => {
@aws-cdk/integ-runner:       at Object.<anonymous> (test/workers/integ-worker.test.ts:152:21)
@aws-cdk/integ-runner: PASS test/workers/snapshot-worker.test.js
@aws-cdk/integ-runner: =============================== Coverage summary ===============================
@aws-cdk/integ-runner: Statements   : 81.1% ( 571/704 )
@aws-cdk/integ-runner: Branches     : 71.94% ( 377/524 )
@aws-cdk/integ-runner: Functions    : 81.1% ( 103/127 )
@aws-cdk/integ-runner: Lines        : 81.21% ( 549/676 )
@aws-cdk/integ-runner: ================================================================================
@aws-cdk/integ-runner: Test Suites: 1 failed, 7 passed, 8 total
@aws-cdk/integ-runner: Tests:       1 failed, 63 passed, 64 total
@aws-cdk/integ-runner: Snapshots:   0 total
@aws-cdk/integ-runner: Time:        4.34 s
@aws-cdk/integ-runner: Ran all test suites.
@aws-cdk/integ-runner: Error: /codebuild/output/src162968263/src/github.com/aws/aws-cdk/node_modules/jest/bin/jest.js exited with error code 1
@aws-cdk/integ-runner: Tests failed. Total time (5.3s) | /codebuild/output/src162968263/src/github.com/aws/aws-cdk/node_modules/jest/bin/jest.js (5.3s)

@TheRealAmazonKendra
Copy link
Contributor

@Mergifyio update

@mergify
Copy link
Contributor

mergify bot commented Aug 9, 2022

update

✅ Branch has been successfully updated

@TheRealAmazonKendra
Copy link
Contributor

Is main broken right now...?

@aws-cdk/integ-runner: PASS test/runner/snapshot-test-runner.test.js
@aws-cdk/integ-runner: PASS test/runner/integ-test-runner.test.js
@aws-cdk/integ-runner: FAIL test/workers/integ-worker.test.js
@aws-cdk/integ-runner:   � test runner › has snapshot
@aws-cdk/integ-runner:     expect(received).toEqual(expected) // deep equality
@aws-cdk/integ-runner:     - Expected  - 1
@aws-cdk/integ-runner:     + Received  + 6
@aws-cdk/integ-runner:     - Array []
@aws-cdk/integ-runner:     + Array [
@aws-cdk/integ-runner:     +   Object {
@aws-cdk/integ-runner:     +     "discoveryRoot": "test/test-data",
@aws-cdk/integ-runner:     +     "fileName": "test/test-data/xxxxx.test-with-snapshot.js",
@aws-cdk/integ-runner:     +   },
@aws-cdk/integ-runner:     + ]
@aws-cdk/integ-runner:       150 |     ]));
@aws-cdk/integ-runner:       151 |
@aws-cdk/integ-runner:     > 152 |     expect(results).toEqual([]);
@aws-cdk/integ-runner:           |                     ^
@aws-cdk/integ-runner:       153 |   });
@aws-cdk/integ-runner:       154 |
@aws-cdk/integ-runner:       155 |   test('deploy failed', () => {
@aws-cdk/integ-runner:       at Object.<anonymous> (test/workers/integ-worker.test.ts:152:21)
@aws-cdk/integ-runner: PASS test/workers/snapshot-worker.test.js
@aws-cdk/integ-runner: =============================== Coverage summary ===============================
@aws-cdk/integ-runner: Statements   : 81.1% ( 571/704 )
@aws-cdk/integ-runner: Branches     : 71.94% ( 377/524 )
@aws-cdk/integ-runner: Functions    : 81.1% ( 103/127 )
@aws-cdk/integ-runner: Lines        : 81.21% ( 549/676 )
@aws-cdk/integ-runner: ================================================================================
@aws-cdk/integ-runner: Test Suites: 1 failed, 7 passed, 8 total
@aws-cdk/integ-runner: Tests:       1 failed, 63 passed, 64 total
@aws-cdk/integ-runner: Snapshots:   0 total
@aws-cdk/integ-runner: Time:        4.34 s
@aws-cdk/integ-runner: Ran all test suites.
@aws-cdk/integ-runner: Error: /codebuild/output/src162968263/src/github.com/aws/aws-cdk/node_modules/jest/bin/jest.js exited with error code 1
@aws-cdk/integ-runner: Tests failed. Total time (5.3s) | /codebuild/output/src162968263/src/github.com/aws/aws-cdk/node_modules/jest/bin/jest.js (5.3s)

Wut? Gotta be a transient issue. Let's see what the retry does.

@mergify
Copy link
Contributor

mergify bot commented Aug 9, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 87b5e67
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit adf4022 into aws:main Aug 9, 2022
@mergify
Copy link
Contributor

mergify bot commented Aug 9, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@skinny85 skinny85 deleted the codepipeline-cycle-sources-events-10896 branch August 10, 2022 05:06
josephedward pushed a commit to josephedward/aws-cdk that referenced this pull request Aug 30, 2022
…n sources that use CloudWatch Events (aws#20149)

When using a newly-created, CDK-managed resource, such as an S3 Bucket or a CodeCommit Repository,
as the source of a CodePipeline, when it's in a different Stack than the pipeline
(but in the same environment as it),
and we use CloudWatch Events to trigger the pipeline from the source,
that would result in a cycle:

1. Because the Event Rule is created in the source Stack, that Stack needs the name of the pipeline to trigger from the Rule, and also the Role to use for the trigger, which is created in the target (in this case, the pipeline) Stack. So, the source Stack depends on the pipeline Stack.
2. The pipeline Stack needs the name of the source resource. So, the pipeline Stack depends on the source Stack.

The only way to break this cycle is to move the Event Rule to the target Stack.
This PR adds an API to the Events module to make it possible for event-creating constructs to make that choice,
and uses that capability in the CodePipeline `CodeCommitSourceAction` and `S3SourceAction`.

Fixes aws#3087
Fixes aws#8042
Fixes aws#10896

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/large Large work item – several weeks of effort p1
Projects
None yet
3 participants