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