View a markdown version of this page

网关规则 API 示例 - Amazon Bedrock AgentCore

网关规则 API 示例

以下示例展示了如何使用 AWS CLI 和 AWS Python SDK (Boto3) 管理网关规则。

使用静态配置包覆盖创建规则

以下示例创建了一个规则,用于将特定的 IAM 委托人固定到配置包版本。使用此方法进行 QA 或调试。

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

创建包罗万象的默认规则

以下示例创建了一个不带条件的规则。此规则匹配所有流量,并作为默认规则。分配一个高优先级数字,以便更具体的规则优先。

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

使用加权配置包拆分创建规则

以下示例创建了一个规则,用于在两个配置包版本之间分配流量以进行 A/B 测试。在此示例中,80% 的流量使用变体 A,20% 的流量使用变体 B

AWS CLI
  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" } ] } } } ]'
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']}")
注意

为了在 A/B 测试期间保持一致的用户体验,请在网关调用请求中包含X-Amzn-Bedrock-AgentCore-Runtime-Session-Id标头。有关更多信息,请参阅 加权规则的会话粘性

使用目标路由创建规则

以下示例创建了一个规则,用于将匹配特定路径模式的请求路由到目标。

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

使用加权目标路由创建规则

以下示例创建了一个在两个目标之间分配流量的规则。在此示例中,90% 的流量路由到主目标,10% 的流量路由到金丝雀目标。

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

创建包含组合条件和操作的规则

以下示例创建了一个既有条件类型又有两种操作类型的规则。请求必须与每种条件类型中的至少一个条目相匹配(跨类型的 AND 逻辑)。

AWS CLI
  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" } } } ]'
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']}")
注意

只有当调用者是 QA 角色并且请求路径匹配时,此规则才匹配/my-target-canary/*。这两个条件都必须满足。

获取网关规则

以下示例检索特定网关规则的详细信息。

AWS CLI
  1. 运行以下命令:

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

更新网关规则

以下示例更新了现有网关规则的优先级和描述。

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

列出网关规则

以下示例列出了网关的所有规则。结果按优先级升序排序。

AWS CLI
  1. 运行以下命令:

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

删除网关规则

以下示例删除了网关规则。

AWS CLI
  1. 运行以下命令:

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