View a markdown version of this page

Exemplos de API de regras de gateway - Amazon Bedrock AgentCore

Exemplos de API de regras de gateway

Os exemplos a seguir mostram como gerenciar regras de gateway usando a AWS CLI e o SDK do AWS Python (Boto3).

Crie uma regra com uma substituição estática do pacote de configuração

O exemplo a seguir cria uma regra que fixa um principal do IAM específico a uma versão do pacote de configuração. Use essa abordagem para controle de qualidade ou depuração.

exemplo
AWS CLI
  1. Execute este comando: .

    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" } } } ]'
AWS Python SDK (Boto3)
  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']}")

Crie uma regra padrão abrangente

O exemplo a seguir cria uma regra sem condições. Essa regra corresponde a todo o tráfego e serve como padrão. Atribua um número de alta prioridade para que regras mais específicas tenham precedência.

exemplo
AWS CLI
  1. Execute este comando: .

    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" } } } ]'
AWS Python SDK (Boto3)
  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']}")

Crie uma regra com a divisão ponderada do pacote de configuração

O exemplo a seguir cria uma regra que divide o tráfego entre duas versões do pacote de configuração para A/B teste. Neste exemplo, 80% do tráfego usa a variante A e 20% usa a variante B.

exemplo
AWS CLI
  1. Execute este comando: .

    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" } ] } } } ]'
AWS Python SDK (Boto3)
  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

Para manter uma experiência de usuário consistente durante o A/B teste, inclua o X-Amzn-Bedrock-AgentCore-Runtime-Session-Id cabeçalho nas solicitações de invocação do gateway. Para obter mais informações, consulte Fixação da sessão para regras ponderadas.

Crie uma regra com roteamento de destino

O exemplo a seguir cria uma regra que encaminha solicitações que correspondem a um padrão de caminho específico para um destino.

exemplo
AWS CLI
  1. Execute este comando: .

    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" } } } ]'
AWS Python SDK (Boto3)
  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']}")

Crie uma regra com roteamento de metas ponderadas

O exemplo a seguir cria uma regra que divide o tráfego entre dois destinos. Neste exemplo, 90% das rotas de tráfego para o alvo principal e 10% para o alvo canário.

exemplo
AWS CLI
  1. Execute este comando: .

    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" } ] } } } ]'
AWS Python SDK (Boto3)
  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']}")

Crie uma regra com condições e ações combinadas

O exemplo a seguir cria uma regra com os dois tipos de condição e os dois tipos de ação. A solicitação deve corresponder a pelo menos uma entrada de cada tipo de condição (E lógica entre os tipos).

exemplo
AWS CLI
  1. Execute este comando: .

    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" } } } ]'
AWS Python SDK (Boto3)
  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

Essa regra corresponde somente quando o chamador tem a função de controle de qualidade e o caminho da solicitação corresponde/my-target-canary/*. Ambas as condições devem ser satisfeitas.

Obtenha uma regra de gateway

O exemplo a seguir recupera os detalhes de uma regra de gateway específica.

exemplo
AWS CLI
  1. Execute este comando: .

    aws bedrock-agentcore-control get-gateway-rule \ --gateway-identifier my-gateway-abc1234567 \ --rule-id 12345678-1234-1234-1234-123456789012
AWS Python SDK (Boto3)
  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']}")

Atualizar uma regra de gateway

O exemplo a seguir atualiza a prioridade e a descrição de uma regra de gateway existente.

exemplo
AWS CLI
  1. Execute este comando: .

    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"
AWS Python SDK (Boto3)
  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']}")

Listar regras de gateway

O exemplo a seguir lista todas as regras de um gateway. Os resultados são classificados por prioridade em ordem crescente.

exemplo
AWS CLI
  1. Execute este comando: .

    aws bedrock-agentcore-control list-gateway-rules \ --gateway-identifier my-gateway-abc1234567
AWS Python SDK (Boto3)
  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']}")

Excluir uma regra de gateway

O exemplo a seguir exclui uma regra de gateway.

exemplo
AWS CLI
  1. Execute este comando: .

    aws bedrock-agentcore-control delete-gateway-rule \ --gateway-identifier my-gateway-abc1234567 \ --rule-id 12345678-1234-1234-1234-123456789012
AWS Python SDK (Boto3)
  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']}")