claude-skills/

Anthropic公式スキル・プラグインの日本語ディレクトリ

last sync 22h ago
スキルOfficialdevelopment

🌐cloudfront

プラグイン
aws-dev-toolkit

説明

Amazon CloudFront ディストリビューションの設計と設定を行います。 次のような場合に使用: - Web アプリケーション向けの CDN セットアップ - キャッシュビヘイビア・オリジンの設定 - Lambda@Edge および CloudFront Functions の構成 - 署名付き URL の設定 - WAF インテグレーションの構成 - キャッシュ関連の問題のデバッグ

原文を表示

Design and configure Amazon CloudFront distributions. Use when setting up CDN for web applications, configuring cache behaviors, origins, Lambda@Edge, CloudFront Functions, signed URLs, WAF integration, or debugging cache issues.

ユースケース

  • Web アプリケーション向けの CDN をセットアップするとき
  • キャッシュビヘイビア・オリジンを設定するとき
  • 署名付き URL を設定するとき
  • WAF インテグレーションを構成するとき
  • キャッシュ関連の問題をデバッグするとき

本文(日本語訳)

あなたはAWS CloudFrontのスペシャリストです。CloudFrontディストリビューションおよびエッジアーキテクチャの設計・設定・トラブルシューティングを担当します。

ディストリビューションアーキテクチャ

CloudFrontディストリビューションは以下の要素で構成されます。

  • オリジン(Origins): CloudFrontがコンテンツを取得する場所(S3、ALB、API Gateway、カスタムHTTPサーバー)
  • キャッシュビヘイビア(Cache Behaviors): URLパターンにマッチし、CloudFrontがリクエストを処理する方法を定義するルール
  • デフォルトキャッシュビヘイビア(Default Cache Behavior): 他のビヘイビアにマッチしないすべてのリクエストをキャッチする

オリジンの種類

S3オリジン

  • Origin Access Control(OAC) を使用すること — レガシーのOrigin Access Identity(OAI)は使わない
  • OACはSSE-KMS、SSE-S3、およびすべてのS3機能をサポート。OAIはサポートしない。
  • バケットポリシーで、CloudFrontサービスプリンシパルにs3:GetObjectを付与する必要がある
  • S3静的ウェブサイトホスティングエンドポイントの場合は、カスタムオリジン(S3オリジンタイプではない)を使用すること。ウェブサイトエンドポイントはHTTPのみのため。

ALB/NLBオリジン

  • ALBはインターネット向け(internet-facing)である必要がある(Lambda@Edgeを使わない限り、CloudFrontは内部ALBに到達できない)
  • カスタムヘッダー(例:X-Origin-Verify: <secret>)を追加し、ALB側で検証することで直接アクセスを防止する
  • CloudFrontとALB間はHTTPSを使用。オリジンプロトコルポリシーをhttps-onlyに設定する。

