

# 閘道規則 API 範例
<a name="gateway-rules-examples"></a>

下列範例示範如何使用 CLI 和 AWS Python SDK (Boto3) AWS 管理閘道規則。

## 建立具有靜態組態套件覆寫的規則
<a name="gateway-rules-create-static"></a>

下列範例會建立將特定 IAM 主體繫結至組態套件版本的規則。使用此方法來進行 QA 或偵錯。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control create-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --priority 100 \
       --description "Pin QA role to bundle version" \
       --conditions '[
           {
               "matchPrincipals": {
                   "anyOf": [
                       {
                           "iamPrincipal": {
                               "arn": "arn:aws:iam::123456789012:role/QARole",
                               "operator": "StringEquals"
                           }
                       }
                   ]
               }
           }
       ]' \
       --actions '[
           {
               "configurationBundle": {
                   "staticOverride": {
                       "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                       "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab"
                   }
               }
           }
       ]'
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.create_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       priority=100,
       description="Pin QA role to bundle version",
       conditions=[
           {
               "matchPrincipals": {
                   "anyOf": [
                       {
                           "iamPrincipal": {
                               "arn": "arn:aws:iam::123456789012:role/QARole",
                               "operator": "StringEquals",
                           }
                       }
                   ]
               }
           }
       ],
       actions=[
           {
               "configurationBundle": {
                   "staticOverride": {
                       "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                       "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab",
                   }
               }
           }
       ],
   )
   
   print(f"Rule ID: {response['ruleId']}")
   ```

## 建立全部擷取的預設規則
<a name="gateway-rules-create-catchall"></a>

下列範例會建立沒有條件的規則。此規則符合所有流量，並做為預設值。指派高優先順序號碼，讓更具體的規則優先。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control create-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --priority 1000000 \
       --description "Default configuration bundle for all traffic" \
       --actions '[
           {
               "configurationBundle": {
                   "staticOverride": {
                       "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                       "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab"
                   }
               }
           }
       ]'
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.create_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       priority=1000000,
       description="Default configuration bundle for all traffic",
       actions=[
           {
               "configurationBundle": {
                   "staticOverride": {
                       "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                       "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab",
                   }
               }
           }
       ],
   )
   
   print(f"Rule ID: {response['ruleId']}")
   ```

## 建立具有加權組態套件分割的規則
<a name="gateway-rules-create-weighted"></a>

下列範例會建立規則，以分割兩個組態套件版本之間的流量，以進行 A/B 測試。在此範例中，80% 的流量使用變體 A，20% 使用變體 B。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control create-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --priority 500 \
       --description "A/B test: 80/20 traffic split" \
       --actions '[
           {
               "configurationBundle": {
                   "weightedOverride": {
                       "trafficSplit": [
                           {
                               "name": "variant-a",
                               "weight": 80,
                               "configurationBundle": {
                                   "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                                   "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab"
                               },
                               "description": "Control variant"
                           },
                           {
                               "name": "variant-b",
                               "weight": 20,
                               "configurationBundle": {
                                   "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                                   "bundleVersion": "12345678-1234-5678-9abc-123456789012"
                               },
                               "description": "Treatment variant"
                           }
                       ]
                   }
               }
           }
       ]'
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.create_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       priority=500,
       description="A/B test: 80/20 traffic split",
       actions=[
           {
               "configurationBundle": {
                   "weightedOverride": {
                       "trafficSplit": [
                           {
                               "name": "variant-a",
                               "weight": 80,
                               "configurationBundle": {
                                   "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                                   "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab",
                               },
                               "description": "Control variant",
                           },
                           {
                               "name": "variant-b",
                               "weight": 20,
                               "configurationBundle": {
                                   "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                                   "bundleVersion": "12345678-1234-5678-9abc-123456789012",
                               },
                               "description": "Treatment variant",
                           },
                       ]
                   }
               }
           }
       ],
   )
   
   print(f"Rule ID: {response['ruleId']}")
   ```

**注意**  
若要在 A/B 測試期間維持一致的使用者體驗，請在閘道調用請求中包含 `X-Amzn-Bedrock-AgentCore-Runtime-Session-Id`標頭。如需詳細資訊，請參閱[加權規則的工作階段黏性](gateway-rules-session-stickiness.md)。

## 建立具有目標路由的規則
<a name="gateway-rules-create-target-route"></a>

下列範例會建立規則，將符合特定路徑模式的請求路由至目標。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control create-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --priority 150 \
       --description "Route /my-target-canary requests to canary target" \
       --conditions '[
           {
               "matchPaths": {
                   "anyOf": [
                       "/my-target-canary/*"
                   ]
               }
           }
       ]' \
       --actions '[
           {
               "routeToTarget": {
                   "staticRoute": {
                       "targetName": "my-target-canary"
                   }
               }
           }
       ]'
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.create_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       priority=150,
       description="Route /my-target-canary requests to canary target",
       conditions=[
           {
               "matchPaths": {
                   "anyOf": ["/my-target-canary/*"]
               }
           }
       ],
       actions=[
           {
               "routeToTarget": {
                   "staticRoute": {
                       "targetName": "my-target-canary",
                   }
               }
           }
       ],
   )
   
   print(f"Rule ID: {response['ruleId']}")
   ```

## 建立具有加權目標路由的規則
<a name="gateway-rules-create-weighted-target"></a>

