用 VPC Endpoint 让 Amazon Bedrock API 调用全走内网:完整配置与踩坑记录

## 背景 在 EC2 上运行 AI Agent 调用 Amazon Bedrock 时,默认流量路径是:EC2 → NAT Gateway → 公网 → Bedrock Endpoint。流量经过公共互联网,在合规审计和安全性上都不理想。 通过配置 VPC Endpoint(PrivateLink),可以让所有 Bedrock API 调用走 VPC 内网,完全不经过公网。 ## Amazon Bedrock 支持的 Endpoint | Endpoint 后缀 | 用途 | |--------------|------| | `bedrock` | 控制面 API | | `bedrock-runtime` | InvokeModel 等运行时调用 | | `bedrock-mantle` | 推理相关 | | `bedrock-agent` | Agents 构建 | | `bedrock-agent-runtime` | Agents 运行时 | 日常 AI Agent 使用主要需要 `bedrock-runtime`。 ## 配置步骤 ### 1. 创建 Interface Endpoint ```bash aws ec2 create-vpc-endpoint \ --vpc-id vpc-xxxxxxxx \ --service-name com.amazonaws.us-west-2.bedrock-runtime \ --vpc-endpoint-type Interface \ --subnet-ids subnet-aaaa subnet-bbbb \ --security-group-ids sg-xxxxxxxx \ --private-dns-enabled ``` `--private-dns-enabled` 开启后,`bedrock-runtime.region.amazonaws.com` 自动解析到 VPC 内网地址。应用代码无需改动。 ### 2. 安全组 为 VPC Endpoint 配置安全组,入站允许 VPC CIDR 的 TCP 443: ```bash aws ec2 authorize-security-group-ingress \ --group-id sg-vpce-xxxxx \ --protocol tcp \ --port 443 \ --cidr 10.0.0.0/16 ``` ### 3. Endpoint Policy(推荐) ```json { "Version": "2012-10-17", "Statement": [ { "Principal": { "AWS": "arn:aws:iam::123456789012:role/openclaw-role" }, "Effect": "Allow", "Action": ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"], "Resource": "arn:aws:bedrock:us-west-2::foundation-model/*" } ] } ``` ### 4. 验证 ```bash nslookup bedrock-runtime.us-west-2.amazonaws.com # 返回 10.x.x.x 内网 IP 表示流量已走 VPC Endpoint ``` ## 推荐的网络架构 ``` VPC ├── 公有子网:NAT Gateway(Agent 需要访问外部网站时用) ├── 私有子网:EC2 + VPC Endpoint ENI └── 安全组:EC2 SG(SSH 限 IP) + VPCE SG(443 限 VPC CIDR) ``` EC2 放私有子网无公网 IP,SSH 通过 EC2 Instance Connect Endpoint 或 Systems Manager 连接。 ## 额外建议的 Endpoint - S3 Gateway Endpoint(免费) - CloudWatch Logs(推日志) - Secrets Manager(密钥管理) - STS(IAM Role 认证) ## 踩坑 1. VPC 的 `enableDnsSupport` 和 `enableDnsHostnames` 必须开启 2. 安全组入站 443 未开放会导致超时(报错像 DNS 问题) 3. 多 AZ 场景建议每个 AZ 都配 Endpoint,避免跨 AZ 延迟 ## 成本 Interface Endpoint 约 $0.01/小时/AZ(~$7/月)。相比 NAT Gateway 的 $0.045/GB 数据处理费,在高流量场景下更经济。S3 Gateway Endpoint 完全免费。 --- > 在亚马逊云科技 EC2(us-west-2)上验证。
posted @ 2026-04-14 11:40  亚马逊云开发者  阅读(7)  评论(0)    收藏  举报