CloudFront では、エッジロケーションでコードを実行してリクエスト/レスポンスをカスタマイズできる
2つの方法がある:
- CloudFront Functions
- Lambda@Edge
CloudFront Functions
CloudFront のエッジロケーションで軽量な JavaScript 関数を実行できるサービス
出典: Customize at the edge with CloudFront Functions
“With CloudFront Functions, you can write lightweight functions in JavaScript for high-scale, latency-sensitive CDN customizations. Your functions can manipulate the requests and responses that flow through CloudFront, perform basic authentication and authorization, generate HTTP responses at the edge, and more.”
なぜ必要か
CDN でコンテンツを配信する際、リクエストやレスポンスをカスタマイズしたい場面がある。これらをオリジンサーバーで処理すると遅延が発生するため、エッジで処理することで高速化できる
出典: Customize at the edge with CloudFront Functions
“The CloudFront Functions runtime environment offers submillisecond startup times, scales immediately to handle millions of requests per second, and is highly secure.”
主なユースケース
出典: Differences between CloudFront Functions and Lambda@Edge
| ユースケース | 説明 |
|---|---|
| キャッシュキーの正規化 | リクエスト属性を変換してキャッシュヒット率を向上 |
| ヘッダー操作 | リクエスト/レスポンスのHTTPヘッダーを追加・変更・削除 |
| URLリダイレクト/リライト | リクエスト情報に基づいてリダイレクトやパス書き換え |
| リクエスト認証 | JWTなどの認証トークンを検証 |
“Cache key normalization – Transform HTTP request attributes (headers, query strings, cookies, and even the URL path) to create an optimal cache key, which can improve your cache hit ratio.”
“Request authorization – Validate hashed authorization tokens, such as JSON web tokens (JWT), by inspecting authorization headers or other request metadata.”
Lambda@Edge
CloudFront Functions より高機能なエッジコンピューティングサービス
外部ネットワークアクセスや複雑な処理が必要な場合に使用する
出典: Differences between CloudFront Functions and Lambda@Edge
“Lambda@Edge is an extension of AWS Lambda that provides powerful and flexible computing for complex functions and full application logic closer to viewers.”
CloudFront Functions vs Lambda@Edge
両方とも「エッジで処理を実行する」点は同じだが、用途が違う
- CloudFront Functions: 軽くて速い、単純な処理向け
- Lambda@Edge: 重くて遅いが、複雑な処理が可能
以下は Differences between CloudFront Functions and Lambda@Edge の比較表を日本語化したもの
| 項目 | CloudFront Functions | Lambda@Edge |
|---|---|---|
| 言語 | JavaScript (ES 5.1) | Node.js, Python |
| 処理時間 | サブミリ秒 | 最大30秒 |
| スケール | 毎秒数百万リクエスト | リージョンあたり毎秒10,000リクエスト |
| コードサイズ | 最大10KB | 最大50MB |
| ネットワークアクセス | 不可 | 可能 |
| ファイルシステムアクセス | 不可 | 可能 |
| リクエストボディ参照 | 不可 | 可能 |
| イベントソース | Viewer request/response のみ | Origin request/response も可 |
“CloudFront Functions is ideal for lightweight, short-running functions”
“Lambda@Edge is ideal for… Functions that require network access to use external services for processing”
どっちを使うべきか
CloudFront Functions を選ぶケース:
- ヘッダーの追加・削除
- URLのリダイレクト
- JWTトークンの検証(外部通信不要な場合)
- クエリパラメータの正規化
Lambda@Edge を選ぶケース:
- 外部APIを呼んで認証する
- DynamoDB からデータを取得する
- 画像をリサイズする
- リクエストボディを解析する
処理タイミングの違い
ユーザー ──> [CloudFront エッジ] ──> [Regional Edge Cache] ──> オリジン
│ │
├─ Viewer Request ├─ Origin Request
│ (CF Functions ✓) │ (Lambda@Edge のみ)
│ (Lambda@Edge ✓) │
│ │
├─ Viewer Response ├─ Origin Response
│ (CF Functions ✓) │ (Lambda@Edge のみ)
│ (Lambda@Edge ✓) │
CloudFront Functions は Viewer イベントのみ対応。Origin イベント(オリジンとの通信前後)で処理したい場合は Lambda@Edge が必須
出典: Differences between CloudFront Functions and Lambda@Edge
“Event sources: CloudFront Functions - Viewer request, Viewer response / Lambda@Edge - Viewer request, Viewer response, Origin request, Origin response”