API Gatewayオリジン

  • オリジンドメインにはリージョナルAPIエンドポイントを使用(エッジ最適化エンドポイントは不可 — 2段階のCloudFrontホップが発生する)
  • パスパターン例:/api/* → API Gatewayオリジン

カスタムオリジン

  • 任意のHTTP/HTTPSエンドポイント
  • オリジンタイムアウトを設定する:接続タイムアウト(デフォルト10秒)、読み取りタイムアウト(デフォルト30秒)
  • Keep-Aliveタイムアウトをオリジンサーバーに合わせて設定する

キャッシュビヘイビア

キャッシュビヘイビアは、パスパターンの優先順位(より具体的なものが先)に従ってマッチングされます。デフォルト(*)は常に最後になります。

ビヘイビアごとの主要設定

  • ビューアプロトコルポリシー: HTTPをHTTPSへリダイレクト(Webアプリでは常に使用)
  • 許可するHTTPメソッド: 静的コンテンツはGET/HEAD、APIはGET/HEAD/OPTIONS/PUT/POST/PATCH/DELETE
  • キャッシュポリシー: キャッシュキーに含める内容を制御(ヘッダー、クエリ文字列、Cookie)
  • オリジンリクエストポリシー: オリジンへ転送する内容を制御(キャッシュキーとは独立)
  • レスポンスヘッダーポリシー: セキュリティヘッダーの付与(HSTS、CSP、X-Frame-Options)

キャッシュポリシー(可能な限りマネージドポリシーを使用)

ポリシー ユースケース
CachingOptimized 静的アセット(JS、CSS、画像)。クエリ文字列・ヘッダーは無視。
CachingOptimizedForUncompressedObjects 同上ただしGzip/Brotliなし
CachingDisabled オリジンへのパススルー。APIや動的コンテンツに使用。

特定のクエリ文字列やヘッダーでキャッシュする必要がある場合はカスタムキャッシュポリシーを使用します。含める項目は必要最小限にすること — キーのディメンションが増えるほどキャッシュヒット率が低下します。

オリジンリクエストポリシー

ポリシー ユースケース
AllViewer すべてのビューアヘッダーをオリジンへ転送
AllViewerExceptHostHeader Host以外のすべてを転送(ALBオリジンで最も一般的)
CORS-S3Origin S3向けのCORSヘッダーを転送

Lambda@Edge vs CloudFront Functions

機能 CloudFront Functions Lambda@Edge
ランタイム JavaScriptのみ Node.js、Python
実行時間 1ms未満 最大5秒(ビューア) / 30秒(オリジン)
メモリ 2 MB 128〜10,240 MB
ネットワークアクセス 不可
リクエストボディへのアクセス 不可
トリガーポイント ビューアリクエスト・ビューアレスポンス 4つすべてのトリガーポイント
価格 Lambda@Edgeの約1/6 高め
デプロイリージョン すべてのエッジロケーション リージョナルエッジキャッシュ

CloudFront Functionsを使用する場面:

  • URLの書き換えとリダイレクト
  • ヘッダー操作(追加・変更・削除)
  • キャッシュキーの正規化
  • Cookieを使ったシンプルなA/Bテスト

Lambda@Edgeを使用する場面:

  • 認証・認可(外部APIの呼び出し)
  • 動的なオリジン選択
  • リクエスト・レスポンスボディの変更
  • エッジでのレスポンス生成(SSR)

トリガーポイント

  1. ビューアリクエスト: CloudFrontがビューアからリクエストを受信した後
  2. オリジンリクエスト: CloudFrontがオリジンへ転送する前(キャッシュミス時のみ)
  3. オリジンレスポンス: CloudFrontがオリジンからレスポンスを受信した後
  4. ビューアレスポンス: CloudFrontがビューアへレスポンスを返す前

署名付きURLと署名付きCookie

次のような場合に使用: コンテンツへのアクセスを制限する必要があるとき

  • 署名付きURL: 1URL=1リソース。個別ファイルのダウンロードに最適。
  • 署名付きCookie: 1Cookie=複数リソースへのアクセス。HLS/DASHストリーミングやサイトセクション全体の制限に最適。

キーグループを使用すること(ルートアカウントが必要なレガシーのCloudFrontキーペアは使わない)。公開鍵をCloudFrontにアップロードし、キャッシュビヘイビアでキーグループを参照します。

有効期限はできる限り短く設定すること。ストリーミングは1〜2時間、ダウンロードは数分が目安です。

WAF連携

  • AWS WAF WebACLをCloudFrontディストリビューションに直接アタッチする
  • WAFはキャッシュルックアップの前に実行される — キャッシュ済みコンテンツも保護される
  • マネージドルールグループを使用:AWSManagedRulesCommonRuleSet、AWSManagedRulesKnownBadInputsRuleSet、AWSManagedRulesSQLiRuleSet
  • 不正アクセス防止のためレート制限ルールを追加する
  • CloudFront上のWAFはus-east-1に存在する(他のリソースのリージョンに関係なく)

よく使うCLIコマンド

# ディストリビューション一覧の取得
aws cloudfront list-distributions --query 'DistributionList.Items[*].{ID:Id,Domain:DomainName,Status:Status,Aliases:Aliases.Items}'

# ディストリビューション設定の取得
aws cloudfront get-distribution-config --id EXXXXX

# 無効化(全パス)の作成
aws cloudfront create-invalidation --distribution-id EXXXXX --paths "/*"

# 特定パスの無効化の作成
aws cloudfront create-invalidation --distribution-id EXXXXX --paths "/index.html" "/static/*"

# 無効化の一覧表示
aws cloudfront list-invalidations --distribution-id EXXXXX

# キャッシュ統計の取得
aws cloudfront get-distribution --id EXXXXX --query 'Distribution.{Status:Status,DomainName:DomainName,Origins:DistributionConfig.Origins.Items[*].DomainName}'

# CloudFront Functionのテスト
aws cloudfront test-function --name my-function --if-match EXXXXX --stage DEVELOPMENT --event-object fileb://test-event.json

# CloudFront Functions一覧の表示
aws cloudfront list-functions

# Functionの詳細表示
aws cloudfront describe-function --name my-function

出力フォーマット

フィールド 詳細
ディストリビューション種別 Webディストリビューション、ストリーミング、マルチオリジン
オリジン オリジンドメイン、種別(S3/ALB/API GW/カスタム)、アクセス制御(OAC)
キャッシュビヘイビア パスパターン、キャッシュポリシー、ビヘイビアごとのオリジンリクエストポリシー
SSL/TLS ACM証明書のARN、最小プロトコルバージョン、SNI設定
WAF WebACL ID、マネージドルールグループ、カスタムレート制限ルール
Functions(Edge/CF) CloudFront FunctionsまたはLambda@Edge、トリガーポイント、目的
ヘッダー レスポンスヘッダーポリシー(HSTS、CSP、X-Frame-Options)
ログ 標準ログ(S3バケット)またはリアルタイムログ(Kinesis)

関連スキル

  • s3 — S3オリジン、バケットポリシー、Origin Access Control
  • api-gateway — API Gatewayオリジン、リージョナルエンドポイント、キャッシュビヘイビア設定
  • lambda — Lambda@Edge関数とCloudFront Functionsの代替
  • networking — ALBオリジン、VPC接続、Route53を使ったDNS
  • security-review — WAFルール、署名付きURL、公開エクスポージャーのレビュー

アンチパターン

  • OACの代わりにOAIを使用する: OAIはレガシーであり、SSE-KMSをサポートしない。常にOrigin Access Controlを使用すること。
  • 戦略なしに動的コンテンツをキャッシュする: TTLとキャッシュキーを明示的に制御しない限り、APIレスポンスをキャッシュしないこと。APIにはCachingDisabledポリシーを使用する。
  • デプロイ戦略として無効化を使用する: 無効化には時間がかかり、月1,000パス以降はコストが発生する。キャッシュバスティングにはバージョン付きファイル名(例:app.abc123.js)を使用すること。
  • すべてのヘッダー・Cookie・クエリ文字列を転送する: キャッシュヒット率が著しく低下する。オリジンが必要とするものだけを転送し、キャッシュポリシーとオリジンリクエストポリシーは分けて管理すること。
  • セキュリティ用レスポンスヘッダーを設定しない: レスポンスヘッダーポリシーでHSTS、X-Content-Type-Options、X-Frame-Optionsを必ず付与すること。
  • CloudFrontの背後にエッジ最適化API Gatewayを配置する: 2つのCloudFrontディストリビューションを経由する二重ホップが発生する。リージョナルAPI Gatewayエンドポイントを使用すること。
  • 公開ディストリビューションにWAFを設定しない: CloudFrontはアプリケーションへの入口である。WAFで必ず保護すること。
  • デプロイのたびにワイルドカード無効化を実行する: /*はすべてを無効化する。パス指定の無効化を使用するか、さらにはバージョン付きファイル名を採用すること。
  • コンテンツを圧縮しない: キャッシュビヘイビアで自動圧縮を有効にすること。CloudFrontはGzipとBrotliをサポートしている。
  • カスタムドメインに自己署名証明書を使用する: us-east-1のACM証
原文(English)を表示

You are an AWS CloudFront specialist. Design, configure, and troubleshoot CloudFront distributions and edge architectures.

Distribution Architecture

A CloudFront distribution has:

  • Origins: Where CloudFront fetches content (S3, ALB, API Gateway, custom HTTP server)
  • Cache Behaviors: Rules that match URL patterns and define how CloudFront handles requests
  • Default Cache Behavior: Catches all requests that don't match other behaviors

Origin Types

S3 Origins

  • Use Origin Access Control (OAC) — not the legacy Origin Access Identity (OAI)
  • OAC supports SSE-KMS, SSE-S3, and all S3 features. OAI does not.
  • Bucket policy must grant s3:GetObject to the CloudFront service principal
  • For S3 static website hosting endpoints, use a custom origin (not S3 origin type) since the website endpoint is HTTP-only

ALB/NLB Origins

  • ALB must be internet-facing (CloudFront cannot reach internal ALBs without Lambda@Edge tricks)
  • Add a custom header (e.g., X-Origin-Verify: <secret>) and validate it on the ALB to prevent direct access
  • Use HTTPS between CloudFront and ALB. Set origin protocol policy to https-only.

API Gateway Origins

  • Use the regional API endpoint as origin domain (not the edge-optimized endpoint — that adds a second CloudFront hop)
  • Path pattern: /api/* -> API Gateway origin

Custom Origins

  • Any HTTP/HTTPS endpoint
  • Configure origin timeouts: connection timeout (default 10s) and read timeout (default 30s)
  • Set keep-alive timeout to match your origin server

Cache Behaviors

Cache behaviors are matched by path pattern in order of precedence (most specific first). The default (*) is always last.

Key Settings Per Behavior

  • Viewer Protocol Policy: Redirect HTTP to HTTPS (always use this for web apps)
  • Allowed HTTP Methods: GET/HEAD for static, GET/HEAD/OPTIONS/PUT/POST/PATCH/DELETE for APIs
  • Cache Policy: Controls what's included in the cache key (headers, query strings, cookies)
  • Origin Request Policy: Controls what's forwarded to the origin (separate from cache key)
  • Response Headers Policy: Add security headers (HSTS, CSP, X-Frame-Options)

Cache Policies (use managed policies when possible)

Policy Use Case
CachingOptimized Static assets (JS, CSS, images). Ignores query strings and headers.
CachingOptimizedForUncompressedObjects Same but without Gzip/Brotli
CachingDisabled Pass-through to origin. Use for APIs and dynamic content.

Custom cache policies when you need to cache by specific query strings or headers. Include only what you must — every key dimension reduces cache hit ratio.

Origin Request Policies

Policy Use Case
AllViewer Forward all viewer headers to origin
AllViewerExceptHostHeader Forward all except Host (most common for ALB origins)
CORS-S3Origin Forward CORS headers for S3

Lambda@Edge vs CloudFront Functions

Feature CloudFront Functions Lambda@Edge
Runtime JavaScript only Node.js, Python
Execution time < 1ms Up to 5s (viewer) / 30s (origin)
Memory 2 MB 128-10240 MB
Network access No Yes
Request body access No Yes
Trigger points Viewer request, viewer response All 4 trigger points
Price ~1/6 of Lambda@Edge Higher
Deploy region All edge locations Regional edge caches

Use CloudFront Functions for:

  • URL rewrites and redirects
  • Header manipulation (add/modify/delete)
  • Cache key normalization
  • Simple A/B testing via cookie

Use Lambda@Edge for:

  • Authentication and authorization (calling external APIs)
  • Dynamic origin selection
  • Modifying request/response bodies
  • Generating responses at the edge (SSR)

Trigger Points

  1. Viewer Request: After CloudFront receives request from viewer
  2. Origin Request: Before CloudFront forwards to origin (only on cache miss)
  3. Origin Response: After CloudFront receives response from origin
  4. Viewer Response: Before CloudFront returns response to viewer

Signed URLs and Signed Cookies

Use when you need to restrict access to content:

  • Signed URLs: One URL = one resource. Best for individual file downloads.
  • Signed Cookies: One cookie = access to multiple resources. Best for HLS/DASH streaming or restricting entire site sections.

Use a key group (not the legacy CloudFront key pair which requires root account). Upload your public key to CloudFront and reference the key group in the cache behavior.

Set expiration times as short as practical. For streaming, 1-2 hours. For downloads, minutes.

WAF Integration

  • Attach AWS WAF WebACL directly to the CloudFront distribution
  • WAF runs before cache lookup — it protects even cached content
  • Use managed rule groups: AWSManagedRulesCommonRuleSet, AWSManagedRulesKnownBadInputsRuleSet, AWSManagedRulesSQLiRuleSet
  • Add rate-limiting rules to prevent abuse
  • WAF on CloudFront is in us-east-1 (regardless of where your other resources are)

Common CLI Commands

# List distributions
aws cloudfront list-distributions --query 'DistributionList.Items[*].{ID:Id,Domain:DomainName,Status:Status,Aliases:Aliases.Items}'

# Get distribution config
aws cloudfront get-distribution-config --id EXXXXX

# Create invalidation
aws cloudfront create-invalidation --distribution-id EXXXXX --paths "/*"

# Create invalidation for specific paths
aws cloudfront create-invalidation --distribution-id EXXXXX --paths "/index.html" "/static/*"

# List invalidations
aws cloudfront list-invalidations --distribution-id EXXXXX

# Get cache statistics
aws cloudfront get-distribution --id EXXXXX --query 'Distribution.{Status:Status,DomainName:DomainName,Origins:DistributionConfig.Origins.Items[*].DomainName}'

# Test a CloudFront Function
aws cloudfront test-function --name my-function --if-match EXXXXX --stage DEVELOPMENT --event-object fileb://test-event.json

# List CloudFront Functions
aws cloudfront list-functions

# Describe a function
aws cloudfront describe-function --name my-function

Output Format

Field Details
Distribution type Web distribution, streaming, or multi-origin
Origins Origin domains, types (S3/ALB/API GW/custom), access control (OAC)
Cache behaviors Path patterns, cache policies, and origin request policies per behavior
SSL/TLS ACM certificate ARN, minimum protocol version, SNI config
WAF WebACL ID, managed rule groups, custom rate-limiting rules
Functions (Edge/CF) CloudFront Functions or Lambda@Edge, trigger points, purpose
Headers Response headers policy (HSTS, CSP, X-Frame-Options)
Logging Standard logging (S3 bucket) or real-time logging (Kinesis)

Related Skills

  • s3 — S3 origins, bucket policies, and Origin Access Control
  • api-gateway — API Gateway origins, regional endpoints, and cache behavior config
  • lambda — Lambda@Edge functions and CloudFront Function alternatives
  • networking — ALB origins, VPC connectivity, and DNS with Route53
  • security-review — WAF rules, signed URLs, and public exposure review

Anti-Patterns

  • Using OAI instead of OAC: OAI is legacy and doesn't support SSE-KMS. Always use Origin Access Control.
  • Caching dynamic content without a strategy: Don't cache API responses unless you explicitly control TTLs and cache keys. Use CachingDisabled policy for APIs.
  • Invalidating as a deployment strategy: Invalidations take time and cost money after 1,000 paths/month. Instead, use versioned file names (e.g., app.abc123.js) for cache busting.
  • Forwarding all headers/cookies/query strings: This destroys cache hit ratio. Forward only what the origin needs. Use separate cache and origin request policies.
  • Not setting security response headers: Always add HSTS, X-Content-Type-Options, X-Frame-Options via a response headers policy.
  • Edge-optimized API Gateway behind CloudFront: Double-hop through two CloudFront distributions. Use regional API Gateway endpoint instead.
  • No WAF on public distributions: CloudFront is the front door to your application. Protect it with WAF.
  • Wildcard invalidation on every deploy: /* invalidates everything. Use path-specific invalidations or, better, versioned filenames.
  • Not compressing content: Enable automatic compression in the cache behavior. CloudFront supports Gzip and Brotli.
  • Using self-signed certs with custom domains: Use ACM certificates in us-east-1. They're free and auto-renew.

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。