下列範例會建立在兩個目標之間分割流量的規則。在此範例中，90% 的流量路由至主要目標，10% 的流量路由至 Canary 目標。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control create-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --priority 300 \
       --description "Canary deployment: 90/10 target split" \
       --actions '[
           {
               "routeToTarget": {
                   "weightedRoute": {
                       "trafficSplit": [
                           {
                               "name": "primary",
                               "weight": 90,
                               "targetName": "my-target-primary",
                               "description": "Primary target"
                           },
                           {
                               "name": "canary",
                               "weight": 10,
                               "targetName": "my-target-canary",
                               "description": "Canary target"
                           }
                       ]
                   }
               }
           }
       ]'
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.create_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       priority=300,
       description="Canary deployment: 90/10 target split",
       actions=[
           {
               "routeToTarget": {
                   "weightedRoute": {
                       "trafficSplit": [
                           {
                               "name": "primary",
                               "weight": 90,
                               "targetName": "my-target-primary",
                               "description": "Primary target",
                           },
                           {
                               "name": "canary",
                               "weight": 10,
                               "targetName": "my-target-canary",
                               "description": "Canary target",
                           },
                       ]
                   }
               }
           }
       ],
   )
   
   print(f"Rule ID: {response['ruleId']}")
   ```

## 建立具有合併條件和動作的規則
<a name="gateway-rules-create-combined"></a>

下列範例會建立同時具有條件類型和動作類型的規則。請求必須至少符合每個條件類型的一個項目 （跨類型AND 邏輯）。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control create-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --priority 50 \
       --description "QA role on /my-target-canary paths: pin bundle and route to canary" \
       --conditions '[
           {
               "matchPrincipals": {
                   "anyOf": [
                       {
                           "iamPrincipal": {
                               "arn": "arn:aws:iam::123456789012:role/QARole",
                               "operator": "StringEquals"
                           }
                       }
                   ]
               }
           },
           {
               "matchPaths": {
                   "anyOf": [
                       "/my-target-canary/*"
                   ]
               }
           }
       ]' \
       --actions '[
           {
               "configurationBundle": {
                   "staticOverride": {
                       "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                       "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab"
                   }
               }
           },
           {
               "routeToTarget": {
                   "staticRoute": {
                       "targetName": "my-target-canary"
                   }
               }
           }
       ]'
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.create_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       priority=50,
       description="QA role on /my-target-canary paths: pin bundle and route to canary",
       conditions=[
           {
               "matchPrincipals": {
                   "anyOf": [
                       {
                           "iamPrincipal": {
                               "arn": "arn:aws:iam::123456789012:role/QARole",
                               "operator": "StringEquals",
                           }
                       }
                   ]
               }
           },
           {
               "matchPaths": {
                   "anyOf": ["/my-target-canary/*"]
               },
           },
       ],
       actions=[
           {
               "configurationBundle": {
                   "staticOverride": {
                       "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-abc1234567",
                       "bundleVersion": "1234abcd-12ab-34cd-56ef-1234567890ab",
                   }
               }
           },
           {
               "routeToTarget": {
                   "staticRoute": {
                       "targetName": "my-target-canary",
                   }
               }
           },
       ],
   )
   
   print(f"Rule ID: {response['ruleId']}")
   ```

**注意**  
此規則只有在發起人是 QA 角色且請求路徑符合 時才會相符`/my-target-canary/*`。必須滿足這兩個條件。

## 取得閘道規則
<a name="gateway-rules-get"></a>

下列範例會擷取特定閘道規則的詳細資訊。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control get-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --rule-id 12345678-1234-1234-1234-123456789012
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.get_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       ruleId="12345678-1234-1234-1234-123456789012",
   )
   
   print(f"Priority: {response['priority']}")
   print(f"Status: {response['status']}")
   print(f"Actions: {response['actions']}")
   ```

## 更新閘道規則
<a name="gateway-rules-update"></a>

下列範例會更新現有閘道規則的優先順序和描述。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control update-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --rule-id 12345678-1234-1234-1234-123456789012 \
       --priority 200 \
       --description "Updated priority for QA rule"
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.update_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       ruleId="12345678-1234-1234-1234-123456789012",
       priority=200,
       description="Updated priority for QA rule",
   )
   
   print(f"Status: {response['status']}")
   ```

## 列出閘道規則
<a name="gateway-rules-list"></a>

下列範例列出閘道的所有規則。結果會依優先順序遞增排序。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control list-gateway-rules \
       --gateway-identifier my-gateway-abc1234567
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.list_gateway_rules(
       gatewayIdentifier="my-gateway-abc1234567"
   )
   
   for rule in response["gatewayRules"]:
       print(f"Rule: {rule['ruleId']}, Priority: {rule['priority']}, Status: {rule['status']}")
   ```

## 刪除閘道規則
<a name="gateway-rules-delete"></a>

下列範例會刪除閘道規則。

**Example**  

1. 執行以下命令：

   ```
   aws bedrock-agentcore-control delete-gateway-rule \
       --gateway-identifier my-gateway-abc1234567 \
       --rule-id 12345678-1234-1234-1234-123456789012
   ```

1. 

   ```
   import boto3
   
   client = boto3.client("bedrock-agentcore-control", region_name="us-west-2")
   
   response = client.delete_gateway_rule(
       gatewayIdentifier="my-gateway-abc1234567",
       ruleId="12345678-1234-1234-1234-123456789012",
   )
   
   print(f"Status: {response['status']}")
   ```