下面帶你速覽「價格表 → 6 大省錢技巧 → 可運行代碼 → KPI 看板」,送 AI 提效外掛,復制就能跑!
| 類型 | 免費層 | 超出后單價 | 計費粒度 | 關鍵提示 |
|---|---|---|---|---|
| HTTP API | 100 萬/月 | $0.90/百萬次 | 512 KB | 比 REST 便宜 ~70% |
| REST API | 100 萬/月 | $3.50/百萬次 | 512 KB | 功能最全 |
| WebSocket | 750 小時/月 | $0.25/百萬分鐘 + $0.60/百萬消息 | 32 KB | 消息按塊計費 |
| 緩存 | — | $0.02/小時-GB | 小時 | 1 GB 起跳 |
| DTO | — | $0.09/GB | 公網出流 | CloudFront 可降本 |
把「API 成本占比」「DTO 流量」量化成 KPI?用 開發任務管理系統KPI 一鍵生成可衡量指標。
| 技巧 | 操作要點 | 預計節省 |
|---|---|---|
| ① 選 HTTP API | 除非必須用 REST 功能 | 70% |
| ② 服務代理直連 | API → DynamoDB/SQS/SNS,跳過 Lambda | 30–50% |
| ③ Cognito + JS SDK | 前端直連 Lambda(跳過網關) | 網關費用 ≈ 0 |
| ④ ALB 替代 | 不需要緩存/限流/鑒權時 | 50–60% |
| ⑤ 減少冗余調用 | 批量、緩存、去重 | 20–40% |
| ⑥ CloudFront 加速 | 緩存邊緣+壓縮,降低 DTO | 15–25% |
場景:前端上傳日志 → API Gateway → Kinesis Firehose → S3
省錢點:無 Lambda 運行時長
template.yaml(SAM)
Resources:
HttpApi:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: UploadLogs
ProtocolType: HTTP
Integration:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref HttpApi
IntegrationType: AWS_PROXY
IntegrationUri: !Sub "arn:aws:apigateway:${AWS::Region}:firehose:action/PutRecord"
PayloadFormatVersion: "1.0"
RequestTemplates:
application/json: |
{
"DeliveryStreamName": "upload-stream",
"Record": { "Data": "$util.base64Encode($input.json('$'))" }
}
Route:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref HttpApi
RouteKey: POST /logs
Target: !Sub Integration/${Integration}
| 成本對比 | 方案 | 每月 1000 萬次 | 費用 |
|---|---|---|---|
| REST + Lambda 512 ms | 1000 萬 × $3.50 + 512 GB-s | ~$180 | |
| HTTP 直連 Firehose | 1000 萬 × $0.90 | ~$90 | |
| 節省 | ≈ 50% |
架構:用戶登錄 → Cognito → 臨時 IAM 憑據 → 前端直接調用 Lambda(InvokeFunction)
index.html
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
const lambda = new LambdaClient({
region: "eu-west-1",
credentials: fromCognitoIdentityPool({
clientConfig: { region: "eu-west-1" },
identityPoolId: "eu-west-1:xxx"
})
});
async function callLambda(payload) {
const cmd = new InvokeCommand({
FunctionName: "my-function",
Payload: JSON.stringify(payload)
});
const { Payload } = await lambda.send(cmd);
return JSON.parse(new TextDecoder().decode(Payload));
}
結果:API Gateway 調用次數 → 0,僅付 Lambda 費用。
控制臺步驟:
| 指標 | 公式 | 目標 |
|---|---|---|
| API 成本占比 | APIG 費用 / 總 AWS 費用 |
≤ 10% |
| DTO 流量環比 | (本月-上月)/上月 |
≤ 5% |
| 平均調用大小 | DTO GB / 調用次數 |
≤ 256 KB |
| 緩存命中率 | CacheHit / (CacheHit+Miss) |
≥ 80% |
用 代碼優化 自動合并重復請求,緩存命中率 ↑10%。
優化后的 API 網關 = 性能不減,賬單減半!??
原文鏈接: https://www.stormit.cloud/blog/amazon-api-gateway-pricing/