> **Experimental**
>
> `@aws-cdk/aws-bedrock-alpha` is an [**experimental**](/stability) (alpha) package: its API may change in any release, without a major version bump.
# Guardrail

Class to create a Guardrail with CDK.

Extends: Construct › Resource › GuardrailBase › Guardrail
Implements: IGuardrail, IResource, IEnvironmentAware, IConstruct, IDependable

- Stability: [Experimental](/cdk/api/stability.html)
- CloudFormation: [`AWS::Bedrock::Guardrail`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-bedrock-guardrail.html)
- Source: [`bedrock/guardrails/guardrails.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0-alpha.0/packages/@aws-cdk/aws-bedrock-alpha/bedrock/guardrails/guardrails.ts#L418)

## Example

```ts
const guardrail = new bedrock.Guardrail(this, 'bedrockGuardrails', {
  guardrailName: 'my-BedrockGuardrails',
});
// Add regex filter with input/output actions
guardrail.addRegexFilter({
  name: 'TestRegexFilter',
  pattern: 'test-pattern',
  action: bedrock.GuardrailAction.ANONYMIZE,
  // below props are optional
  description: 'This is a test regex filter',
  inputAction: bedrock.GuardrailAction.BLOCK,
  inputEnabled: true,
  outputAction: bedrock.GuardrailAction.ANONYMIZE,
  outputEnabled: true,
});
```

## Initializer

```ts
new Guardrail(scope: Construct, id: string, props: GuardrailProps)
```

## Construct Props

- `blockedInputMessaging`: string — The message to return when the guardrail blocks a prompt.

  **Default:** "Sorry, your query violates our usage policy."

  Must be between 1 and 500 characters.

- `blockedOutputsMessaging`: string — The message to return when the guardrail blocks a model response.

  **Default:** "Sorry, I am unable to answer your question because of our usage policy."

  Must be between 1 and 500 characters.

- `contentFilters`: [ContentFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.ContentFilter.html)[] = [] — The content filters to apply to the guardrail.

- `contentFiltersTierConfig`: [TierConfig](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.TierConfig.html) = filters.TierConfig.CLASSIC — The tier configuration to apply to the guardrail.

- `contextualGroundingFilters`: [ContextualGroundingFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.ContextualGroundingFilter.html)[] = [] — The contextual grounding filters to apply to the guardrail.

- `crossRegionConfig`: [GuardrailCrossRegionConfigProperty](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.GuardrailCrossRegionConfigProperty.html) — The cross-region configuration for the guardrail.

  **Default:** No cross-region configuration

  This is optional and when provided, it should be of type GuardrailCrossRegionConfigProperty.

- `deniedTopics`: [Topic](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.Topic.html)[] = [] — A list of policies related to topics that the guardrail should deny.

- `description`: string — The description of the guardrail.

  **Default:** No description

- `guardrailName`: string (required) — The name of the guardrail.

  This will be used as the physical name of the guardrail.

- `kmsKey`: [IKey](/cdk/api/v2/docs/aws-cdk-lib.aws_kms.IKey.html) — A custom KMS key to use for encrypting data.

  **Default:** Data is encrypted by default with a key that AWS owns and manages for you

- `managedWordListFilters`: [ManagedWordFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.ManagedWordFilter.html)[] = [] — The managed word filters to apply to the guardrail.

- `piiFilters`: [PIIFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.PIIFilter.html)[] = [] — The PII filters to apply to the guardrail.

- `regexFilters`: [RegexFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.RegexFilter.html)[] = [] — The regular expression (regex) filters to apply to the guardrail.

- `topicsTierConfig`: [TierConfig](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.TierConfig.html) = filters.TierConfig.CLASSIC — The tier configuration to apply to the guardrail.

- `wordFilters`: [WordFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.WordFilter.html)[] = [] — The word filters to apply to the guardrail.

## Properties

- `blockedInputMessaging`: string — The message to return when the guardrail blocks a prompt.

  **Default:** "Sorry, your query violates our usage policy."

- `blockedOutputsMessaging`: string — The message to return when the guardrail blocks a model response.

  **Default:** "Sorry, I am unable to answer your question because of our usage policy."

- `contentFilters`: [ContentFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.ContentFilter.html)[] — The content filters applied by the guardrail.

- `contentFiltersTierConfig`: [TierConfig](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.TierConfig.html) = filters.TierConfig.CLASSIC — The tier that your guardrail uses for content filters.

  Consider using a tier that balances performance, accuracy, and compatibility with your existing generative AI workflows.

- `contextualGroundingFilters`: [ContextualGroundingFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.ContextualGroundingFilter.html)[] — The contextual grounding filters applied by the guardrail.

- `crossRegionConfig`: [GuardrailCrossRegionConfigProperty](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.GuardrailCrossRegionConfigProperty.html) — The cross-region configuration for the guardrail.

- `deniedTopics`: [Topic](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.Topic.html)[] — The denied topic filters applied by the guardrail.

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `guardrailArn`: string — The ARN of the guardrail.

- `guardrailId`: string — The ID of the guardrail.

- `guardrailVersion`: string — The version of the guardrail.

- `hash`: string — The computed hash of the guardrail properties.

- `kmsKey`: [IKey](/cdk/api/v2/docs/aws-cdk-lib.aws_kms.IKey.html) — The KMS key used to encrypt data.

  **Default:** undefined - "Data is encrypted by default with a key that AWS owns and manages for you"

- `lastUpdated`: string — When this guardrail was last updated.

- `managedWordListFilters`: [ManagedWordFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.ManagedWordFilter.html)[] — The managed word list filters applied by the guardrail.

- `name`: string — The name of the guardrail.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `piiFilters`: [PIIFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.PIIFilter.html)[] — The PII filters applied by the guardrail.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `regexFilters`: [RegexFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.RegexFilter.html)[] — The regex filters applied by the guardrail.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

- `topicsTierConfig`: [TierConfig](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.TierConfig.html) = filters.TierConfig.CLASSIC — The tier that your guardrail uses for denied topic filters.

- `wordFilters`: [WordFilter](/cdk/api/v2/docs/@aws-cdk_aws-bedrock-alpha.WordFilter.html)[] — The word filters applied by the guardrail.

## Methods

- `addContentFilter(filter: ContentFilter): void` — Adds a content filter to the guardrail.

  **Parameters**

  - `filter` — The content filter to add.

- `addContextualGroundingFilter(filter: ContextualGroundingFilter): void` — Adds a contextual grounding filter to the guardrail.

  **Parameters**

  - `filter` — The contextual grounding filter to add.

- `addDeniedTopicFilter(filter: Topic): void` — Adds a denied topic filter to the guardrail.

  **Parameters**

  - `filter` — The denied topic filter to add.

- `addManagedWordListFilter(filter: ManagedWordFilter): void` — Adds a managed word list filter to the guardrail.

  **Parameters**

  - `filter` — The managed word list filter to add.

- `addPIIFilter(filter: PIIFilter): void` — Adds a PII filter to the guardrail.

  **Parameters**

  - `filter` — The PII filter to add.

- `addRegexFilter(filter: RegexFilter): void` — Adds a regex filter to the guardrail.

  **Parameters**

  - `filter` — The regex filter to add.

- `addWordFilter(filter: WordFilter): void` — Adds a word filter to the guardrail.

  **Parameters**

  - `filter` — The word filter to add.

- `addWordFilterFromFile(filePath: string, inputAction?: GuardrailAction, outputAction?: GuardrailAction, inputEnabled?: boolean, outputEnabled?: boolean): void` — Adds a word filter to the guardrail.

  **Parameters**

  - `filePath` — The location of the word filter file.

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `createVersion(description?: string): string` — Create a version for the guardrail.

  **Parameters**

  - `description` — The description of the version.

  **Returns:** The guardrail version.

- `fromCfnGuardrail(cfnGuardrail: CfnGuardrail): IGuardrail` (static) — Import a low-level L1 Cfn Guardrail.

- `fromGuardrailAttributes(scope: Construct, id: string, attrs: GuardrailAttributes): IGuardrail` (static) — Import a guardrail given its attributes.

- `grant(grantee: IGrantable, ...actions: string[]): Grant` — Grant the given principal identity permissions to perform actions on this guardrail.

  [disable-awslint:no-grants]

- `grantApply(grantee: IGrantable): Grant` — Grant the given identity permissions to apply the guardrail.

  [disable-awslint:no-grants]

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `metric(metricName: string, props?: MetricOptions): Metric` — Return the given named metric for this guardrail.

  By default, the metric will be calculated as a sum over a period of 5 minutes.
  You can customize this by using the `statistic` and `period` properties.

- `metricAll(metricName: string, props?: MetricOptions): Metric` (static) — Return the given named metric for all guardrails.

  By default, the metric will be calculated as a sum over a period of 5 minutes.
  You can customize this by using the `statistic` and `period` properties.

- `metricAllInvocationLatency(props?: MetricOptions): Metric` (static) — Return the invocation latency metric for all guardrails.

- `metricAllInvocations(props?: MetricOptions): Metric` (static) — Return the invocations metric for all guardrails.

- `metricAllInvocationsIntervened(props?: MetricOptions): Metric` (static) — Return the invocations intervened metric for all guardrails.

- `metricAllTextUnitCount(props?: MetricOptions): Metric` (static) — Return the text unit count metric for all guardrails.

- `metricInvocationClientErrors(props?: MetricOptions): Metric` — Return the invocation client errors metric for this guardrail.

- `metricInvocationLatency(props?: MetricOptions): Metric` — Return the invocation latency metric for this guardrail.

- `metricInvocations(props?: MetricOptions): Metric` — Return the invocations metric for this guardrail.

- `metricInvocationServerErrors(props?: MetricOptions): Metric` — Return the invocation server errors metric for this guardrail.

- `metricInvocationsIntervened(props?: MetricOptions): Metric` — Return the invocations intervened metric for this guardrail.

- `metricInvocationThrottles(props?: MetricOptions): Metric` — Return the invocation throttles metric for this guardrail.

- `metricTextUnitCount(props?: MetricOptions): Metric` — Return the text unit count metric for this guardrail.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
