Skip to content

fix(kinesisfirehose): can't call grantPrincipal multiple times #34682

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

WinterYukky
Copy link
Contributor

@WinterYukky WinterYukky commented Jun 11, 2025

Issue # (if applicable)

Closes #.

Reason for this change

DeliveryStream creates a new iam.Role every reference grantPrincipal, so we get the error There is already a Construct with name 'Service Role' in DeliveryStream [Delivery Stream Multiple]. This is easy to reproduce if you use deliveryStream with multiple grantXXX methods.

This is test result before fix codes.

yarn test aws-kinesisfirehose/test/delivery-stream.test.ts -t 
"multiple calls to grantPrincipal should return the same instance of IAM role"
yarn run v1.22.22
$ jest aws-kinesisfirehose/test/delivery-stream.test.ts -t 'multiple calls to grantPrincipal should return the same instance of IAM role'
ts-jest[config] (WARN) 
    The "ts-jest" config option "isolatedModules" is deprecated and will be removed in v30.0.0. Please use "isolatedModules: true" in /workspaces/aws-cdk/packages/aws-cdk-lib/tsconfig.json instead, see https://www.typescriptlang.org/tsconfig/#isolatedModules
  
 FAIL  aws-kinesisfirehose/test/delivery-stream.test.ts
  delivery stream
    ✕ multiple calls to grantPrincipal should return the same instance of IAM role (45 ms)
    ○ other tests...

  ● delivery stream › multiple calls to grantPrincipal should return the same instance of IAM role

    expect(received).not.toThrow()

    Error name:    "Error"
    Error message: "There is already a Construct with name 'Service Role' in DeliveryStream [Delivery Stream Multiple]"

          155 |
          156 |   constructor(scope: Construct, id: string, props: ResourceProps = {}) {
        > 157 |     super(scope, id);
              |     ^
          158 |
          159 |     if ((props.account !== undefined || props.region !== undefined) && props.environmentFromArn !== undefined) {
          160 |       throw new ValidationError(`Supply at most one of 'account'/'region' (${props.account}/${props.region}) and 'environmentFromArn' (${props.environmentFromArn})`, this);

          at Node.addChild (../../node_modules/constructs/src/construct.ts:430:13)
          at new Node (../../node_modules/constructs/src/construct.ts:71:17)
          at new Construct (../../node_modules/constructs/src/construct.ts:482:17)
          at new Resource (core/lib/resource.ts:157:5)
          at new Role (aws-iam/lib/role.ts:472:5)
          at new Role (core/lib/prop-injectable.ts:36:7)
          at WrappedClass.get grantPrincipal [as grantPrincipal] (aws-kinesisfirehose/lib/delivery-stream.ts:320:26)
          at aws-kinesisfirehose/test/delivery-stream.test.ts:410:33
          at Object.<anonymous> (../../node_modules/expect/build/toThrowMatchers.js:74:11)
          at Object.throwingMatcher [as toThrow] (../../node_modules/expect/build/index.js:320:21)
          at Object.<anonymous> (aws-kinesisfirehose/test/delivery-stream.test.ts:410:53)

      408 |     });
      409 |     const principal = deliveryStream.grantPrincipal;
    > 410 |     expect(() => deliveryStream.grantPrincipal).not.toThrow();
          |                                                     ^
      411 |     expect(deliveryStream.grantPrincipal).toBe(principal);
      412 |   });
      413 |

      at Object.<anonymous> (aws-kinesisfirehose/test/delivery-stream.test.ts:410:53)

Description of changes

I changed grantPrincipal implementation from create a new iam.Role every reference to create a new iam.Role only at first reference.

Describe any new or updated permissions being added

None.

Description of how you validated changes

Create the new unit test.

Checklist


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

@aws-cdk-automation aws-cdk-automation requested a review from a team June 11, 2025 09:28
@github-actions github-actions bot added p2 admired-contributor [Pilot] contributed between 13-24 PRs to the CDK labels Jun 11, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

(This review is outdated)

@WinterYukky
Copy link
Contributor Author

Exemption Request

We know at the unit test level whether this bug has been resolved

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Jun 11, 2025
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 11, 2025
@aws-cdk-automation aws-cdk-automation added pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Jun 11, 2025
@ozelalisen ozelalisen self-assigned this Jun 16, 2025
Copy link
Member

@ozelalisen ozelalisen left a comment

Choose a reason for hiding this comment

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

Left some minor comments

@@ -402,6 +402,15 @@ describe('delivery stream', () => {
expect(deliveryStream.grantPrincipal).toBeInstanceOf(iam.Role);
});

test('multiple calls to grantPrincipal should return the same instance of IAM role', () => {
Copy link
Member

Choose a reason for hiding this comment

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

While the test is good, it could be more explicit about what's being tested. The test name indicates it's checking that multiple calls return the same instance, but the first assertion is actually checking that it doesn't throw an error. Consider splitting this into two separate tests or making the test name more explicit about both behaviors being tested

}
// backwards compatibility
return new iam.Role(this, 'Service Role', {
// backwards compatibility - create role only once
Copy link
Member

Choose a reason for hiding this comment

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

The comment backwards compatibility doesn't fully explain the purpose of this change. It's not just for backwards compatibility but also to fix a bug where multiple calls to grantPrincipal would create multiple roles with the same name. Improve the comment to better explain the purpose. Maybe something like:
Create the role only once to ensure multiple calls to grantPrincipal return the same role instance, while maintaining backwards compatibility

@ozelalisen ozelalisen added pr-linter/exempt-integ-test The PR linter will not require integ test changes and removed pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. labels Jun 16, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 16, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review June 16, 2025 15:06

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admired-contributor [Pilot] contributed between 13-24 PRs to the CDK p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants