Skip to content

Commit 92525e4

Browse files
authored
fix(agentcore): addToRolePolicy for runtime with imported role destroys and recreates policies on every deployment (#35842)
### Issue # (if applicable) Closes #35844 ### Reason for this change The current `addToRolePolicy` for Runtime with imported role destroys and recreates policies on every deployment. The reason is that `Date.now()` is used for a construct ID of a new Policy in the situation: https://github.com/aws/aws-cdk/blob/v2.221.0/packages/%40aws-cdk/aws-bedrock-agentcore-alpha/agentcore/runtime/runtime-base.ts#L253 ```ts public addToRolePolicy(statement: iam.PolicyStatement): IBedrockAgentRuntime { // Check if role is a concrete Role instance if (this.role instanceof iam.Role) { this.role.addToPolicy(statement); } else { // For imported roles (IRole), we need to attach via a new policy const policy = new iam.Policy(this, `CustomPolicy${Date.now()}`, { statements: [statement], }); ``` #### Reproduction 1. Deploy your stack with the following CDK code: ```ts const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-cdk-bedrock-agentcore-runtime-with-imported-role'); const runtimeArtifact = agentcore.AgentRuntimeArtifact.fromAsset( path.join(__dirname, 'testArtifact'), ); const role = new iam.Role(stack, 'ExecutionRole', { assumedBy: new iam.ServicePrincipal('bedrock-agentcore.amazonaws.com'), }); const imported = iam.Role.fromRoleArn(stack, 'ImportedRole', role.roleArn); const runtime = new agentcore.Runtime(stack, 'TestRuntime', { runtimeName: 'integ_test_runtime', agentRuntimeArtifact: runtimeArtifact, executionRole: imported, }); runtime.addToRolePolicy(new iam.PolicyStatement({ actions: ['dynamodb:Query'], resources: ['arn:aws:dynamodb:us-east-1:123456789012:table/my-table'], })); ``` 2. Deploy or diff the stack with the same CDK code again. 3. The change will occur: ``` [-] AWS::IAM::Policy TestRuntimeCustomPolicy1761380931769044921D2 destroy [+] AWS::IAM::Policy TestRuntimeCustomPolicy1761381522330E0DC0D40 ``` ### Description of changes Use `addToPrincipalPolicy` directly instead. ### Describe any new or updated permissions being added ### Description of how you validated changes Both unit tests and an integ test. ### 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 01d122c commit 92525e4

16 files changed

+1444
-18
lines changed

packages/@aws-cdk/aws-bedrock-agentcore-alpha/agentcore/runtime/runtime-base.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,7 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
245245
* @returns The runtime instance for chaining
246246
*/
247247
public addToRolePolicy(statement: iam.PolicyStatement): IBedrockAgentRuntime {
248-
// Check if role is a concrete Role instance
249-
if (this.role instanceof iam.Role) {
250-
this.role.addToPolicy(statement);
251-
} else {
252-
// For imported roles (IRole), we need to attach via a new policy
253-
const policy = new iam.Policy(this, `CustomPolicy${Date.now()}`, {
254-
statements: [statement],
255-
});
256-
this.role.attachInlinePolicy(policy);
257-
}
248+
this.role.addToPrincipalPolicy(statement);
258249
return this;
259250
}
260251

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/BedrockAgentCoreRuntimeWithImportedRoleDefaultTestDeployAssert640CC592.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/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/BedrockAgentCoreRuntimeWithImportedRoleDefaultTestDeployAssert640CC592.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/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/asset.f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240/Dockerfile

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/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/asset.f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240/app.py

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

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/asset.f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240/requirements.txt

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

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/aws-cdk-bedrock-agentcore-runtime-with-imported-role.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,163 @@
1+
{
2+
"Resources": {
3+
"ImportedRolePolicyawscdkbedrockagentcoreruntimewithimportedroleImportedRole261507D78EB91FCC": {
4+
"Type": "AWS::IAM::Policy",
5+
"Properties": {
6+
"PolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "s3:GetObject",
10+
"Effect": "Allow",
11+
"Resource": "arn:aws:s3:::my-bucket/my-object"
12+
},
13+
{
14+
"Action": "dynamodb:Query",
15+
"Effect": "Allow",
16+
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table"
17+
},
18+
{
19+
"Action": [
20+
"ecr:BatchCheckLayerAvailability",
21+
"ecr:BatchGetImage",
22+
"ecr:GetDownloadUrlForLayer"
23+
],
24+
"Effect": "Allow",
25+
"Resource": {
26+
"Fn::Join": [
27+
"",
28+
[
29+
"arn:",
30+
{
31+
"Ref": "AWS::Partition"
32+
},
33+
":ecr:",
34+
{
35+
"Ref": "AWS::Region"
36+
},
37+
":",
38+
{
39+
"Ref": "AWS::AccountId"
40+
},
41+
":repository/",
42+
{
43+
"Fn::Sub": "cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}"
44+
}
45+
]
46+
]
47+
}
48+
},
49+
{
50+
"Action": "ecr:GetAuthorizationToken",
51+
"Effect": "Allow",
52+
"Resource": "*"
53+
}
54+
],
55+
"Version": "2012-10-17"
56+
},
57+
"PolicyName": "PolicyawscdkbedrockagentcoreruntimewithimportedroleImportedRole261507D7",
58+
"Roles": [
59+
{
60+
"Fn::Select": [
61+
1,
62+
{
63+
"Fn::Split": [
64+
"/",
65+
{
66+
"Fn::Select": [
67+
5,
68+
{
69+
"Fn::Split": [
70+
":",
71+
{
72+
"Fn::ImportValue": "pre-stack:ExportsOutputFnGetAttExecutionRole605A040BArnA891DEDE"
73+
}
74+
]
75+
}
76+
]
77+
}
78+
]
79+
}
80+
]
81+
}
82+
]
83+
}
84+
},
85+
"TestRuntime65042BB5": {
86+
"Type": "AWS::BedrockAgentCore::Runtime",
87+
"Properties": {
88+
"AgentRuntimeArtifact": {
89+
"ContainerConfiguration": {
90+
"ContainerUri": {
91+
"Fn::Join": [
92+
"",
93+
[
94+
{
95+
"Ref": "AWS::AccountId"
96+
},
97+
".dkr.ecr.",
98+
{
99+
"Ref": "AWS::Region"
100+
},
101+
".",
102+
{
103+
"Ref": "AWS::URLSuffix"
104+
},
105+
"/",
106+
{
107+
"Fn::Sub": "cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}"
108+
},
109+
":f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240"
110+
]
111+
]
112+
}
113+
}
114+
},
115+
"AgentRuntimeName": "integ_test_runtime",
116+
"NetworkConfiguration": {
117+
"NetworkMode": "PUBLIC"
118+
},
119+
"ProtocolConfiguration": "HTTP",
120+
"RoleArn": {
121+
"Fn::ImportValue": "pre-stack:ExportsOutputFnGetAttExecutionRole605A040BArnA891DEDE"
122+
}
123+
},
124+
"DependsOn": [
125+
"ImportedRolePolicyawscdkbedrockagentcoreruntimewithimportedroleImportedRole261507D78EB91FCC"
126+
]
127+
}
128+
},
129+
"Parameters": {
130+
"BootstrapVersion": {
131+
"Type": "AWS::SSM::Parameter::Value<String>",
132+
"Default": "/cdk-bootstrap/hnb659fds/version",
133+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
134+
}
135+
},
136+
"Rules": {
137+
"CheckBootstrapVersion": {
138+
"Assertions": [
139+
{
140+
"Assert": {
141+
"Fn::Not": [
142+
{
143+
"Fn::Contains": [
144+
[
145+
"1",
146+
"2",
147+
"3",
148+
"4",
149+
"5"
150+
],
151+
{
152+
"Ref": "BootstrapVersion"
153+
}
154+
]
155+
}
156+
]
157+
},
158+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
159+
}
160+
]
161+
}
162+
}
163+
}

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.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/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.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)