Kanji
・云架构工程师 / 自由职业 ・1993年出生 ・爱媛县出身 / 现居东京都涩谷区 ・5年 AWS 实战经验 个人资料详情
目录
bedrock:GuardrailIdentifier
当我在 Amazon Bedrock 上构建企业内部 AI 助手与多智能体(Multi-Agent)平台时,最让我绷紧神经的就是“生成式 AI 的安全护栏(Guardrails)”。
只要涉及客户数据或内部技术文档,个人信息(姓名、身份证号、信用卡号、电话号码)脱敏、禁止提及竞争对手、拦截不当言论,以及防范提示词注入(越狱攻击)都是不可妥协的红线。Amazon Bedrock 提供了托管安全服务“Amazon Bedrock Guardrails”,只需在控制台或通过 IaC 配置即可建立严格的过滤策略。
创建好企业标准护栏后,我在团队开发 Wiki 中明确规定:“生产环境调用 Bedrock 模型时,必须传入此 Guardrail ID 与版本号”,并向所有研发团队发出了通知。
然而几周后,当我在排查 AWS CloudTrail 与 CloudWatch Logs 审计日志时,却惊出了一身冷汗。
内部某个数据分析团队部署的 Python 批处理脚本,以及另一个团队引入的开源 AI 智能体运行时(通过 MCP 服务器集成), 在完全未指定任何 Guardrail 参数的情况下,直接调用了数千次底层基础模型(Claude 3.5 Sonnet / Llama 3) 。
# 生产环境中实际运行的“裸奔推理”代码 import boto3 bedrock = boto3.client('bedrock-runtime', region_name='us-east-1') # 开发者漏传了 guardrailIdentifier,但因为 IAM 角色拥有 InvokeModel 权限,推理请求照常放行! response = bedrock.invoke_model( modelId='anthropic.claude-3-5-sonnet-20241022-v2:0', body=b'{"prompt": "...", "max_tokens": 1000}' )
原因非常简单:开发人员在使用 boto3 或 LangChain 封装时漏写了参数,或者第三方智能体框架压根没有原生支持 Bedrock Guardrails 的参数透传。
只要将护栏的执行寄托在应用代码或客户端的“自觉性”上,就永远无法 100% 避免漏写参数、影子 AI(Shadow AI)或内部绕过。“安全护栏绝不能只靠客户端传递,必须在云基础设施与 IAM 访问控制层进行强制执行(Enforce)。”
在基础设施层彻底解决这一痛点的核心机制,正是 AWS 提供的 “IAM Policy-Based Enforcement for Amazon Bedrock Guardrails(基于 IAM 策略的护栏强制执行)” 。
▲ 整体架构图:由 IAM 策略评估引擎强制校验推理请求中的护栏标识,立即阻断未受保护的“裸奔调用”
IAM 策略强制执行的核心,是 Amazon Bedrock 提供的 IAM 条件键: bedrock:GuardrailIdentifier 。
过去,针对 Bedrock 推理 API( bedrock:InvokeModel 、 bedrock:InvokeModelWithResponseStream 、 bedrock:Converse 、 bedrock:ConverseStream )的 IAM 权限只能粗粒度地控制“某个角色能否调用某个模型”。
bedrock:InvokeModel
bedrock:InvokeModelWithResponseStream
bedrock:Converse
bedrock:ConverseStream
而引入 bedrock:GuardrailIdentifier 条件键后,我们可以在 IAM 策略层精确定义: “除非请求中携带了指定的 Guardrail ARN(包含具体版本号),否则一律拒绝模型推理调用” 。
PutEnforcedGuardrailConfiguration
这一机制的最大优势在于: 未携带合规护栏的请求在到达基础模型之前就会被 IAM 评估引擎以 403 Forbidden 直接拦截,因此不会产生任何模型推理 Token 费用 ,同时实现了安全防御与成本控制。
下面展示我在验证环境与生产 IAM 角色中实际部署的策略配置。
最稳健的做法是将允许调用模型的 Allow 语句,与针对未携带指定 Guardrail ARN 请求的显式 Deny ( StringNotEquals )语句结合使用:
Allow
Deny
StringNotEquals
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowBedrockInvocation", "Effect": "Allow", "Action": [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream", "bedrock:Converse", "bedrock:ConverseStream" ], "Resource": "arn:aws:bedrock:us-east-1::foundation-model/*" }, { "Sid": "AllowGuardrailApplication", "Effect": "Allow", "Action": "bedrock:ApplyGuardrail", "Resource": "arn:aws:bedrock:us-east-1:111122223333:guardrail/abc123def456" }, { "Sid": "EnforceMandatoryCorporateGuardrail", "Effect": "Deny", "Action": [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream", "bedrock:Converse", "bedrock:ConverseStream" ], "Resource": "arn:aws:bedrock:us-east-1::foundation-model/*", "Condition": { "StringNotEquals": { "bedrock:GuardrailIdentifier": "arn:aws:bedrock:us-east-1:111122223333:guardrail/abc123def456:1" } } } ] }
bedrock:ApplyGuardrail
InvokeModel
:1
:2
:DRAFT
guardrailIdentifier
在多账号环境中,为了防止子账号管理员私自放宽 IAM 策略,可以在 AWS Organizations 的根或 OU 层级部署服务控制策略(SCP)。当条件值包含通配符( * )时,应使用 ArnNotLike (或 StringNotLike ) ,且不要在同一 Condition 块内叠加 "Null": {"bedrock:GuardrailIdentifier": "true"} (同一块内的多个运算符按逻辑与 AND 评估,而 ArnNotLike 本身对未传参请求即评估为真):
*
ArnNotLike
StringNotLike
"Null": {"bedrock:GuardrailIdentifier": "true"}
{ "Version": "2012-10-17", "Statement": [ { "Sid": "EnforceGuardrailAcrossAllAccounts", "Effect": "Deny", "Action": [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream", "bedrock:Converse", "bedrock:ConverseStream" ], "Resource": "arn:aws:bedrock:*::foundation-model/*", "Condition": { "ArnNotLike": { "bedrock:GuardrailIdentifier": "arn:aws:bedrock:*:*:guardrail/corp-standard-*:*" } } } ] }
除了通过 SCP 对未指定请求执行 403 拒绝(Fail-Closed)外,若结合 AWS Organizations 的 Amazon Bedrock Policies 或账号级的 PutEnforcedGuardrailConfiguration API( aws bedrock put-enforced-guardrail-configuration ) ,还可以在客户端省略参数时由基础设施自动注入基线护栏。将“工作负载级显式绑定( bedrock:GuardrailIdentifier )”与“全账号基线自动兜底( PutEnforcedGuardrailConfiguration )”结合,即可构建无死角的纵深防御。
aws bedrock put-enforced-guardrail-configuration
下面是通过 AWS CLI 与 boto3 实测该 IAM 强制策略的真实终端日志。
▲ 企业标准 Guardrail(PII 脱敏与越狱攻击拦截)配置详情及 IAM 强制策略(敏感账号 ID 已脱敏)
首先,按照传统方式在不传护栏参数的情况下调用 InvokeModel :
# 未指定护栏调用 Claude 3.5 Sonnet aws bedrock-runtime invoke-model --model-id "anthropic.claude-3-5-sonnet-20241022-v2:0" --body '{"anthropic_version":"bedrock-2023-05-31","max_tokens":200,"messages":[{"role":"user","content":"请介绍内部开发规范"}]}' --cli-binary-format raw-in-base64-out response.json
执行结果(报错日志):
An error occurred (AccessDeniedException) when calling the InvokeModel operation: User: arn:aws:iam::111122223333:user/test-developer is not authorized to perform: bedrock:InvokeModel on resource: arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0 with an explicit deny in a service control policy or identity-based policy
请求在 IAM 层被 AccessDeniedException (with an explicit deny) 瞬间拒绝,完全没有触达模型。
AccessDeniedException (with an explicit deny)
接着,加上 --guardrail-identifier 与 --guardrail-version "1" 重新发起请求:
--guardrail-identifier
--guardrail-version "1"
# 指定合规 Guardrail ID 与 Version 1 调用 aws bedrock-runtime invoke-model --model-id "anthropic.claude-3-5-sonnet-20241022-v2:0" --guardrail-identifier "abc123def456" --guardrail-version "1" --body '{"anthropic_version":"bedrock-2023-05-31","max_tokens":200,"messages":[{"role":"user","content":"客户田中太郎的电话是 090-1234-5678,请起草内部联络备忘录。"}]}' --cli-binary-format raw-in-base64-out response.json cat response.json | jq .
▲ CLI 实机验证日志:未指定请求被 IAM 立即拦截,合规请求成功将电话号码自动脱敏为 [PHONE_NUMBER]
执行结果(正常响应并自动脱敏):
{ "content": [ { "text": "内部联络备忘录已生成。 【客户联络备忘录】 对象:田中太郎 先生 联系方式:[PHONE_NUMBER] ...", "type": "text" } ], "amazon-bedrock-guardrailAction": "INTERVENED", "amazon-bedrock-guardrailIntervention": { "sensitiveInformationPolicy": { "piiEntities": [ { "type": "PHONE", "action": "ANONYMIZED" } ] } } }
电话号码被自动替换为 [PHONE_NUMBER] ,安全合规地完成了推理。
[PHONE_NUMBER]
为了验证每次强制经过护栏是否会拖慢响应速度,我使用 Claude 3.5 Sonnet 进行了 100 次请求的基准测试:
实测表明,护栏评估带来的额外延迟仅为 43~57 ms 左右 ,无论是对话界面还是多步智能体循环,体感延迟几乎为零。
AccessDenied
["...:guardrail/xxx:1", "...:guardrail/xxx:2"]
随着企业生成式 AI 与自主智能体从实验走向核心生产,依赖代码审查或开发人员记忆来维系安全的模式必然会触及瓶颈。
利用 AWS bedrock:GuardrailIdentifier 条件键实施 IAM 策略强制执行,能够将云架构师熟知的“纵深防御(Defense in Depth)”原则无缝引入生成式 AI 领域,从根源上消除“裸奔推理”的后顾之忧。
Real-time AI quant analysis of corporate earnings reports.
提供逼真 AI 模拟通话与多维反馈,结合 16 讲实战社交学院与工作室级 AI 肖像生成,全面提升社交互动质量。
拍小票即可完成高精度智能拆单,支持 LINE 登录无缝结算团队聚会支出,告别繁琐账单分摊。
占卜师、管家、调酒师等 AI 角色实时引导话题与聚会游戏,让线下与线上聚餐氛围瞬间升温。
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。