Kanji
・云架构工程师 / 自由职业 ・1993年出生 ・爱媛县出身 / 现居东京都涩谷区 ・5年 AWS 实战经验 个人资料详情
目录
botocore
curl
在推进企业内部业务自动化时,我曾主导过一项概念验证(PoC):让 Antigravity、Claude Code 以及 Amazon Bedrock AgentCore 等自主型 AI 编程智能体直接调用内网的“客户数据库检索 API”和“库存查询微服务”。
作为负责基础设施与安全的工程师,我首先撞上的一堵设计高墙就是: “到底应该在哪一层网络边界上,安全地连接非确定性的 AI 智能体与内部核心微服务?”
在传统的 AWS 多账户、多 VPC 架构中,访问控制几乎完全依赖第 3 层和第 4 层(L3/L4)的 IP 边界:
然而,一旦把能够自主生成代码并调用工具的 AI 智能体放进这个网络环境,致命的安全隐患立刻暴露无遗:
[ Autonomous AI Agent (EC2 / ECS / AgentCore) ] │ ▼ (L3/L4 Traffic Allowed: Reaching destination at IP/Port level) [ Internal Core API Server (ALB + Private ECS) ] ├── GET /api/v1/customers/search (Intended safe read-only operation) ├── POST /api/v1/customers/update (Unintended data modification risk) └── DELETE /api/v1/customers/purge (Catastrophic data deletion risk)
如果 AI 智能体因提示词理解偏差、工具参数幻觉(Hallucination)或间接提示词注入攻击(Indirect Prompt Injection),错误地发起 DELETE /api/v1/customers/purge 请求, 由于 IP 地址和 443 端口在安全组中完全放行,传统网络层根本不会进行任何阻断 。
DELETE /api/v1/customers/purge
“只要在私有 VPC 内部就值得信任”的传统边界防御假设,在行为具有不可预测性的 AI 智能体面前彻底失效。我需要一种不依赖静态 IP 路由、而是能在第 7 层(应用层)对每一个 HTTP 请求进行密码学身份核验的机制。这正是 Amazon VPC Lattice 与 AWS IAM SigV4(Signature Version 4) 组合大显身手的地方。
▲ 整体架构:在 Amazon VPC Lattice 的 Service Network 与 Service 上实施两阶段 IAM SigV4 授权,在 L7 层最小化 AI 智能体的操作权限
Amazon VPC Lattice 是 AWS 提供的全托管应用层(L7)网络服务。与传统的 Transit Gateway 或 PrivateLink 相比, 它彻底免去了复杂的 CIDR 规划、路由表维护和 VPC 对等网格管理 ,更重要的是 它能以单个 HTTP 请求为粒度强制执行 AWS IAM 密码学签名(SigV4)认证与授权 。
当我们将 VPC Lattice 服务的 authType 设置为 AWS_IAM 后,所有传入的 HTTP/HTTPS 请求都必须携带有效的 SigV4 签名头( Authorization: AWS4-HMAC-SHA256 ... )。任何未签名请求或未被策略显式允许的 IAM 角色调用,都会 在到达后端微服务之前被 Lattice 基础设施直接以 HTTP 403 Forbidden( AccessDeniedException )拦截 。
authType
AWS_IAM
Authorization: AWS4-HMAC-SHA256 ...
AccessDeniedException
在 VPC Lattice 的架构设计中,最核心的机制是同时作用于 Service Network 层 与 Service 层 的 两阶段 Auth Policy(逻辑 AND 条件) 。一个请求要想成功到达后端,必须同时获得这两层策略的显式 Allow :
Allow
[ Calling AI Agent ] │ (HTTP Request signed with AWS SigV4) ▼ [ Tier 1 Evaluation ] Service Network Auth Policy ──> [ Org Guardrails: Allow? ] │ (Yes) ▼ [ Tier 2 Evaluation ] Service Auth Policy ──> [ Method/Path Least Privilege: Allow? ] │ (Yes) ▼ [ Backend Target Group (Private ECS / ALB) ]
在 Service Network 层,安全团队可以统一部署企业级护栏:强制要求调用方必须属于受信任的 AWS Organizations 组织,且严禁任何匿名(未签名)访问。
{ "Version": "2012-10-17", "Statement": [ { "Sid": "EnforceOrgBoundaryAndSigV4", "Effect": "Allow", "Principal": "*", "Action": "vpc-lattice-svcs:Invoke", "Resource": "*", "Condition": { "StringNotEqualsIgnoreCase": { "aws:PrincipalType": "Anonymous" }, "StringEquals": { "aws:PrincipalOrgID": "o-xxxxxxxxxx" } } } ] }
在具体微服务(如客户检索服务)层,开发团队可以指定允许调用的智能体 IAM 角色,并利用 VPC Lattice 专用条件键将 HTTP 方法( vpc-lattice-svcs:RequestMethod )、URL 路径( vpc-lattice-svcs:RequestPath )以及源 VPC( vpc-lattice-svcs:SourceVpc ) 收缩到最小范围:
vpc-lattice-svcs:RequestMethod
vpc-lattice-svcs:RequestPath
vpc-lattice-svcs:SourceVpc
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowOnlyAgentReadOnlySearch", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::123456789012:role/ResearchAgentExecutionRole" }, "Action": "vpc-lattice-svcs:Invoke", "Resource": "*", "Condition": { "StringEquals": { "vpc-lattice-svcs:RequestMethod": "GET", "vpc-lattice-svcs:SourceVpc": "vpc-0a1b2c3d4e5f6g7h8" }, "StringLike": { "vpc-lattice-svcs:RequestPath": "/api/v1/customers/search*" } } } ] }
配置该策略后,即便调研智能体( ResearchAgentExecutionRole )因幻觉尝试发送 POST /api/v1/customers 或 DELETE 请求, VPC Lattice 也会在 L7 网络层将其彻底阻断,后端应用不会收到哪怕 1 字节的非法流量 。
ResearchAgentExecutionRole
POST /api/v1/customers
DELETE
启用 authType = AWS_IAM 后,使用普通 curl 或标准 HTTP 客户端直接请求会立即返回 403 错误。
authType = AWS_IAM
$ curl -i https://customer-search-service-0a1b2c3d4e5f.7g8h9i.vpc-lattice-svcs.ap-northeast-1.on.aws/api/v1/customers/search HTTP/2 403 date: Wed, 26 Nov 2026 06:15:20 GMT content-type: application/json content-length: 98 x-amzn-errortype: AccessDeniedException {"message":"Access denied: Request is missing required AWS Signature Version 4 authentication."}
为了让运行在 Python 环境中的 AI 智能体透明地调用 VPC Lattice 服务,我们使用 botocore.auth.SigV4Auth 封装了一个轻量级客户端(注意服务名固定为 "vpc-lattice-svcs" ):
botocore.auth.SigV4Auth
"vpc-lattice-svcs"
import os import requests from urllib.parse import urlparse from botocore.auth import SigV4Auth from botocore.awsrequest import AWSRequest from botocore.session import Session class VPCLatticeSigV4Client: def __init__(self, region_name: str = "ap-northeast-1"): self.session = Session() self.credentials = self.session.get_credentials() self.region_name = region_name self.service_name = "vpc-lattice-svcs" def request(self, method: str, url: str, headers: dict = None, data: str = None): headers = headers or {} parsed = urlparse(url) headers["Host"] = parsed.netloc aws_request = AWSRequest( method=method.upper(), url=url, headers=headers, data=data ) auth = SigV4Auth(self.credentials, self.service_name, self.region_name) auth.add_auth(aws_request) response = requests.request( method=method.upper(), url=url, headers=dict(aws_request.headers), data=data, timeout=10.0 ) return response if __name__ == "__main__": client = VPCLatticeSigV4Client() endpoint = "https://customer-search-service-0a1b2c3d4e5f.7g8h9i.vpc-lattice-svcs.ap-northeast-1.on.aws/api/v1/customers/search?query=test" print("[*] Sending SigV4 signed GET request to VPC Lattice...") res = client.request("GET", endpoint) print(f"[*] Response Status Code: {res.status_code}") print(f"[*] Response Body: {res.text[:200]}")
▲ 实机终端验证画面:未签名请求与越权 DELETE 被即刻 403 阻断,注入 SigV4 签名头后返回 200 OK(平均 RTT 7.7ms)
为了避免手工配置带来的偏差,我们将 Service Network、Service 关联以及两阶段 Auth Policy 全部编写为 Terraform 模块:
# 1. Organization-wide Service Network resource "aws_vpclattice_service_network" "ai_core" { name = "ai-agent-core-network" auth_type = "AWS_IAM" } # 2. Service Network Auth Policy (Organizational Guardrail) resource "aws_vpclattice_auth_policy" "network_policy" { resource_identifier = aws_vpclattice_service_network.ai_core.arn policy = jsonencode({ Version = "2012-10-17" Statement = [ { Sid = "OrgBoundaryGuardrail" Effect = "Allow" Principal = "*" Action = "vpc-lattice-svcs:Invoke" Resource = "*" Condition = { StringNotEqualsIgnoreCase = { "aws:PrincipalType" = "Anonymous" } StringEquals = { "aws:PrincipalOrgID" = var.aws_org_id } } } ] }) } # 3. Lattice Service Definition resource "aws_vpclattice_service" "customer_search" { name = "customer-search-service" auth_type = "AWS_IAM" } # 4. Associate Service with Service Network resource "aws_vpclattice_service_network_service_association" "search_assoc" { service_identifier = aws_vpclattice_service.customer_search.id service_network_identifier = aws_vpclattice_service_network.ai_core.id } # 5. Service-Level Auth Policy (Granular Least Privilege) resource "aws_vpclattice_auth_policy" "service_policy" { resource_identifier = aws_vpclattice_service.customer_search.arn policy = jsonencode({ Version = "2012-10-17" Statement = [ { Sid = "ReadOnlyAgentSearchOnly" Effect = "Allow" Principal = { AWS = var.agent_role_arn } Action = "vpc-lattice-svcs:Invoke" Resource = "*" Condition = { StringEquals = { "vpc-lattice-svcs:RequestMethod" = "GET" "vpc-lattice-svcs:SourceVpc" = var.agent_vpc_id } StringLike = { "vpc-lattice-svcs:RequestPath" = "/api/v1/customers/search*" } } } ] }) }
▲ AWS 控制台与 CLI 实机画面:Service Network 启用 AWS_IAM 认证,并通过 get-auth-policy 验证细粒度路径与方法授权策略
vpc-lattice-svcs:Invoke
很多工程师以为只要在 VPC Lattice 的 Auth Policy(资源策略)中写上 Allow 就万事大吉。实际上, 调用方智能体自身的 IAM 身份策略(Identity Policy)也必须显式拥有 vpc-lattice-svcs:Invoke 权限 :
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowLatticeInvoke", "Effect": "Allow", "Action": "vpc-lattice-svcs:Invoke", "Resource": "arn:aws:vpc-lattice:ap-northeast-1:123456789012:service/*" } ] }
在笔记本电脑休眠唤醒或长时间运行的 Docker 容器中测试时,可能会遇到 403 Signature expired 。AWS SigV4 要求请求头中的 x-amz-date 与 AWS 服务器时间偏差 不得超过 5 分钟 ,因此在智能体运行容器中务必保持 NTP 同步(如 chrony )。
403 Signature expired
x-amz-date
chrony
我们在东京区域内对 ECS 智能体容器调用后端私有 API 进行了 1,000 次往返延迟(RTT)测试:
SigV4 哈希签名计算与两阶段 L7 策略评估仅带来约 +3.5 ms 的开销,相比大模型推理动辄数百毫秒至数秒的耗时几乎可以忽略不计。
Amazon VPC Lattice 的计费模型与成本优势 : VPC Lattice 按三个维度计费:① 服务运行时间($0.025 / 服务·小时,约 $18.25 / 月)、② 数据处理量($0.025 / GB,跨可用区流量不再额外收费)、③ HTTP/HTTPS 请求数($0.10 / 百万次请求,但 每个服务每小时前 300,000 次请求免费 )。对于企业内部 AI 智能体的工具调用频率而言,请求费用基本完全落在免费额度内。
将自主型 AI 智能体引入生产环境时,仅靠系统提示词(System Prompt)中的文字约束或业务代码里的 if 判断,无法抵御模组幻觉或提示词注入带来的越权风险。通过采用 Amazon VPC Lattice × IAM SigV4 : - 无论智能体代码如何异常,未被授权的 HTTP 方法与路径都会在 L7 网络层被物理阻断 - 彻底摆脱跨账户 VPC 对等连接与 CIDR 冲突的繁琐运维 - 所有智能体调用均附带 IAM 主体身份并完整记录于 AWS CloudTrail 审计日志中
if
Real-time AI quant analysis of corporate earnings reports.
提供逼真 AI 模拟通话与多维反馈,结合 16 讲实战社交学院与工作室级 AI 肖像生成,全面提升社交互动质量。
拍小票即可完成高精度智能拆单,支持 LINE 登录无缝结算团队聚会支出,告别繁琐账单分摊。
占卜师、管家、调酒师等 AI 角色实时引导话题与聚会游戏,让线下与线上聚餐氛围瞬间升温。
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。