

# Exemples d'API de règles de passerelle
<a name="gateway-rules-examples"></a>

Les exemples suivants montrent comment gérer les règles de passerelle à l'aide de la AWS CLI et du SDK AWS Python (Boto3).

## Création d'une règle avec remplacement d'un bundle de configuration statique
<a name="gateway-rules-create-static"></a>

L'exemple suivant crée une règle qui associe un principal IAM spécifique à une version de bundle de configuration. Utilisez cette approche pour l'assurance qualité ou le débogage.

**Example**  

1. Exécutez la commande suivante :

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

## Création d'une règle fourre-tout par défaut
<a name="gateway-rules-create-catchall"></a>

L'exemple suivant crée une règle sans conditions. Cette règle correspond à l'ensemble du trafic et sert de règle par défaut. Attribuez un numéro de priorité élevé afin que les règles plus spécifiques aient la priorité.

**Example**  

1. Exécutez la commande suivante :

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

## Création d'une règle avec répartition pondérée des ensembles de configuration
<a name="gateway-rules-create-weighted"></a>

L'exemple suivant crée une règle qui répartit le trafic entre deux versions de bundle de configuration à des fins de A/B test. Dans cet exemple, 80 % du trafic utilise la variante A et 20 % utilise la variante B.

**Example**  

1. Exécutez la commande suivante :

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

**Note**  
Pour garantir une expérience utilisateur cohérente pendant les A/B tests, incluez l'`X-Amzn-Bedrock-AgentCore-Runtime-Session-Id`en-tête dans les demandes d'appel de votre passerelle. Pour de plus amples informations, veuillez consulter [Adhérence des sessions pour les règles pondérées](gateway-rules-session-stickiness.md).

## Création d'une règle avec un routage cible
<a name="gateway-rules-create-target-route"></a>

L'exemple suivant crée une règle qui achemine les demandes correspondant à un modèle de chemin spécifique vers une cible.

**Example**  

1. Exécutez la commande suivante :

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

## Création d'une règle avec un routage cible pondéré
<a name="gateway-rules-create-weighted-target"></a>

L'exemple suivant crée une règle qui divise le trafic entre deux cibles. Dans cet exemple, 90 % du trafic est dirigé vers la cible principale et 10 % vers la cible Canary.

**Example**  

1. Exécutez la commande suivante :

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

## Création d'une règle avec des conditions et des actions combinées
<a name="gateway-rules-create-combined"></a>

L'exemple suivant crée une règle avec les deux types de condition et les deux types d'action. La demande doit correspondre à au moins une entrée pour chaque type de condition (ET logique entre les types).

**Example**  

1. Exécutez la commande suivante :

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

**Note**  
Cette règle ne correspond que lorsque l'appelant joue le rôle QA ET que le chemin de la demande correspond`/my-target-canary/*`. Les deux conditions doivent être satisfaites.

## Obtenir une règle de passerelle
<a name="gateway-rules-get"></a>

L'exemple suivant récupère les détails d'une règle de passerelle spécifique.

**Example**  

1. Exécutez la commande suivante :

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

## Mettre à jour une règle de passerelle
<a name="gateway-rules-update"></a>

L'exemple suivant met à jour la priorité et la description d'une règle de passerelle existante.

**Example**  

1. Exécutez la commande suivante :

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

## Règles de passerelle de liste
<a name="gateway-rules-list"></a>

L'exemple suivant répertorie toutes les règles d'une passerelle. Les résultats sont triés par ordre de priorité croissant.

**Example**  

1. Exécutez la commande suivante :

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

## Supprimer une règle de passerelle
<a name="gateway-rules-delete"></a>

L'exemple suivant supprime une règle de passerelle.

**Example**  

1. Exécutez la commande suivante :

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