---
title: "Tutorial: Enforce Amazon Bedrock Guardrails with a custom Python policy"
description: Enforce an Amazon Bedrock guardrail on AI model services, model provider services, and MCP services with a custom Python service policy and a Unity Catalog service credential.
canonical_url: https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/tutorial-bedrock-guardrails
---

# Tutorial: Enforce Amazon Bedrock Guardrails with a custom Python policy

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md). Workspace admins can control access to this feature from the **Previews** page by opting into the **Unity Gateway Service Policies** preview. See [ Manage workspace-level previews](https://docs.databricks.com/aws/en/admin/workspace-settings/manage-previews.md#workspace).

This tutorial shows how to enforce a guardrail you configured in [Amazon Bedrock Guardrails](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html) on traffic through Unity Gateway. A [custom Python service policy](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md) sends each message to the Bedrock `ApplyGuardrail` API and blocks the interaction when Bedrock intervenes.

Bedrock authenticates with AWS Identity and Access Management (IAM), so the policy reaches it through a Unity Catalog service credential that assumes an IAM role in your AWS account.

In this tutorial, you:

1. Create an IAM role and a Unity Catalog service credential that can call your Bedrock guardrail.
2. Create the Python policy function.
3. Attach the policy to a service and test it.

## <a id="requirements" />Requirements

* The workspace that sends requests to the service is in a region that supports custom Python service policies. See [Availability](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md#availability).
* The **Unity Gateway Custom Python Service Policies** preview and the **Unity Gateway Service Policies** preview are turned on for your workspace. A workspace admin turns them on from the **Previews** page. See [Before you begin](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md#before-you-begin).
* The workspace that sends requests to the service doesn't use the compliance security profile, a serverless network policy that restricts internet access or blocks specific destinations, a private network gateway, or serverless private endpoints. See [Before you begin](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md#before-you-begin).
* A guardrail in Amazon Bedrock with a published version, and its guardrail ID and version number. See [Create a guardrail](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-create.html) in the AWS documentation. Set the guardrail's actions to **Block**. A Python policy can't redact content, so a guardrail that masks sensitive information denies the whole interaction instead.
* Permission in your AWS account to create an IAM role and policy.
* The `CREATE SERVICE CREDENTIAL` privilege on the metastore, or a metastore admin who can create the service credential for you.
* `USE CATALOG`, `USE SCHEMA`, and `CREATE FUNCTION` on the catalog and schema where you create the function. This tutorial uses `main.governance`.
* `MANAGE` on the Model Service, Model Provider Service, or MCP Service you want to govern.

## <a id="step-1-create-a-service-credential-for-bedrock" />Step 1: Create a service credential for Bedrock

1. Follow [Create a service credential that references an AWS IAM role](https://docs.databricks.com/aws/en/connect/unity-catalog/cloud-services/service-credentials.md#create-a-service-credential-that-references-an-aws-iam-role). Name the credential `bedrock-guardrail`.

2. In the IAM role's permissions policy, allow the role to apply your guardrail. Replace the Region, account ID, and guardrail ID with your values:

   ```json
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
         "Action": ["bedrock:ApplyGuardrail", "bedrock:GetGuardrail"],
         "Resource": "arn:aws:bedrock:<region>:<account-id>:guardrail/<guardrail-id>"
       }
     ]
   }
   ```

3. If someone else creates the function in Step 2, grant them `ACCESS` on the credential:

   ```sql
   GRANT ACCESS ON SERVICE CREDENTIAL `bedrock-guardrail` TO `<user-or-group>`;
   ```

## <a id="step-2-create-the-policy-function" />Step 2: Create the policy function

Run the following SQL in a notebook or the SQL editor. Replace the Region, guardrail ID, and version with your guardrail's values. Use a published version, so the guardrail doesn't change while the policy uses it.

On a Model Service or Model Provider Service, the function sends the latest message to Bedrock as `INPUT` on the request phase and as `OUTPUT` on the response phase. On an MCP Service, it sends the tool name and arguments as `INPUT` before the tool runs. It doesn't check MCP tool results. It allows the interaction when Bedrock doesn't intervene. When Bedrock intervenes, it denies the interaction and names the denied topics, content filters, and sensitive information types that blocked it.

The function fails closed. It denies the interaction when the event or Bedrock's result is unexpected, or when the call to Bedrock fails. It waits up to 3 seconds to connect and 5 seconds for a response, and doesn't retry, so a slow guardrail call fails quickly instead of holding up the request.

```sql
CREATE OR REPLACE FUNCTION main.governance.bedrock_guardrail(
  event VARIANT,
  config VARIANT
)
RETURNS VARIANT
LANGUAGE PYTHON
CREDENTIALS (`bedrock-guardrail` DEFAULT)
ENVIRONMENT (environment_version = '6')
AS $$
import json
import boto3
from botocore.config import Config
from databricks.service_credentials import getServiceCredentialsProvider

REGION = "<region>"                        # for example, "us-east-1"
GUARDRAIL_ID = "<guardrail-id>"
GUARDRAIL_VERSION = "<guardrail-version>"  # a published version, for example "1"

def policy_decision(result, reason=""):
    return type(event).parseJson(json.dumps({"decision": {"result": result, "reason": reason}}))

def blocked_policies(response):
    names = set()
    for assessment in response.get("assessments", []):
        for topic in assessment.get("topicPolicy", {}).get("topics", []):
            if topic.get("action") == "BLOCKED" and topic.get("name"):
                names.add(topic["name"])
        for content_filter in assessment.get("contentPolicy", {}).get("filters", []):
            if content_filter.get("action") == "BLOCKED" and content_filter.get("type"):
                names.add(content_filter["type"])
        for pii in assessment.get("sensitiveInformationPolicy", {}).get("piiEntities", []):
            if pii.get("action") == "BLOCKED" and pii.get("type"):
                names.add(pii["type"])
    return ", ".join(sorted(names))

event_value = event.toPython() if hasattr(event, "toPython") else event
if not isinstance(event_value, dict):
    return policy_decision("DENY", "The policy received an invalid event.")
phase = event_value.get("type")
source = {"request": "INPUT", "response": "OUTPUT"}.get(phase)
if source is None:
    return policy_decision("DENY", "The policy received an unexpected event type.")

context = event_value.get("context") or {}
tool = context.get("tool")
if tool:
    # MCP Service: check the tool name and arguments before the tool runs.
    if phase != "request":
        return policy_decision("ALLOW")
    text = f"{tool.get('name', '')} {json.dumps(tool.get('arguments', {}))}"
else:
    # Model Service or Model Provider Service: check the latest message.
    text = str(context.get("message", ""))
if not text.strip():
    return policy_decision("ALLOW")

try:
    session = boto3.Session(botocore_session=getServiceCredentialsProvider("bedrock-guardrail"))
    client = session.client(
        "bedrock-runtime",
        region_name=REGION,
        config=Config(connect_timeout=3, read_timeout=5, retries={"total_max_attempts": 1}),
    )
    response = client.apply_guardrail(
        guardrailIdentifier=GUARDRAIL_ID,
        guardrailVersion=GUARDRAIL_VERSION,
        source=source,
        content=[{"text": {"text": text}}],
    )
except Exception as error:
    return policy_decision("DENY", f"Could not reach the Bedrock guardrail: {type(error).__name__}.")

action = response.get("action")
if action == "NONE":
    return policy_decision("ALLOW")
if action != "GUARDRAIL_INTERVENED":
    return policy_decision("DENY", "The Bedrock guardrail returned an unexpected result.")
blocked = blocked_policies(response)
return policy_decision("DENY", f"Blocked by the Bedrock guardrail ({blocked})." if blocked else "Blocked by the Bedrock guardrail.")
$$;
```

When the call fails, the reason names only the error type. The reason is returned to the caller, and an AWS error message can include your account ID and IAM role name.

The function must set `environment_version = '6'` because it declares a service credential. Without it, the function is created, but the policy fails with a `CREDENTIALS_UNSUPPORTED_ENVIRONMENT` error when it runs.

You can also create the function in the policy form instead of with SQL. In Step 3, under **Function**, select **Create new** and **Python**. Enter the function name. Expand **Secrets and credentials (optional)** and select `bedrock-guardrail` in **Default credential (optional)**. Then paste the function body, the code between the `$$` markers, into the editor.

## <a id="step-3-attach-and-test-the-policy" />Step 3: Attach and test the policy

1. In the workspace sidebar, click **Unity Gateway**.
2. Select the service to govern: a model service on the **Models** tab, a model provider service on the **Providers** tab, or an MCP service on the **MCPs** tab.
3. Open the **Policies** tab, then click **New policy**.
4. Enter a **Name** for the policy, such as `bedrock_guardrail`.
5. In **Guardrail type**, select **Custom**.
6. Set the **Rank** to control evaluation order. The lowest rank runs first on the request and last on the response.
7. Under **Implementation**, select **Custom function**.
8. Under **Function**, select **Select existing**, then select `main.governance.bedrock_guardrail`. In **Compute for creating the function and testing**, select a SQL warehouse.
9. Expand **Test function**. Under `event`, insert a sample event with text your guardrail should block. On a model service or model provider service, click **Request** and replace its message. On an MCP service, select a tool in **Select tool** and set one of its arguments. Click **Run test**, and confirm that **Returns** shows `deny` with a reason that names the Bedrock policy that intervened.
10. (Optional) Expand **Advanced options** and set **Mode** to **Log** to record decisions without blocking while you review them.
11. Click **Create policy**.

The first time the policy runs, the function's environment starts, and the policy can deny interactions until it's ready. See [Python policy performance](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md#performance). To check that the policy is ready, send a request to the service that your guardrail should block, for example from [AI Playground](https://docs.databricks.com/aws/en/large-language-models/ai-playground.md), and retry until the policy returns its verdict. In **Enforce** mode, Databricks blocks it and returns a successful (HTTP 200) response. On a model service or model provider service, the `databricks_service_policy` object carries the reason. On an MCP service, the response is a JSON-RPC error with the reason in `error.message`. See [Policy decisions](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/index.md#policy-decisions).

> [!NOTE]
>
> **Test function** runs on the SQL warehouse you select. To call Bedrock from a test on a serverless SQL warehouse, enable the **Enable networking for isolated workloads in Serverless SQL Warehouses** preview on your workspace's [Previews page](https://docs.databricks.com/aws/en/admin/workspace-settings/manage-previews.md). Live policy evaluation doesn't use the warehouse.

## <a id="performance-considerations" />Performance considerations

Each evaluation calls Bedrock, so the policy adds Bedrock's latency to the Python policy's own [evaluation time](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md#performance), on every request and response it checks. Bedrock also charges for each `ApplyGuardrail` call. To limit latency and cost:

* Attach the policy only to the services that need it.
* Create the guardrail in the AWS Region closest to your workspace. A call to another Region adds network latency.
* To check only one direction, return `ALLOW` early for the other phase. For example, to check only requests, return `ALLOW` when `phase` is `response`.

## <a id="clean-up" />Clean up

1. Delete the policy from the service's **Policies** tab.

2. Drop the function:

   ```sql
   DROP FUNCTION IF EXISTS main.governance.bedrock_guardrail;
   ```

3. If you no longer need it, delete the `bedrock-guardrail` service credential and the IAM role.

## <a id="additional-resources" />Additional resources

* [Write a custom Python service policy](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/python-policies.md)
* [Service policy function reference](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/policy-function-reference.md)
* [Use Unity Catalog service credentials to connect to external cloud services](https://docs.databricks.com/aws/en/connect/unity-catalog/cloud-services/use-service-credentials.md)
