Skip to content

Conversation

@pahud
Copy link
Contributor

@pahud pahud commented Nov 7, 2025

Issue # (if applicable)

Closes #35967.

Reason for this change

In CDK 2.222.0, PR #35554 fixed addToResourcePolicy() to actually work (it was previously a no-op). This exposed a circular dependency issue when using grantReadData() or other grant methods with AccountRootPrincipal.

When AccountRootPrincipal is used with grant methods, the IAM Grant system adds the policy to the table's resource policy (since it's in the same account). The resource policy statement included the table's ARN (!GetAtt Table.Arn), creating a circular dependency: Table → ResourcePolicy → Table.Arn → Table.

This is a regression that breaks existing user code that worked in 2.221.1.

Description of changes

Applied the established KMS grant pattern to DynamoDB by adding resourceSelfArns: ['*'] parameter to Grant.addToPrincipalOrResource() calls in the combinedGrant method.

How it works:

  • resourceArns contains actual table ARNs → used for principal policies (IAM user/role policies)
  • resourceSelfArns: ['*'] → used for resource policies (table's resource policy)
  • IAM Grant system automatically chooses which to use based on context
  • No circular dependency because resource policy uses wildcard instead of !GetAtt Table.Arn

Why wildcard is safe:

  • Wildcard is scoped to the table's resource policy (not global)
  • Resource policy is attached to specific table resource
  • Principal and Action fields still enforce access control
  • Same pattern used by KMS for years in production

Files modified:

  • packages/aws-cdk-lib/aws-dynamodb/lib/table.ts - Added resourceSelfArns: ['*'] to combinedGrant method
  • packages/aws-cdk-lib/aws-dynamodb/lib/table-v2-base.ts - Applied identical change for Table V2
  • packages/aws-cdk-lib/aws-dynamodb/README.md - Added documentation about grant methods and resource policy interaction

Before (causes circular dependency):

const table = new dynamodb.Table(this, 'Table', {
  partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});

// This caused circular dependency error in 2.222.0
table.grantReadData(new iam.AccountRootPrincipal());
// Error: Circular dependency between resources: [Table]

After (no circular dependency):

const table = new dynamodb.Table(this, 'Table', {
  partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});

// This now works correctly
table.grantReadData(new iam.AccountRootPrincipal());
// ✓ Resource policy uses wildcard, no circular dependency

CloudFormation template change:

{
  "Resources": {
    "Table": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "ResourcePolicy": {
          "PolicyDocument": {
            "Statement": [{
              "Action": ["dynamodb:BatchGetItem", "dynamodb:GetItem", "dynamodb:Query", "dynamodb:Scan"],
              "Effect": "Allow",
              "Principal": { "AWS": "arn:aws:iam::ACCOUNT:root" },
              "Resource": "*"
            }]
          }
        }
      }
    }
  }
}

Describe any new or updated permissions being added

N/A - This fix does not add new permissions. It resolves how existing grant methods generate resource policies to avoid circular dependencies.

Description of how you validated changes

  • Unit tests: Added 2 new tests validating AccountRootPrincipal with grant methods

    • packages/aws-cdk-lib/aws-dynamodb/test/dynamodb.test.ts: Test for Table V1
    • packages/aws-cdk-lib/aws-dynamodb/test/table-v2.test.ts: Test for Table V2
    • Both tests verify resource policy uses wildcard (*) to avoid circular dependency
    • All 348 unit tests pass (346 existing + 2 new)
  • Integration tests: Enhanced existing integration test with grant scenario

    • packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.add-to-resource-policy.ts
    • Added TEST 3: Validates grantWriteData(new AccountRootPrincipal()) works without circular dependency
    • Successfully deployed to AWS (us-east-1)
    • CloudFormation synthesis succeeds, no circular dependency errors
    • Snapshots updated with GrantTable resource
  • Regression testing: All 346 existing tests pass

    • Grant methods with IAM Users still work
    • Grant methods with IAM Roles still work
    • Grant methods with Service Principals still work
    • Tables with indexes work correctly
    • Global tables (Table V2) work correctly
    • Encrypted tables work correctly

Checklist


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

…tables

- Enhance DynamoDB Table construct to support `addToResourcePolicy` method
- Update integration tests to demonstrate resource policy configuration
- Modify table implementation to allow flexible resource policy management
- Improve documentation and test coverage for resource policy feature
- Ensure compatibility with existing DynamoDB table configurations
@aws-cdk-automation aws-cdk-automation requested a review from a team November 7, 2025 18:13
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Nov 7, 2025
@pahud pahud marked this pull request as ready for review November 7, 2025 18:13
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 7, 2025
@pahud pahud requested review from alvazjor and removed request for alvazjor November 7, 2025 18:14
@Abogical Abogical self-assigned this Nov 10, 2025
@Abogical Abogical added the pr/requires-two-approvers This PR is critical (e.g., security, broadly-impacting) and requires 2 approvers to be merged. label Nov 11, 2025
Copy link
Member

@Abogical Abogical left a comment

Choose a reason for hiding this comment

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

Thanks! I've asked for another reviewer to this.

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2025

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

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2025

This pull request has been removed from the queue for the following reason: pull request branch update failed.

The pull request can't be updated

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/integration-test-deployment.yml without workflows permission.

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio requeue comment.

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2025

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

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2025

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

@mergify mergify bot merged commit 24d2adf into aws:main Nov 11, 2025
18 of 20 checks passed
@github-actions
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p1 pr/requires-two-approvers This PR is critical (e.g., security, broadly-impacting) and requires 2 approvers to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dynamodb: CDK Error WIth Stacks Containing Dynamodb

3 participants