Skip to content

Commit ee94b63

Browse files
authored
fix(agentcore): custom execution role policy for runtime lacks proper permissions (#35849)
### Issue # (if applicable) Closes #35852 . ### Reason for this change ECR permissions are attached even when the role is a custom role or an imported role. (https://github.com/aws/aws-cdk/blob/v2.221.0/packages/%40aws-cdk/aws-bedrock-agentcore-alpha/agentcore/runtime/runtime-artifact.ts#L65) However, the other required permissions are only granted to a policy for an auto-generated role. (https://github.com/aws/aws-cdk/blob/v2.221.0/packages/%40aws-cdk/aws-bedrock-agentcore-alpha/agentcore/runtime/runtime.ts#L252-L259) In constructs of other common modules, permissions are attached even when a custom role is passed. - https://github.com/aws/aws-cdk/blob/v2.221.0/packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts#L693 - https://github.com/aws/aws-cdk/blob/v2.221.0/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L1468 - https://github.com/aws/aws-cdk/blob/v2.221.0/packages/aws-cdk-lib/aws-ecs/lib/base/base-service.ts#L1161 So this PR adds the permissions to the custom role. FYI: If you avoid to add the permissions to the custom role, you can use `withoutPolicyUpdates()` method for Role. ### Description of changes Add the permissions to the custom role. ### 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 ebef303 commit ee94b63

18 files changed

+1741
-34
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,13 @@ export class Runtime extends RuntimeBase {
255255
this.validateRoleArn(props.executionRole.roleArn);
256256
}
257257
} else {
258-
this.role = this.createExecutionRole();
258+
this.role = new iam.Role(this, 'ExecutionRole', {
259+
assumedBy: new iam.ServicePrincipal('bedrock-agentcore.amazonaws.com'),
260+
description: 'Execution role for Bedrock Agent Core Runtime',
261+
maxSessionDuration: Duration.hours(8),
262+
});
259263
}
264+
this.addExecutionRolePermissions();
260265

261266
this.grantPrincipal = this.role;
262267
this.agentRuntimeArtifact = props.agentRuntimeArtifact;
@@ -320,53 +325,47 @@ export class Runtime extends RuntimeBase {
320325
}
321326

322327
/**
323-
* Creates an execution role for the agent runtime with proper permissions
328+
* Adds proper permissions to the execution role for the agent runtime
324329
* Based on: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-permissions.html
325330
*/
326-
private createExecutionRole(): iam.Role {
327-
const role = new iam.Role(this, 'ExecutionRole', {
328-
assumedBy: new iam.ServicePrincipal('bedrock-agentcore.amazonaws.com'),
329-
description: 'Execution role for Bedrock Agent Core Runtime',
330-
maxSessionDuration: Duration.hours(8),
331-
});
332-
331+
private addExecutionRolePermissions() {
333332
const region = Stack.of(this).region;
334333
const account = Stack.of(this).account;
335334

336335
// CloudWatch Logs - Log Group operations
337-
role.addToPolicy(new iam.PolicyStatement({
336+
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
338337
sid: 'LogGroupAccess',
339338
effect: iam.Effect.ALLOW,
340339
actions: RUNTIME_LOGS_GROUP_ACTIONS,
341340
resources: [`arn:${Stack.of(this).partition}:logs:${region}:${account}:log-group:/aws/bedrock-agentcore/runtimes/*`],
342341
}));
343342

344343
// CloudWatch Logs - Describe all log groups
345-
role.addToPolicy(new iam.PolicyStatement({
344+
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
346345
sid: 'DescribeLogGroups',
347346
effect: iam.Effect.ALLOW,
348347
actions: RUNTIME_LOGS_DESCRIBE_ACTIONS,
349348
resources: [`arn:${Stack.of(this).partition}:logs:${region}:${account}:log-group:*`],
350349
}));
351350

352351
// CloudWatch Logs - Log Stream operations
353-
role.addToPolicy(new iam.PolicyStatement({
352+
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
354353
sid: 'LogStreamAccess',
355354
effect: iam.Effect.ALLOW,
356355
actions: RUNTIME_LOGS_STREAM_ACTIONS,
357356
resources: [`arn:${Stack.of(this).partition}:logs:${region}:${account}:log-group:/aws/bedrock-agentcore/runtimes/*:log-stream:*`],
358357
}));
359358

360359
// X-Ray Tracing - must be * for tracing
361-
role.addToPolicy(new iam.PolicyStatement({
360+
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
362361
sid: 'XRayAccess',
363362
effect: iam.Effect.ALLOW,
364363
actions: RUNTIME_XRAY_ACTIONS,
365364
resources: ['*'],
366365
}));
367366

368367
// CloudWatch Metrics - scoped to bedrock-agentcore namespace
369-
role.addToPolicy(new iam.PolicyStatement({
368+
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
370369
sid: 'CloudWatchMetrics',
371370
effect: iam.Effect.ALLOW,
372371
actions: RUNTIME_CLOUDWATCH_METRICS_ACTIONS,
@@ -380,7 +379,7 @@ export class Runtime extends RuntimeBase {
380379

381380
// Bedrock AgentCore Workload Identity Access
382381
// Note: The agent name will be determined at runtime, so we use a wildcard pattern
383-
role.addToPolicy(new iam.PolicyStatement({
382+
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
384383
sid: 'GetAgentAccessToken',
385384
effect: iam.Effect.ALLOW,
386385
actions: RUNTIME_WORKLOAD_IDENTITY_ACTIONS,
@@ -389,7 +388,6 @@ export class Runtime extends RuntimeBase {
389388
`arn:${Stack.of(this).partition}:bedrock-agentcore:${region}:${account}:workload-identity-directory/default/workload-identity/*`,
390389
],
391390
}));
392-
return role;
393391
}
394392

395393
/**

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-with-custom-role.js.snapshot/BedrockAgentCoreRuntimeWithCustomRoleDefaultTestDeployAssert81CD6146.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-custom-role.js.snapshot/BedrockAgentCoreRuntimeWithCustomRoleDefaultTestDeployAssert81CD6146.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-custom-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-custom-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-custom-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-custom-role.js.snapshot/aws-cdk-bedrock-agentcore-runtime-with-custom-role.assets.json

Lines changed: 34 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)