Skip to content

Commit b39ccf3

Browse files
fix(bedrock-alpha): apply permission dependency to existing and non-existing roles (#35123)
### Issue # (if applicable) Closes #35120 ### Reason for this change Dependency on permission was only applied to new roles, not to existing roles. ### Description of changes The dependency on permission now applies to both new and existing roles. ### Describe any new or updated permissions being added Not applicable. ### Description of how you validated changes I've added an integration test for an agent with an existing/custom role attached to it. ### 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 1d96d6b commit b39ccf3

File tree

11 files changed

+979
-5
lines changed

11 files changed

+979
-5
lines changed

packages/@aws-cdk/aws-bedrock-alpha/bedrock/agents/agent.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,8 @@ export class Agent extends AgentBase implements IAgent {
575575

576576
// Add explicit dependency between the agent resource and the agent's role default policy
577577
// See https://github.com/awslabs/generative-ai-cdk-constructs/issues/899
578-
if (!props.existingRole) {
579-
// add the appropriate permissions to use the FM
580-
const grant = this.foundationModel.grantInvoke(this.role);
581-
grant.applyBefore(this.__resource);
582-
}
578+
const grant = this.foundationModel.grantInvoke(this.role);
579+
grant.applyBefore(this.__resource);
583580

584581
this.testAlias = AgentAlias.fromAttributes(this, 'DefaultAlias', {
585582
aliasId: 'TSTALIASID',

packages/@aws-cdk/aws-bedrock-alpha/test/bedrock/agents/agent.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,35 @@ describe('Agent', () => {
769769
});
770770
});
771771

772+
test('applies dependency for existing role', () => {
773+
const existingRole = new iam.Role(stack, 'ExistingRole', {
774+
assumedBy: new iam.ServicePrincipal('bedrock.amazonaws.com'),
775+
});
776+
777+
new bedrock.Agent(stack, 'Agent', {
778+
instruction: 'This is a test instruction that must be at least 40 characters long to be valid',
779+
foundationModel: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V2_0,
780+
existingRole,
781+
});
782+
783+
// Verify CloudFormation template has DependsOn
784+
Template.fromStack(stack).hasResource('AWS::Bedrock::Agent', {
785+
DependsOn: [Match.stringLikeRegexp('ExistingRoleDefaultPolicy.*')],
786+
});
787+
});
788+
789+
test('applies dependency for role created by Agent', () => {
790+
new bedrock.Agent(stack, 'Agent', {
791+
instruction: 'This is a test instruction that must be at least 40 characters long to be valid',
792+
foundationModel: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V2_0,
793+
});
794+
795+
// Verify CloudFormation template has DependsOn
796+
Template.fromStack(stack).hasResource('AWS::Bedrock::Agent', {
797+
DependsOn: [Match.stringLikeRegexp('AgentRoleDefaultPolicy.*')],
798+
});
799+
});
800+
772801
test('creates agent with guardrail', () => {
773802
const guardrail = new bedrock.Guardrail(stack, 'TestGuardrail', {
774803
guardrailName: 'TestGuardrail',

packages/@aws-cdk/aws-bedrock-alpha/test/bedrock/agents/integ.agent-existing-role.js.snapshot/BedrockAgentExistingRoleDefaultTestDeployAssertD5C55EAB.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-alpha/test/bedrock/agents/integ.agent-existing-role.js.snapshot/BedrockAgentExistingRoleDefaultTestDeployAssertD5C55EAB.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-alpha/test/bedrock/agents/integ.agent-existing-role.js.snapshot/aws-cdk-bedrock-agent-existing-role-1.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,144 @@
1+
{
2+
"Resources": {
3+
"AgentRoleF4A42D6B": {
4+
"Type": "AWS::IAM::Role",
5+
"Properties": {
6+
"AssumeRolePolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "bedrock.amazonaws.com"
13+
}
14+
}
15+
],
16+
"Version": "2012-10-17"
17+
}
18+
}
19+
},
20+
"AgentRoleDefaultPolicy1027F5D2": {
21+
"Type": "AWS::IAM::Policy",
22+
"Properties": {
23+
"PolicyDocument": {
24+
"Statement": [
25+
{
26+
"Action": [
27+
"bedrock:GetFoundationModel",
28+
"bedrock:InvokeModel*"
29+
],
30+
"Effect": "Allow",
31+
"Resource": {
32+
"Fn::Join": [
33+
"",
34+
[
35+
"arn:",
36+
{
37+
"Ref": "AWS::Partition"
38+
},
39+
":bedrock:",
40+
{
41+
"Ref": "AWS::Region"
42+
},
43+
"::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0"
44+
]
45+
]
46+
}
47+
}
48+
],
49+
"Version": "2012-10-17"
50+
},
51+
"PolicyName": "AgentRoleDefaultPolicy1027F5D2",
52+
"Roles": [
53+
{
54+
"Ref": "AgentRoleF4A42D6B"
55+
}
56+
]
57+
}
58+
},
59+
"Agent255F68E9": {
60+
"Type": "AWS::Bedrock::Agent",
61+
"Properties": {
62+
"ActionGroups": [
63+
{
64+
"ActionGroupName": "UserInputAction",
65+
"ActionGroupState": "DISABLED",
66+
"ParentActionGroupSignature": "AMAZON.UserInput",
67+
"SkipResourceInUseCheckOnDelete": false
68+
},
69+
{
70+
"ActionGroupName": "CodeInterpreterAction",
71+
"ActionGroupState": "DISABLED",
72+
"ParentActionGroupSignature": "AMAZON.CodeInterpreter",
73+
"SkipResourceInUseCheckOnDelete": false
74+
}
75+
],
76+
"AgentName": "agent-awscdkbedrockagentistingrole1-agent-70062d50-bedrockagent",
77+
"AgentResourceRoleArn": {
78+
"Fn::GetAtt": [
79+
"AgentRoleF4A42D6B",
80+
"Arn"
81+
]
82+
},
83+
"AutoPrepare": false,
84+
"FoundationModel": {
85+
"Fn::Join": [
86+
"",
87+
[
88+
"arn:",
89+
{
90+
"Ref": "AWS::Partition"
91+
},
92+
":bedrock:",
93+
{
94+
"Ref": "AWS::Region"
95+
},
96+
"::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0"
97+
]
98+
]
99+
},
100+
"IdleSessionTTLInSeconds": 600,
101+
"Instruction": "This is a test agent that uses an existing IAM role.",
102+
"OrchestrationType": "DEFAULT",
103+
"SkipResourceInUseCheckOnDelete": true
104+
},
105+
"DependsOn": [
106+
"AgentRoleDefaultPolicy1027F5D2"
107+
]
108+
}
109+
},
110+
"Parameters": {
111+
"BootstrapVersion": {
112+
"Type": "AWS::SSM::Parameter::Value<String>",
113+
"Default": "/cdk-bootstrap/hnb659fds/version",
114+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
115+
}
116+
},
117+
"Rules": {
118+
"CheckBootstrapVersion": {
119+
"Assertions": [
120+
{
121+
"Assert": {
122+
"Fn::Not": [
123+
{
124+
"Fn::Contains": [
125+
[
126+
"1",
127+
"2",
128+
"3",
129+
"4",
130+
"5"
131+
],
132+
{
133+
"Ref": "BootstrapVersion"
134+
}
135+
]
136+
}
137+
]
138+
},
139+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
140+
}
141+
]
142+
}
143+
}
144+
}

packages/@aws-cdk/aws-bedrock-alpha/test/bedrock/agents/integ.agent-existing-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-alpha/test/bedrock/agents/integ.agent-existing-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)