Skip to content

Commit 5dad62f

Browse files
authored
feat(bedrock-agentcore-alpha): update resources on grantInvokeXXX for runtime (#35864)
### Issue # (if applicable) ### Reason for this change The `grantInvoke`, `grantInvokeRuntime`, and `grantInvokeRuntimeForUser` methods in the BedrockAgentRuntime were only granting permissions to the runtime ARN itself, but not to its sub-resources. This caused permission issues when trying to invoke runtime endpoints from an AWS resource like lambda, as the actual invocation happens on sub-resources (e.g., arn:aws:bedrock-agentcore:region:account:runtime/runtime-id/*). ### Description of changes Updated the `resourceArns` parameter in three grant methods within `runtime-base.ts`: ### Describe any new or updated permissions being added The IAM permissions granted by these methods now include: - `bedrock-agentcore:InvokeAgentRuntime` on both the runtime ARN and its sub-resources - `bedrock-agentcore:InvokeAgentRuntimeForUser` on both the runtime ARN and its sub-resources ### Description of how you validated changes Manual and Integration 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 3a9fa54 commit 5dad62f

File tree

8 files changed

+581
-10
lines changed

8 files changed

+581
-10
lines changed

packages/@aws-cdk/aws-bedrock-agentcore-alpha/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,54 @@ runtime.connections.allowTo(databaseSecurityGroup, ec2.Port.tcp(5432), 'Allow Po
580580
runtime.connections.allowToAnyIpv4(ec2.Port.tcp(443), 'Allow HTTPS outbound');
581581
```
582582

583+
### Runtime IAM Permissions
584+
585+
The Runtime construct provides convenient methods for granting IAM permissions to principals that need to invoke the runtime or manage its execution role.
586+
587+
```typescript fixture=default
588+
const repository = new ecr.Repository(this, "TestRepository", {
589+
repositoryName: "test-agent-runtime",
590+
});
591+
const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
592+
593+
// Create a runtime
594+
const runtime = new agentcore.Runtime(this, "MyRuntime", {
595+
runtimeName: "my_runtime",
596+
agentRuntimeArtifact: agentRuntimeArtifact,
597+
});
598+
599+
// Create a Lambda function that needs to invoke the runtime
600+
const invokerFunction = new lambda.Function(this, "InvokerFunction", {
601+
runtime: lambda.Runtime.PYTHON_3_12,
602+
handler: "index.handler",
603+
code: lambda.Code.fromInline(`
604+
import boto3
605+
def handler(event, context):
606+
client = boto3.client('bedrock-agentcore')
607+
# Invoke the runtime...
608+
`),
609+
});
610+
611+
// Grant permission to invoke the runtime directly
612+
runtime.grantInvokeRuntime(invokerFunction);
613+
614+
// Grant permission to invoke the runtime on behalf of a user
615+
// (requires X-Amzn-Bedrock-AgentCore-Runtime-User-Id header)
616+
runtime.grantInvokeRuntimeForUser(invokerFunction);
617+
618+
// Grant both invoke permissions (most common use case)
619+
runtime.grantInvoke(invokerFunction);
620+
621+
// Grant specific custom permissions to the runtime's execution role
622+
runtime.grant(['bedrock:InvokeModel'], ['arn:aws:bedrock:*:*:*']);
623+
624+
// Add a policy statement to the runtime's execution role
625+
runtime.addToRolePolicy(new iam.PolicyStatement({
626+
actions: ['s3:GetObject'],
627+
resources: ['arn:aws:s3:::my-bucket/*'],
628+
}));
629+
```
630+
583631
### Other configuration
584632

585633
#### Lifecycle configuration

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
258258
return iam.Grant.addToPrincipal({
259259
grantee,
260260
actions: RUNTIME_INVOKE_PERMS,
261-
resourceArns: [this.agentRuntimeArn],
261+
resourceArns: [this.agentRuntimeArn, `${this.agentRuntimeArn}/*`], // * is needed because it invoke the endpoint as subresource
262262
});
263263
}
264264

@@ -272,7 +272,7 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
272272
return iam.Grant.addToPrincipal({
273273
grantee,
274274
actions: RUNTIME_INVOKE_USER_PERMS,
275-
resourceArns: [this.agentRuntimeArn],
275+
resourceArns: [this.agentRuntimeArn, `${this.agentRuntimeArn}/*`],
276276
});
277277
}
278278

@@ -285,7 +285,7 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
285285
return iam.Grant.addToPrincipal({
286286
grantee,
287287
actions: [...RUNTIME_INVOKE_PERMS, ...RUNTIME_INVOKE_USER_PERMS],
288-
resourceArns: [this.agentRuntimeArn],
288+
resourceArns: [this.agentRuntimeArn, `${this.agentRuntimeArn}/*`],
289289
});
290290
}
291291

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime.js.snapshot/aws-cdk-bedrock-agentcore-runtime.assets.json

Lines changed: 4 additions & 4 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.js.snapshot/aws-cdk-bedrock-agentcore-runtime.template.json

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,122 @@
310310
"Description": "Version 2 endpoint",
311311
"Name": "v2_endpoint"
312312
}
313+
},
314+
"TestInvokerFunctionServiceRole08233DAF": {
315+
"Type": "AWS::IAM::Role",
316+
"Properties": {
317+
"AssumeRolePolicyDocument": {
318+
"Statement": [
319+
{
320+
"Action": "sts:AssumeRole",
321+
"Effect": "Allow",
322+
"Principal": {
323+
"Service": "lambda.amazonaws.com"
324+
}
325+
}
326+
],
327+
"Version": "2012-10-17"
328+
},
329+
"ManagedPolicyArns": [
330+
{
331+
"Fn::Join": [
332+
"",
333+
[
334+
"arn:",
335+
{
336+
"Ref": "AWS::Partition"
337+
},
338+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
339+
]
340+
]
341+
}
342+
]
343+
}
344+
},
345+
"TestInvokerFunctionServiceRoleDefaultPolicyC6DC62B6": {
346+
"Type": "AWS::IAM::Policy",
347+
"Properties": {
348+
"PolicyDocument": {
349+
"Statement": [
350+
{
351+
"Action": [
352+
"bedrock-agentcore:InvokeAgentRuntime",
353+
"bedrock-agentcore:InvokeAgentRuntimeForUser"
354+
],
355+
"Effect": "Allow",
356+
"Resource": [
357+
{
358+
"Fn::GetAtt": [
359+
"TestRuntime65042BB5",
360+
"AgentRuntimeArn"
361+
]
362+
},
363+
{
364+
"Fn::Join": [
365+
"",
366+
[
367+
{
368+
"Fn::GetAtt": [
369+
"TestRuntime65042BB5",
370+
"AgentRuntimeArn"
371+
]
372+
},
373+
"/*"
374+
]
375+
]
376+
}
377+
]
378+
}
379+
],
380+
"Version": "2012-10-17"
381+
},
382+
"PolicyName": "TestInvokerFunctionServiceRoleDefaultPolicyC6DC62B6",
383+
"Roles": [
384+
{
385+
"Ref": "TestInvokerFunctionServiceRole08233DAF"
386+
}
387+
]
388+
}
389+
},
390+
"TestInvokerFunction6708F5AE": {
391+
"Type": "AWS::Lambda::Function",
392+
"Properties": {
393+
"Code": {
394+
"ZipFile": "def handler(event, context): return {\"statusCode\": 200}"
395+
},
396+
"Description": "Test function to verify runtime grant permissions with sub-resources",
397+
"Handler": "index.handler",
398+
"Role": {
399+
"Fn::GetAtt": [
400+
"TestInvokerFunctionServiceRole08233DAF",
401+
"Arn"
402+
]
403+
},
404+
"Runtime": "python3.12"
405+
},
406+
"DependsOn": [
407+
"TestInvokerFunctionServiceRoleDefaultPolicyC6DC62B6",
408+
"TestInvokerFunctionServiceRole08233DAF"
409+
]
410+
},
411+
"TestInvokerFunctionLogGroup003396AB": {
412+
"Type": "AWS::Logs::LogGroup",
413+
"Properties": {
414+
"LogGroupName": {
415+
"Fn::Join": [
416+
"",
417+
[
418+
"/aws/lambda/",
419+
{
420+
"Ref": "TestInvokerFunction6708F5AE"
421+
}
422+
]
423+
]
424+
},
425+
"RetentionInDays": 731
426+
},
427+
"UpdateReplacePolicy": "Retain",
428+
"DeletionPolicy": "Retain"
313429
}
314430
},
315431
"Outputs": {

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

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

0 commit comments

Comments
 (0)