Skip to content

Commit 94d7e34

Browse files
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

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/add-to-resource-policy-test-stack.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
{
2+
"Resources": {
3+
"WildcardTableE075CD4D": {
4+
"Type": "AWS::DynamoDB::Table",
5+
"Properties": {
6+
"AttributeDefinitions": [
7+
{
8+
"AttributeName": "id",
9+
"AttributeType": "S"
10+
}
11+
],
12+
"KeySchema": [
13+
{
14+
"AttributeName": "id",
15+
"KeyType": "HASH"
16+
}
17+
],
18+
"ProvisionedThroughput": {
19+
"ReadCapacityUnits": 5,
20+
"WriteCapacityUnits": 5
21+
},
22+
"ResourcePolicy": {
23+
"PolicyDocument": {
24+
"Statement": [
25+
{
26+
"Action": [
27+
"dynamodb:GetItem",
28+
"dynamodb:PutItem",
29+
"dynamodb:Query"
30+
],
31+
"Effect": "Allow",
32+
"Principal": {
33+
"AWS": {
34+
"Fn::Join": [
35+
"",
36+
[
37+
"arn:",
38+
{
39+
"Ref": "AWS::Partition"
40+
},
41+
":iam::",
42+
{
43+
"Ref": "AWS::AccountId"
44+
},
45+
":root"
46+
]
47+
]
48+
}
49+
},
50+
"Resource": "*"
51+
}
52+
],
53+
"Version": "2012-10-17"
54+
}
55+
}
56+
},
57+
"UpdateReplacePolicy": "Delete",
58+
"DeletionPolicy": "Delete"
59+
},
60+
"ScopedTableC019D4A1": {
61+
"Type": "AWS::DynamoDB::Table",
62+
"Properties": {
63+
"AttributeDefinitions": [
64+
{
65+
"AttributeName": "id",
66+
"AttributeType": "S"
67+
}
68+
],
69+
"KeySchema": [
70+
{
71+
"AttributeName": "id",
72+
"KeyType": "HASH"
73+
}
74+
],
75+
"ProvisionedThroughput": {
76+
"ReadCapacityUnits": 5,
77+
"WriteCapacityUnits": 5
78+
},
79+
"ResourcePolicy": {
80+
"PolicyDocument": {
81+
"Statement": [
82+
{
83+
"Action": [
84+
"dynamodb:GetItem",
85+
"dynamodb:Query"
86+
],
87+
"Effect": "Allow",
88+
"Principal": {
89+
"AWS": {
90+
"Fn::Join": [
91+
"",
92+
[
93+
"arn:",
94+
{
95+
"Ref": "AWS::Partition"
96+
},
97+
":iam::",
98+
{
99+
"Ref": "AWS::AccountId"
100+
},
101+
":root"
102+
]
103+
]
104+
}
105+
},
106+
"Resource": {
107+
"Fn::Sub": "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/my-explicit-scoped-table"
108+
}
109+
}
110+
],
111+
"Version": "2012-10-17"
112+
}
113+
},
114+
"TableName": "my-explicit-scoped-table"
115+
},
116+
"UpdateReplacePolicy": "Delete",
117+
"DeletionPolicy": "Delete"
118+
}
119+
},
120+
"Parameters": {
121+
"BootstrapVersion": {
122+
"Type": "AWS::SSM::Parameter::Value<String>",
123+
"Default": "/cdk-bootstrap/hnb659fds/version",
124+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
125+
}
126+
},
127+
"Rules": {
128+
"CheckBootstrapVersion": {
129+
"Assertions": [
130+
{
131+
"Assert": {
132+
"Fn::Not": [
133+
{
134+
"Fn::Contains": [
135+
[
136+
"1",
137+
"2",
138+
"3",
139+
"4",
140+
"5"
141+
],
142+
{
143+
"Ref": "BootstrapVersion"
144+
}
145+
]
146+
}
147+
]
148+
},
149+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
150+
}
151+
]
152+
}
153+
}
154+
}

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.add-to-resource-policy.js.snapshot/addtoresourcepolicyintegtestDefaultTestDeployAssert0D97EAEA.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.add-to-resource-policy.js.snapshot/addtoresourcepolicyintegtestDefaultTestDeployAssert0D97EAEA.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.add-to-resource-policy.js.snapshot/cdk.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.add-to-resource-policy.js.snapshot/integ.json

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

Comments
 (0)