跳到內容

將 OpenClaw 連接到 Amazon Bedrock:快速設定指南

每次串接新的 AI 模型都要管理一堆 API key 真的很煩,特別是當你的公司已經在使用 AWS 的時候,安全性與權限管理往往變得非常複雜。如果能直接利用現有的 AWS 基礎設施來驅動你的 AI Agent,不僅能簡化流程,還能讓架構更乾淨。

OpenClaw 可以透過 pi‑ai 的 Bedrock Converse 串流供應商來使用 Amazon Bedrock 模型。Bedrock 的驗證方式是使用 AWS SDK 預設憑證鏈 (default credential chain),而不是一般的 API key。

  • Provider: amazon-bedrock
  • API: bedrock-converse-stream
  • Auth: AWS 憑證(環境變數、共享設定檔或執行個體角色)
  • Region: AWS_REGION 或 AWS_DEFAULT_REGION(預設為 us-east-1)

如果偵測到 AWS 憑證,OpenClaw 可以自動探索支援 streaming 和 text output 的 Bedrock 模型。探索功能會使用 bedrock:ListFoundationModels 並進行快取(預設為 1 小時)。

設定選項位於 models.bedrockDiscovery 下:

{
models: {
bedrockDiscovery: {
enabled: true,
region: "us-east-1",
providerFilter: ["anthropic", "amazon"],
refreshInterval: 3600,
defaultContextWindow: 32000,
defaultMaxTokens: 4096,
},
},
}

說明:

  • 當存在 AWS 憑證時,enabled 預設為 true。
  • region 預設會抓 AWS_REGION 或 AWS_DEFAULT_REGION,若都沒有則使用 us-east-1。
  • providerFilter 用於匹配 Bedrock 供應商名稱(例如 anthropic)。
  • refreshInterval 單位為秒;設定為 0 可停用快取。
  • defaultContextWindow(預設 32000)和 defaultMaxTokens(預設 4096)會套用到探索出的模型(如果你知道模型的具體限制,可以自行覆蓋)。
  1. 確保 gateway host 上有可用的 AWS 憑證:
Terminal window
export AWS_ACCESS_KEY_ID="AKIA..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_REGION="us-east-1"
# Optional:
export AWS_SESSION_TOKEN="..."
export AWS_PROFILE="your-profile"
# Optional (Bedrock API key/bearer token):
export AWS_BEARER_TOKEN_BEDROCK="..."
  1. 在你的設定中加入 Bedrock 供應商和模型(不需要 apiKey):
{
models: {
providers: {
"amazon-bedrock": {
baseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com",
api: "bedrock-converse-stream",
auth: "aws-sdk",
models: [
{
id: "us.anthropic.claude-opus-4-6-v1:0",
name: "Claude Opus 4.6 (Bedrock)",
reasoning: true,
input: ["text", "image"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 200000,
maxTokens: 8192,
},
],
},
},
},
agents: {
defaults: {
model: { primary: "amazon-bedrock/us.anthropic.claude-opus-4-6-v1:0" },
},
},
}

當你在附帶 IAM 角色的 EC2 執行個體上執行 OpenClaw 時,AWS SDK 會自動使用執行個體中繼資料服務 (IMDS) 進行驗證。不過,OpenClaw 目前的憑證偵測只會檢查環境變數,不會主動檢查 IMDS 憑證。

解決方案: 設定 AWS_PROFILE=default 來發出 AWS 憑證可用的訊號。實際的驗證仍會透過 IMDS 使用執行個體角色。

Terminal window
# Add to ~/.bashrc or your shell profile
export AWS_PROFILE=default
export AWS_REGION=us-east-1

EC2 執行個體角色所需的 IAM 權限:

  • bedrock:InvokeModel
  • bedrock:InvokeModelWithResponseStream
  • bedrock:ListFoundationModels(用於自動探索)

或者直接附加受管政策 AmazonBedrockFullAccess。

Terminal window
# 1. Create IAM role and instance profile
aws iam create-role --role-name EC2-Bedrock-Access \
--assume-role-policy-document '{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}]
}'
aws iam attach-role-policy --role-name EC2-Bedrock-Access \
--policy-arn arn:aws:iam::aws:policy/AmazonBedrockFullAccess
aws iam create-instance-profile --instance-profile-name EC2-Bedrock-Access
aws iam add-role-to-instance-profile \
--instance-profile-name EC2-Bedrock-Access \
--role-name EC2-Bedrock-Access
# 2. Attach to your EC2 instance
aws ec2 associate-iam-instance-profile \
--instance-id i-xxxxx \
--iam-instance-profile Name=EC2-Bedrock-Access
# 3. On the EC2 instance, enable discovery
openclaw config set models.bedrockDiscovery.enabled true
openclaw config set models.bedrockDiscovery.region us-east-1
# 4. Set the workaround env vars
echo 'export AWS_PROFILE=default' >> ~/.bashrc
echo 'export AWS_REGION=us-east-1' >> ~/.bashrc
source ~/.bashrc
# 5. Verify models are discovered
openclaw models list
  • Bedrock 要求在你的 AWS 帳號/區域中手動啟用 model access。
  • 自動探索功能需要 bedrock:ListFoundationModels 權限。
  • 如果你使用 profile,請在 gateway host 上設定 AWS_PROFILE。
  • OpenClaw 尋找憑證的優先順序為:AWS_BEARER_TOKEN_BEDROCK,接著是 AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY,再來是 AWS_PROFILE,最後才是預設的 AWS SDK 鏈。
  • Reasoning 支援取決於模型;請查看 Bedrock 模型卡以了解目前的技術能力。
  • 如果你偏好使用受管金鑰流程,也可以在 Bedrock 前端架設一個相容 OpenAI 的 Proxy,並將其設定為 OpenAI 供應商。

你可以透過在 amazon-bedrock 插件設定中加入 guardrail 物件,將 Amazon Bedrock Guardrails 套用到所有的 Bedrock 模型調用。防護欄讓你能夠執行內容過濾、主題拒絕、文字篩選、敏感資訊過濾以及內容紮實度檢查 (contextual grounding checks)。

{
plugins: {
entries: {
"amazon-bedrock": {
config: {
guardrail: {
guardrailIdentifier: "abc123", // guardrail ID or full ARN
guardrailVersion: "1", // version number or "DRAFT"
streamProcessingMode: "sync", // optional: "sync" or "async"
trace: "enabled", // optional: "enabled", "disabled", or "enabled_full"
},
},
},
},
},
}
  • guardrailIdentifier(必填)接受防護欄 ID(例如 abc123)或完整 ARN(例如 arn:aws:bedrock:us-east-1:123456789012:guardrail/abc123)。
  • guardrailVersion(必填)指定要使用的已發佈版本,或使用 "DRAFT" 作為草稿版本。
  • streamProcessingMode(選填)控制在串流期間防護欄評估是同步 ("sync") 還是非同步 ("async") 執行。如果省略,Bedrock 將使用其預設行為。
  • trace(選填)在 API 回應中啟用防護欄追蹤輸出。偵錯時可設定為 "enabled" 或 "enabled_full";正式環境請省略或設定為 "disabled"。

Gateway 使用的 IAM 實體除了標準的調用權限外,還必須擁有 bedrock:ApplyGuardrail 權限。

想要更快速地完成設定嗎?試試我們的 AI Setup Assistant。

OpenClaw

OpenClaw Expert

還是卡住了?

如果這篇文件沒有解決你的情境,直接問 OpenClaw Expert,拿到可執行步驟。