Skip to content
48 changes: 48 additions & 0 deletions packages/@aws-cdk/aws-bedrock-agentcore-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,54 @@ runtime.connections.allowTo(databaseSecurityGroup, ec2.Port.tcp(5432), 'Allow Po
runtime.connections.allowToAnyIpv4(ec2.Port.tcp(443), 'Allow HTTPS outbound');
```

### Runtime IAM Permissions

The Runtime construct provides convenient methods for granting IAM permissions to principals that need to invoke the runtime or manage its execution role.

```typescript fixture=default
const repository = new ecr.Repository(this, "TestRepository", {
repositoryName: "test-agent-runtime",
});
const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");

// Create a runtime
const runtime = new agentcore.Runtime(this, "MyRuntime", {
runtimeName: "my_runtime",
agentRuntimeArtifact: agentRuntimeArtifact,
});

// Create a Lambda function that needs to invoke the runtime
const invokerFunction = new lambda.Function(this, "InvokerFunction", {
runtime: lambda.Runtime.PYTHON_3_12,
handler: "index.handler",
code: lambda.Code.fromInline(`
import boto3
def handler(event, context):
client = boto3.client('bedrock-agentcore')
# Invoke the runtime...
`),
});

// Grant permission to invoke the runtime directly
runtime.grantInvokeRuntime(invokerFunction);

// Grant permission to invoke the runtime on behalf of a user
// (requires X-Amzn-Bedrock-AgentCore-Runtime-User-Id header)
runtime.grantInvokeRuntimeForUser(invokerFunction);

// Grant both invoke permissions (most common use case)
runtime.grantInvoke(invokerFunction);

// Grant specific custom permissions to the runtime's execution role
runtime.grant(['bedrock:InvokeModel'], ['arn:aws:bedrock:*:*:*']);

// Add a policy statement to the runtime's execution role
runtime.addToRolePolicy(new iam.PolicyStatement({
actions: ['s3:GetObject'],
resources: ['arn:aws:s3:::my-bucket/*'],
}));
```

### Other configuration

#### Lifecycle configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export abstract class RuntimeBase extends Resource implements IBedrockAgentRunti
return iam.Grant.addToPrincipal({
grantee,
actions: RUNTIME_INVOKE_PERMS,
resourceArns: [this.agentRuntimeArn],
resourceArns: [this.agentRuntimeArn, `${this.agentRuntimeArn}/*`], // * is needed because it invoke the endpoint as subresource
});
}

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

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

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,122 @@
"Description": "Version 2 endpoint",
"Name": "v2_endpoint"
}
},
"TestInvokerFunctionServiceRole08233DAF": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"TestInvokerFunctionServiceRoleDefaultPolicyC6DC62B6": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"bedrock-agentcore:InvokeAgentRuntime",
"bedrock-agentcore:InvokeAgentRuntimeForUser"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"TestRuntime65042BB5",
"AgentRuntimeArn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"TestRuntime65042BB5",
"AgentRuntimeArn"
]
},
"/*"
]
]
}
]
}
],
"Version": "2012-10-17"
},
"PolicyName": "TestInvokerFunctionServiceRoleDefaultPolicyC6DC62B6",
"Roles": [
{
"Ref": "TestInvokerFunctionServiceRole08233DAF"
}
]
}
},
"TestInvokerFunction6708F5AE": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "def handler(event, context): return {\"statusCode\": 200}"
},
"Description": "Test function to verify runtime grant permissions with sub-resources",
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"TestInvokerFunctionServiceRole08233DAF",
"Arn"
]
},
"Runtime": "python3.12"
},
"DependsOn": [
"TestInvokerFunctionServiceRoleDefaultPolicyC6DC62B6",
"TestInvokerFunctionServiceRole08233DAF"
]
},
"TestInvokerFunctionLogGroup003396AB": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": {
"Fn::Join": [
"",
[
"/aws/lambda/",
{
"Ref": "TestInvokerFunction6708F5AE"
}
]
]
},
"RetentionInDays": 731
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
}
},
"Outputs": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading