

# Esempi di API per le regole del gateway
<a name="gateway-rules-examples"></a>

Gli esempi seguenti mostrano come gestire le regole del gateway utilizzando la AWS CLI e AWS Python SDK (Boto3).

## Crea una regola con un pacchetto di configurazione statico che sostituisce
<a name="gateway-rules-create-static"></a>

L'esempio seguente crea una regola che associa un principale IAM specifico a una versione del pacchetto di configurazione. Utilizza questo approccio per il controllo qualità o il debug.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Crea una regola predefinita generica
<a name="gateway-rules-create-catchall"></a>

L'esempio seguente crea una regola senza condizioni. Questa regola corrisponde a tutto il traffico e funge da impostazione predefinita. Assegna un numero ad alta priorità in modo che regole più specifiche abbiano la precedenza.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Crea una regola con una suddivisione ponderata del pacchetto di configurazione
<a name="gateway-rules-create-weighted"></a>

L'esempio seguente crea una regola che suddivide il traffico tra due versioni del pacchetto di configurazione a scopo di test. A/B In questo esempio, l'80% del traffico utilizza la variante A e il 20% la variante B.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

**Nota**  
Per mantenere un'esperienza utente coerente durante i A/B test, includi l'`X-Amzn-Bedrock-AgentCore-Runtime-Session-Id`header nelle richieste di invoke del gateway. Per ulteriori informazioni, consulta [Persistenza della sessione per regole ponderate](gateway-rules-session-stickiness.md).

## Crea una regola con target routing
<a name="gateway-rules-create-target-route"></a>

L'esempio seguente crea una regola che indirizza le richieste che corrispondono a uno schema di percorso specifico verso una destinazione.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Crea una regola con un routing ponderato per obiettivi
<a name="gateway-rules-create-weighted-target"></a>

L'esempio seguente crea una regola che divide il traffico tra due destinazioni. In questo esempio, il 90% delle rotte di traffico verso l'obiettivo principale e il 10% verso l'obiettivo delle isole Canarie.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Crea una regola con condizioni e azioni combinate
<a name="gateway-rules-create-combined"></a>

L'esempio seguente crea una regola con entrambi i tipi di condizioni e entrambi i tipi di azione. La richiesta deve corrispondere ad almeno una voce per ogni tipo di condizione (logica AND tra i tipi).

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

**Nota**  
Questa regola corrisponde solo quando il chiamante è il ruolo QA E il percorso della richiesta corrisponde`/my-target-canary/*`. Entrambe le condizioni devono essere soddisfatte.

## Ottieni una regola di accesso
<a name="gateway-rules-get"></a>

L'esempio seguente recupera i dettagli di una regola gateway specifica.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Aggiornare una regola del gateway
<a name="gateway-rules-update"></a>

L'esempio seguente aggiorna la priorità e la descrizione di una regola gateway esistente.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Elenca le regole del gateway
<a name="gateway-rules-list"></a>

L'esempio seguente elenca tutte le regole per un gateway. I risultati vengono ordinati per priorità in ordine crescente.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```

## Eliminare una regola gateway
<a name="gateway-rules-delete"></a>

L'esempio seguente elimina una regola gateway.

**Example**  

1. Esegui il comando seguente:

   ```
   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']}")
   ```