Skip to content

Commit a8d4373

Browse files
dineshSajwanDinesh Sajwangithub-actionskrokokomergify[bot]
authored
feat(bedrockconstruct): added support for IAM existingRole (#454)
* feat(constructprop): added existinRole to bedrock agent and kb * feat(constructprop): added existinRole to bedrock agent and kb * chore: self mutation Signed-off-by: github-actions <[email protected]> * chore: self mutation Signed-off-by: github-actions <[email protected]> --------- Signed-off-by: github-actions <[email protected]> Signed-off-by: Dinesh Sajwan <[email protected]> Co-authored-by: Dinesh Sajwan <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Alain Krok <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 8225896 commit a8d4373

File tree

4 files changed

+112
-66
lines changed

4 files changed

+112
-66
lines changed

apidocs/interfaces/bedrock.AgentProps.md

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Properties for a Bedrock Agent.
1515
- [description](bedrock.AgentProps.md#description)
1616
- [enableUserInput](bedrock.AgentProps.md#enableuserinput)
1717
- [encryptionKey](bedrock.AgentProps.md#encryptionkey)
18+
- [existingRole](bedrock.AgentProps.md#existingrole)
1819
- [foundationModel](bedrock.AgentProps.md#foundationmodel)
1920
- [idleSessionTTL](bedrock.AgentProps.md#idlesessionttl)
2021
- [instruction](bedrock.AgentProps.md#instruction)
@@ -98,6 +99,15 @@ KMS encryption key to use for the agent.
9899

99100
___
100101

102+
### existingRole
103+
104+
`Optional` `Readonly` **existingRole**: `Role`
105+
106+
The existing IAM Role for the agent with a trust policy that
107+
allows the Bedrock service to assume the role.
108+
109+
___
110+
101111
### foundationModel
102112

103113
`Readonly` **foundationModel**: [`BedrockFoundationModel`](../classes/bedrock.BedrockFoundationModel.md)

apidocs/interfaces/bedrock.KnowledgeBaseProps.md

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Properties for a knowledge base
1212

1313
- [description](bedrock.KnowledgeBaseProps.md#description)
1414
- [embeddingsModel](bedrock.KnowledgeBaseProps.md#embeddingsmodel)
15+
- [existingRole](bedrock.KnowledgeBaseProps.md#existingrole)
1516
- [indexName](bedrock.KnowledgeBaseProps.md#indexname)
1617
- [instruction](bedrock.KnowledgeBaseProps.md#instruction)
1718
- [knowledgeBaseState](bedrock.KnowledgeBaseProps.md#knowledgebasestate)
@@ -45,6 +46,18 @@ The embeddings model for the knowledge base
4546

4647
___
4748

49+
### existingRole
50+
51+
`Optional` `Readonly` **existingRole**: `Role`
52+
53+
Existing IAM role with a policy statement
54+
granting permission to invoke the specific embeddings model.
55+
Any entity (e.g., an AWS service or application) that assumes
56+
this role will be able to invoke or use the
57+
specified embeddings model within the Bedrock service.
58+
59+
___
60+
4861
### indexName
4962

5063
`Optional` `Readonly` **indexName**: `string`

src/cdk-lib/bedrock/agent.ts

+45-35
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ export interface AgentProps {
212212
* @default - A name is automatically generated.
213213
*/
214214
readonly name?: string;
215+
216+
/**
217+
* The existing IAM Role for the agent with a trust policy that
218+
* allows the Bedrock service to assume the role.
219+
*/
220+
readonly existingRole?: iam.Role;
221+
215222
/**
216223
* A narrative instruction to provide the agent as context.
217224
*/
@@ -376,44 +383,47 @@ export class Agent extends Construct {
376383
'bedrock-agent',
377384
{ maxLength: 32, lower: true, separator: '-' });
378385

379-
this.role = new iam.Role(this, 'Role', {
380-
assumedBy: new iam.ServicePrincipal('bedrock.amazonaws.com'),
381-
roleName: generatePhysicalNameV2(
382-
this,
383-
'AmazonBedrockExecutionRoleForAgents_',
384-
{ maxLength: 64, lower: false }),
385-
});
386-
387-
this.role.assumeRolePolicy!.addStatements(
388-
new iam.PolicyStatement({
389-
actions: ['sts:AssumeRole'],
390-
principals: [new iam.ServicePrincipal('bedrock.amazonaws.com')],
391-
conditions: {
392-
StringEquals: {
393-
'aws:SourceAccount': cdk.Stack.of(this).account,
394-
},
395-
ArnLike: {
396-
'aws:SourceArn': cdk.Stack.of(this).formatArn({
397-
service: 'bedrock',
398-
resource: 'agent',
399-
resourceName: '*',
400-
arnFormat: cdk.ArnFormat.SLASH_RESOURCE_NAME,
401-
}),
402-
},
403-
},
404-
}),
405-
);
386+
if (props.existingRole) {
387+
this.role = props.existingRole;
388+
} else {
389+
this.role = new iam.Role(this, 'Role', {
390+
assumedBy: new iam.ServicePrincipal('bedrock.amazonaws.com'),
391+
roleName: generatePhysicalNameV2(
392+
this,
393+
'AmazonBedrockExecutionRoleForAgents_',
394+
{ maxLength: 64, lower: false }),
395+
});
406396

407-
new iam.Policy(this, 'AgentFMPolicy', {
408-
roles: [this.role],
409-
statements: [
397+
this.role.assumeRolePolicy!.addStatements(
410398
new iam.PolicyStatement({
411-
actions: ['bedrock:InvokeModel'],
412-
resources: [props.foundationModel.asArn(this)],
399+
actions: ['sts:AssumeRole'],
400+
principals: [new iam.ServicePrincipal('bedrock.amazonaws.com')],
401+
conditions: {
402+
StringEquals: {
403+
'aws:SourceAccount': cdk.Stack.of(this).account,
404+
},
405+
ArnLike: {
406+
'aws:SourceArn': cdk.Stack.of(this).formatArn({
407+
service: 'bedrock',
408+
resource: 'agent',
409+
resourceName: '*',
410+
arnFormat: cdk.ArnFormat.SLASH_RESOURCE_NAME,
411+
}),
412+
},
413+
},
413414
}),
414-
],
415-
});
416-
415+
);
416+
417+
new iam.Policy(this, 'AgentFMPolicy', {
418+
roles: [this.role],
419+
statements: [
420+
new iam.PolicyStatement({
421+
actions: ['bedrock:InvokeModel'],
422+
resources: [props.foundationModel.asArn(this)],
423+
}),
424+
],
425+
});
426+
}
417427

418428
const agent = new bedrock.CfnAgent(this, 'Agent', {
419429

src/cdk-lib/bedrock/knowledge-base.ts

+44-31
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ export interface KnowledgeBaseProps {
111111
*/
112112
readonly description?: string;
113113

114+
/**
115+
* Existing IAM role with a policy statement
116+
* granting permission to invoke the specific embeddings model.
117+
* Any entity (e.g., an AWS service or application) that assumes
118+
* this role will be able to invoke or use the
119+
* specified embeddings model within the Bedrock service.
120+
*/
121+
readonly existingRole?: iam.Role;
122+
114123
/**
115124
* A narrative description of the knowledge base.
116125
*
@@ -261,39 +270,43 @@ export class KnowledgeBase extends Construct {
261270
'KB',
262271
{ maxLength: 32 });
263272

264-
const roleName = generatePhysicalNameV2(
265-
this,
266-
'AmazonBedrockExecutionRoleForKnowledgeBase',
267-
{ maxLength: 64 });
268-
this.role = new iam.Role(this, 'Role', {
269-
roleName: roleName,
270-
assumedBy: new iam.ServicePrincipal('bedrock.amazonaws.com'),
271-
});
272-
this.role.assumeRolePolicy!.addStatements(
273-
new iam.PolicyStatement({
274-
actions: ['sts:AssumeRole'],
275-
principals: [new iam.ServicePrincipal('bedrock.amazonaws.com')],
276-
conditions: {
277-
StringEquals: {
278-
'aws:SourceAccount': cdk.Stack.of(this).account,
279-
},
280-
ArnLike: {
281-
'aws:SourceArn': cdk.Stack.of(this).formatArn({
282-
service: 'bedrock',
283-
resource: 'knowledge-base',
284-
resourceName: '*',
285-
arnFormat: cdk.ArnFormat.SLASH_RESOURCE_NAME,
286-
}),
287-
},
288-
},
289-
}),
290-
);
291273

292-
this.role.addToPolicy(new iam.PolicyStatement({
293-
actions: ['bedrock:InvokeModel'],
294-
resources: [embeddingsModel.asArn(this)],
295-
}));
274+
if (props.existingRole) {
275+
this.role = props.existingRole;
276+
} else {
277+
const roleName = generatePhysicalNameV2(
278+
this,
279+
'AmazonBedrockExecutionRoleForKnowledgeBase',
280+
{ maxLength: 64 });
281+
this.role = new iam.Role(this, 'Role', {
282+
roleName: roleName,
283+
assumedBy: new iam.ServicePrincipal('bedrock.amazonaws.com'),
284+
});
285+
this.role.assumeRolePolicy!.addStatements(
286+
new iam.PolicyStatement({
287+
actions: ['sts:AssumeRole'],
288+
principals: [new iam.ServicePrincipal('bedrock.amazonaws.com')],
289+
conditions: {
290+
StringEquals: {
291+
'aws:SourceAccount': cdk.Stack.of(this).account,
292+
},
293+
ArnLike: {
294+
'aws:SourceArn': cdk.Stack.of(this).formatArn({
295+
service: 'bedrock',
296+
resource: 'knowledge-base',
297+
resourceName: '*',
298+
arnFormat: cdk.ArnFormat.SLASH_RESOURCE_NAME,
299+
}),
300+
},
301+
},
302+
}),
303+
);
296304

305+
this.role.addToPolicy(new iam.PolicyStatement({
306+
actions: ['bedrock:InvokeModel'],
307+
resources: [embeddingsModel.asArn(this)],
308+
}));
309+
}
297310
/**
298311
* Create the vector store if the vector store was provided by the user.
299312
* Otherwise check againts all possible vector datastores.

0 commit comments

Comments
 (0)