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除外と有効成分あたりの実質単価比較データベース。