api-gateway について
api-gateway は、API ゲートウェイを設計する開発者向けのリファレンススキルです。AWS API Gateway、Kong、NGINX、BFF の例と運用チェック項目を掲載しています。
主な機能
- 01ゲートウェイルーティングパターン
- 02AWS API Gateway の例
- 03Kong の宣言型設定
- 04NGINX のゲートウェイ設定
- 05BFF による集約パターン
- 06本番運用チェックリスト
活用シーン
- 01バックエンドサービスへのリクエスト振り分け
- 02ゲートウェイのレート制限設定
- 03フロントエンド向けのサービスデータ集約
- 04ゲートウェイのデプロイ準備確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ claude-dev-suite/ claude-dev-suite --skill api-gatewayインストール後、Claude Code で/api-gatewayを入力して読み込まれたか確認します。
詳細
API Gateway
ゲートウェイパターン
Client ──▶ API Gateway ──┬──▶ User Service
├──▶ Order Service
├──▶ Product Service
└──▶ Payment Service
AWS API Gateway
// CDK definition
const api = new apigateway.RestApi(this, 'MyApi', {
restApiName: 'My Service',
deployOptions: { stageName: 'prod', throttlingRateLimit: 1000, throttlingBurstLimit: 500 },
});
const orders = api.root.addResource('orders');
orders.addMethod('GET', new apigateway.LambdaIntegration(listOrdersFn));
orders.addMethod('POST', new apigateway.LambdaIntegration(createOrderFn), {
authorizer: cognitoAuthorizer,
authorizationType: apigateway.AuthorizationType.COGNITO,
});
// Usage plan with API key
const plan = api.addUsagePlan('BasicPlan', {
throttle: { rateLimit: 100, burstLimit: 50 },
quota: { limit: 10000, period: apigateway.Period.MONTH },
});
Kong(宣言型設定)
# kong.yml
_format_version: "3.0"
services:
- name: user-service
url: http://user-svc:3000
routes:
- name: users-route
paths: ["/api/users"]
strip_path: true
plugins:
- name: rate-limiting
config: { minute: 100, policy: redis, redis_host: redis }
- name: jwt
- name: cors
config:
origins: ["https://myapp.com"]
methods: ["GET", "POST", "PUT", "DELETE"]
- name: order-service
url: http://order-svc:3000
routes:
- name: orders-route
paths: ["/api/orders"]
plugins:
- name: rate-limiting
config: { minute: 50 }
ゲートウェイとしてのNGINX
upstream user_service { server user-svc:3000; }
upstream order_service { server order-svc:3000; }
server {
listen 443 ssl;
location /api/users/ {
proxy_pass http://user_service/;
proxy_set_header X-Request-ID $request_id;
limit_req zone=api burst=20 nodelay;
}
location /api/orders/ {
proxy_pass http://order_service/;
proxy_set_header X-Request-ID $request_id;
}
}
BFF(フロントエンド向けバックエンド)
// BFF aggregates multiple services for the frontend
app.get('/api/bff/dashboard', auth, async (req, res) => {
const [user, orders, notifications] = await Promise.all([
userService.getProfile(req.user.id),
orderService.getRecent(req.user.id, 5),
notificationService.getUnread(req.user.id),
]);
res.json({ user, recentOrders: orders, unreadCount: notifications.length });
});
アンチパターン
| アンチパターン | 対策 |
|---|---|
| ゲートウェイにビジネスロジックを実装する | ゲートウェイはルーティング、認証、レート制限のみを担当します |
| レート制限がない | ルートごとに制限を設定します |
| 単一障害点 | ゲートウェイを冗長構成でデプロイします |
| リクエストIDが伝播されない | トレース用にX-Request-IDヘッダーを追加します |
| ゲートウェイがデータ変換を処理する | 変換処理はBFFまたはサービスで行います |
本番環境のチェックリスト
- ルートごとにレート制限を設定済み
- 認証をゲートウェイにオフロード済み
- トレース用のリクエストID伝播を設定済み
- アップストリームサービスのヘルスチェックエンドポイントを用意済み
- アップストリームの障害に対するサーキットブレーカーを設定済み
- ゲートウェイでTLSを終端
コメント
Markdown 対応