在 us-east-1 用 boto3 1.34 调 anthropic.claude-3-5-sonnet-20240620-v1:0,IAM 用户已经附加 AmazonBedrockFullAccess,仍然返回 AccessDeniedException,原文 not authorized to perform bedrock:InvokeModel with an explicit deny in an identity-based policy,控制台 Model access 页面显示 Available。

原因不止一个,得分开看。模型访问只在部分区域申请过 调用发到了没开通的区;权限边界或 SCP 里有显式 Deny 覆盖了 Allow;用了跨区推理配置 需要额外的源区域权限;切换过 profile 但进程里仍是旧会话凭证。

先确认当前身份和实际区域

aws sts get-caller-identity; aws configure get region

随后用最小请求复现并把错误全文打出来

aws bedrock-runtime invoke-model --model-id anthropic.claude-3-5-sonnet-20240620-v1:0 --body '{"anthropic_version":"bedrock-2023-05-31","max_tokens":32,"messages":[{"role":"user","content":"hi"}]}' out.json --region us-east-1

接着列出该区域可用的基础模型

aws bedrock list-foundation-models --region us-east-1 --query 'modelSummaries[].modelId'

末了在开通区域重新申请访问后用同一 profile 调

python -c "import boto3;print(boto3.Session().region_name)"

这台环境是别人交接的,不少默认值被动过,只能一项项核。

同事提醒我看一下最近一次发布,时间点确实对得上。

别只看报错消失,验证要多做一步。同一 profile 在开通区域调用返回 200,out.json 里有 completion 字段,CloudTrail 里能看到 InvokeModel 成功记录。

这里还有个坑。FullAccess 既不代表模型访问已开通 也不代表没有 SCP 拒绝,报错里带 explicit deny 时先查策略边界而不是换 key。同一份配置在测试环境跑通,不代表线上没事,两边的默认值经常不一样。

作者 9823189