Skip to content

Commit b4fdb2b

Browse files
authored
feat(bedrock-alpha): add guardrails l2 construct (#34765)
### Issue # (if applicable) Closes #<issue number here>. ### Reason for this change Adding new feature ### Description of changes Add to the bedrock alpha construct support for bedrock guardrails through a new L2 construct ### Describe any new or updated permissions being added ### Description of how you validated changes ### 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 8d09069 commit b4fdb2b

26 files changed

+8070
-3
lines changed

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

Lines changed: 540 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AgentCollaborator } from './agent-collaborator';
1616
import { AgentCollaboration } from './agent-collaboration';
1717
import { PromptOverrideConfiguration } from './prompt-override';
1818
import { AssetApiSchema, S3ApiSchema } from './api-schema';
19+
import { IGuardrail } from '../guardrails/guardrails';
1920
import * as validation from './validation-helpers';
2021
import { IBedrockInvokable } from '.././models';
2122
import { Memory } from './memory';
@@ -178,7 +179,6 @@ export abstract class AgentBase extends Resource implements IAgent {
178179
/**
179180
* Properties for creating a CDK managed Bedrock Agent.
180181
* TODO: Knowledge bases configuration will be added in a future update
181-
* TODO: Guardrails configuration will be added in a future update
182182
* TODO: Inference profile configuration will be added in a future update
183183
*
184184
*/
@@ -241,7 +241,11 @@ export interface AgentProps {
241241
* @default - Only default action groups (UserInput and CodeInterpreter) are added
242242
*/
243243
readonly actionGroups?: AgentActionGroup[];
244-
244+
/**
245+
* The guardrail that will be associated with the agent.
246+
* @default - No guardrail is provided.
247+
*/
248+
readonly guardrail?: IGuardrail;
245249
/**
246250
* Overrides some prompt templates in different parts of an agent sequence configuration.
247251
*
@@ -408,7 +412,10 @@ export class Agent extends AgentBase implements IAgent {
408412
* action groups associated with the ageny
409413
*/
410414
public readonly actionGroups: AgentActionGroup[] = [];
411-
415+
/**
416+
* The guardrail that will be associated with the agent.
417+
*/
418+
public guardrail?: IGuardrail;
412419
// ------------------------------------------------------
413420
// CDK-only attributes
414421
// ------------------------------------------------------
@@ -519,6 +526,10 @@ export class Agent extends AgentBase implements IAgent {
519526
});
520527
}
521528

529+
if (props.guardrail) {
530+
this.addGuardrail(props.guardrail);
531+
}
532+
522533
// Grant permissions for custom orchestration if provided
523534
if (this.customOrchestrationExecutor?.lambdaFunction) {
524535
this.customOrchestrationExecutor.lambdaFunction.grantInvoke(this.role);
@@ -540,6 +551,7 @@ export class Agent extends AgentBase implements IAgent {
540551
customerEncryptionKeyArn: props.kmsKey?.keyArn,
541552
description: props.description,
542553
foundationModel: this.foundationModel.invokableArn,
554+
guardrailConfiguration: Lazy.any({ produce: () => this.renderGuardrail() }),
543555
idleSessionTtlInSeconds: this.idleSessionTTL.toSeconds(),
544556
instruction: props.instruction,
545557
memoryConfiguration: props.memory?._render(),
@@ -581,6 +593,19 @@ export class Agent extends AgentBase implements IAgent {
581593
// HELPER METHODS - addX()
582594
// ------------------------------------------------------
583595

596+
/**
597+
* Add guardrail to the agent.
598+
*/
599+
@MethodMetadata()
600+
public addGuardrail(guardrail: IGuardrail) {
601+
// Do some checks
602+
validation.throwIfInvalid(this.validateGuardrail, guardrail);
603+
// Add it to the construct
604+
this.guardrail = guardrail;
605+
// Handle permissions
606+
guardrail.grantApply(this.role);
607+
}
608+
584609
/**
585610
* Adds an action group to the agent and configures necessary permissions.
586611
*
@@ -662,6 +687,20 @@ export class Agent extends AgentBase implements IAgent {
662687
// Lazy Renderers
663688
// ------------------------------------------------------
664689

690+
/**
691+
* Render the guardrail configuration.
692+
*
693+
* @internal This is an internal core function and should not be called directly.
694+
*/
695+
private renderGuardrail(): bedrock.CfnAgent.GuardrailConfigurationProperty | undefined {
696+
return this.guardrail
697+
? {
698+
guardrailIdentifier: this.guardrail.guardrailId,
699+
guardrailVersion: this.guardrail.guardrailVersion,
700+
}
701+
: undefined;
702+
}
703+
665704
/**
666705
* Render the action groups
667706
*
@@ -715,6 +754,24 @@ export class Agent extends AgentBase implements IAgent {
715754
// ------------------------------------------------------
716755
// Validators
717756
// ------------------------------------------------------
757+
/**
758+
* Checks if the Guardrail is valid
759+
*
760+
* @param guardrail - The guardrail to validate
761+
* @returns Array of validation error messages, empty if valid
762+
*/
763+
private validateGuardrail = (guardrail: IGuardrail) => {
764+
let errors: string[] = [];
765+
if (this.guardrail) {
766+
errors.push(
767+
`Cannot add Guardrail ${guardrail.guardrailId}. ` +
768+
`Guardrail ${this.guardrail.guardrailId} has already been specified for this agent.`,
769+
);
770+
}
771+
errors.push(...validation.validateFieldPattern(guardrail.guardrailVersion, 'version', /^(([0-9]{1,8})|(DRAFT))$/));
772+
return errors;
773+
};
774+
718775
/**
719776
* Check if the action group is valid
720777
*

0 commit comments

Comments
 (0)