Commit 94d7e34
authored
fix(dynamodb): addToResourcePolicy has no effect (#35554)
### Issue # (if applicable)
Closes #35062.
### Reason for this change
The `addToResourcePolicy()` method for DynamoDB tables had no effect - it was not adding resource policies to the synthesized CloudFormation template. Users calling `table.addToResourcePolicy()` found that their policies were ignored, forcing them to use insecure workarounds.
### Description of changes
Fixed the `addToResourcePolicy()` method to properly update the CloudFormation table's resource policy:
- **Fixed core bug**: Added missing `this.table.resourcePolicy = { policyDocument: this.resourcePolicy }` line in `addToResourcePolicy()` method
- **Restored intended functionality**: Resource policies now appear in synthesized CloudFormation templates
- **Applied to both Table V1 and V2**: Consistent behavior across all DynamoDB table constructs
- **Avoids circular dependencies**: Uses wildcard resources (`*`) pattern to prevent CloudFormation circular dependency issues with auto-generated table names
- **Added comprehensive tests**: 5 new tests covering both wildcard and scoped resource scenarios
- **Updated README.md**: Completely rewrote `addToResourcePolicy` documentation:
- Removed problematic examples that would create circular dependencies
- Added correct wildcard resource pattern following KMS approach
- Documented the CloudFormation limitation and workarounds
- Provided clear examples for both standard and scoped resource policies
**Before** (broken):
```typescript
// This had no effect - policy was ignored
table.addToResourcePolicy(new iam.PolicyStatement({
actions: ['dynamodb:GetItem'],
principals: [new iam.AccountRootPrincipal()],
resources: [table.tableArn], // This would also create circular dependency
}));
// CloudFormation template: No ResourcePolicy property
```
**After** (fixed):
```typescript
// Now works correctly - policy appears in CloudFormation
table.addToResourcePolicy(new iam.PolicyStatement({
actions: ['dynamodb:GetItem'],
principals: [new iam.AccountRootPrincipal()],
resources: ['*'], // Wildcard avoids circular dependency (KMS pattern)
}));
// CloudFormation template: ResourcePolicy.PolicyDocument properly set
```
**For scoped resources** (requires explicit table name):
```typescript
const table = new dynamodb.Table(this, 'MyTable', {
tableName: 'my-explicit-table-name', // Explicit name enables scoped resources
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});
table.addToResourcePolicy(new iam.PolicyStatement({
actions: ['dynamodb:GetItem'],
principals: [new iam.AccountRootPrincipal()],
resources: [
Fn.sub('arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/my-explicit-table-name')
],
}));
```
**Architecture Note**: DynamoDB tables use inline `ResourcePolicy` properties (like KMS keys) rather than separate policy resources. Due to CloudFormation's circular dependency limitations, resource policies must use wildcard resources (`*`) when table names are auto-generated, or explicit table names must be specified for scoped resources.
### Describe any new or updated permissions being added
N/A - No new IAM permissions required. This change only affects how existing resource policies are structured.
### Description of how you validated changes
- **Unit tests**: Added new `addToResourcePolicy` tests:
- Standard wildcard resource usage (`resources: ['*']`)
- Explicit table name workaround for scoped resources
- Comprehensive limitation documentation
- **Integration tests**: Added comprehensive integration test covering both wildcard and scoped resource patterns
- **Full test suite**: All DynamoDB unit tests pass, confirming no regressions
- **CloudFormation validation**: Verified synthesis works without circular dependency errors
- **Deployment testing**: Confirmed resource policies are properly applied at deployment time
### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*1 parent a374b6b commit 94d7e34
File tree
23 files changed
+1961
-292
lines changed- packages
- @aws-cdk-testing/framework-integ/test/aws-dynamodb/test
- integ.dynamodb.add-to-resource-policy.js.snapshot
- integ.dynamodb.policy.js.snapshot
- aws-cdk-lib/aws-dynamodb
- lib
- test
23 files changed
+1961
-292
lines changedLines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 154 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments