Skip to content

Commit e72f80e

Browse files
committed
use addToPrincipalPolicy
1 parent d904fae commit e72f80e

File tree

6 files changed

+45
-251
lines changed

6 files changed

+45
-251
lines changed

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
219219
*/
220220
protected _connections: ec2.Connections | undefined;
221221

222-
/**
223-
* Counter for policies attached to imported roles
224-
* @internal
225-
*/
226-
private _policyCounter: number = 0;
227-
228222
constructor(scope: Construct, id: string) {
229223
super(scope, id);
230224
}
@@ -251,16 +245,7 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
251245
* @returns The runtime instance for chaining
252246
*/
253247
public addToRolePolicy(statement: iam.PolicyStatement): IBedrockAgentRuntime {
254-
// Check if role is a concrete Role instance
255-
if (this.role instanceof iam.Role) {
256-
this.role.addToPolicy(statement);
257-
} else {
258-
// For imported roles (IRole), we need to attach via a new policy
259-
const policy = new iam.Policy(this, `CustomPolicy${this._policyCounter++}`, {
260-
statements: [statement],
261-
});
262-
this.role.attachInlinePolicy(policy);
263-
}
248+
this.role.addToPrincipalPolicy(statement);
264249
return this;
265250
}
266251

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: 3 additions & 3 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.template.json

Lines changed: 14 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
"Properties": {
66
"PolicyDocument": {
77
"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+
},
818
{
919
"Action": [
1020
"ecr:BatchCheckLayerAvailability",
@@ -110,89 +120,10 @@
110120
"RoleArn": {
111121
"Fn::ImportValue": "pre-stack:ExportsOutputFnGetAttExecutionRole605A040BArnA891DEDE"
112122
}
113-
}
114-
},
115-
"TestRuntimeCustomPolicy0BD35B9F3": {
116-
"Type": "AWS::IAM::Policy",
117-
"Properties": {
118-
"PolicyDocument": {
119-
"Statement": [
120-
{
121-
"Action": "s3:GetObject",
122-
"Effect": "Allow",
123-
"Resource": "arn:aws:s3:::my-bucket/my-object"
124-
}
125-
],
126-
"Version": "2012-10-17"
127-
},
128-
"PolicyName": "TestRuntimeCustomPolicy0BD35B9F3",
129-
"Roles": [
130-
{
131-
"Fn::Select": [
132-
1,
133-
{
134-
"Fn::Split": [
135-
"/",
136-
{
137-
"Fn::Select": [
138-
5,
139-
{
140-
"Fn::Split": [
141-
":",
142-
{
143-
"Fn::ImportValue": "pre-stack:ExportsOutputFnGetAttExecutionRole605A040BArnA891DEDE"
144-
}
145-
]
146-
}
147-
]
148-
}
149-
]
150-
}
151-
]
152-
}
153-
]
154-
}
155-
},
156-
"TestRuntimeCustomPolicy16EAF0B5F": {
157-
"Type": "AWS::IAM::Policy",
158-
"Properties": {
159-
"PolicyDocument": {
160-
"Statement": [
161-
{
162-
"Action": "dynamodb:Query",
163-
"Effect": "Allow",
164-
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table"
165-
}
166-
],
167-
"Version": "2012-10-17"
168-
},
169-
"PolicyName": "TestRuntimeCustomPolicy16EAF0B5F",
170-
"Roles": [
171-
{
172-
"Fn::Select": [
173-
1,
174-
{
175-
"Fn::Split": [
176-
"/",
177-
{
178-
"Fn::Select": [
179-
5,
180-
{
181-
"Fn::Split": [
182-
":",
183-
{
184-
"Fn::ImportValue": "pre-stack:ExportsOutputFnGetAttExecutionRole605A040BArnA891DEDE"
185-
}
186-
]
187-
}
188-
]
189-
}
190-
]
191-
}
192-
]
193-
}
194-
]
195-
}
123+
},
124+
"DependsOn": [
125+
"ImportedRolePolicyawscdkbedrockagentcoreruntimewithimportedroleImportedRole261507D78EB91FCC"
126+
]
196127
}
197128
},
198129
"Parameters": {

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-imported-role.js.snapshot/manifest.json

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

0 commit comments

Comments
 (0)