Bedrock 调用次数比预期多了一倍?CloudTrail 5 分钟定位元凶

复盘成本发现 Bedrock 调用量异常偏高。CloudTrail 一查------一个忘关的测试 Lambda 在持续调用。

CloudTrail 记录 AWS 账号里所有 API 调用。对 AI Agent 来说,它能告诉你:谁调了 Bedrock、调了哪个模型、什么时间、从哪个 IP。

开启 Bedrock 数据事件

默认 CloudTrail 不记录 InvokeModel(属于数据事件),需要手动开:

bash 复制代码
aws cloudtrail put-event-selectors \
  --trail-name your-trail \
  --advanced-event-selectors '[{
    "Name": "Bedrock events",
    "FieldSelectors": [
      {"Field": "eventCategory", "Equals": ["Data"]},
      {"Field": "resources.type", "Equals": ["AWS::Bedrock::Model"]}
    ]
  }]'

实用查询(CloudWatch Logs Insights)

按小时统计调用量:

java 复制代码
fields @timestamp, requestParameters.modelId
| filter eventSource = "bedrock.amazonaws.com" and eventName = "InvokeModel"
| stats count(*) by bin(1h)

按模型统计:

sql 复制代码
fields requestParameters.modelId
| filter eventSource = "bedrock.amazonaws.com"
| stats count(*) as cnt by requestParameters.modelId
| sort cnt desc

查半夜异常调用:

less 复制代码
filter eventSource = "bedrock.amazonaws.com"
| filter datepart(@timestamp, "hour") < 6 or datepart(@timestamp, "hour") > 22

设告警

bash 复制代码
# Metric Filter
aws logs put-metric-filter \
  --log-group-name /aws/cloudtrail/bedrock-audit \
  --filter-name BedrockInvokeCount \
  --filter-pattern '{ $.eventSource = "bedrock.amazonaws.com" && $.eventName = "InvokeModel" }' \
  --metric-transformations metricName=BedrockInvokeModelCount,metricNamespace=Custom/Bedrock,metricValue=1

# 每小时超 100 次报警
aws cloudwatch put-metric-alarm \
  --alarm-name bedrock-high-invoke \
  --metric-name BedrockInvokeModelCount \
  --namespace Custom/Bedrock \
  --statistic Sum --period 3600 --threshold 100 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 1 \
  --alarm-actions arn:aws:sns:us-west-2:123456789012:ops-alerts

日志成本

S3 存长期($0.023/GB/月),CloudWatch Logs 存 30 天方便查( �L �Ј9�a9ai{�"x� �L9i*yd#�/k�X�Y\�9� zd�x� ���-�HRHY�[�9�9o�yi!�."y.��ie��&�PSH9�y��fd8� U��[��[�9�y�dy��8� P��Y�Z[9�yk�z+�x� ���KKB���9g*9.��jk:"�.�y��y��9."�j�:+�z`&�/���

相关推荐
yyuuuzz1 天前
跨境站点日志时区混乱的排查记录
运维·服务器·网络·云计算·aws·1024程序员节
Geek-Chow6 天前
AWS Lambda and Connection-Bound Backends (CloudHSM, Relational Databases)
云计算·aws
无风听海7 天前
AWS CLI的profile
云计算·aws
翼龙云_cloud7 天前
亚马逊云服务器服务代理商:Bedrock 实践经验 Serverless 架构下的 AI 自动化落地
服务器·架构·serverless·云计算·aws
yyuuuzz10 天前
云服务器部署 web 项目的进程常驻记录
运维·服务器·前端·阿里云·云计算·aws
无风听海11 天前
一文读懂 AWS IAM:从工牌、钥匙到统一登录
云计算·aws
Wonopop12 天前
AWS Budgets 预算告警与卡片拒付,为什么不能共用一个“超支“状态?附对账模型与反例测试
云计算·aws·支付系统·对账·mpchat
七牛开发者12 天前
从 AWS 到 OpenRouter,拆解 Agent 模型路由的工程设计
云计算·aws
小七-七牛开发者13 天前
从 AWS 到 OpenRouter,拆解 Agent 模型路由的工程设计
llm·agent·aws·openrouter·codingagent
可达智灵14 天前
堵了门,钥匙还在:一次持续九个月的 AWS 接管修复
云计算·aws·可达智灵