Amazon API Gateway
API の入口
REST/HTTP/WebSocket API を作成・管理します。認証、レート制限、キャッシュを行い、request を Lambda や他のバックエンドに転送します。
- コスト$ 安い
- 難易度●●●●●
- 未使用でも課金?いいえ
- サーバーレス
- 出題試験SAA · DVA · SOA
たとえるならビルの受付のようなものです。カードを確認し、記帳し、正しい部屋へ案内します。
API Gateway はシステムのどこにある?
具体例でステップごとに
- フード注文のモバイルアプリで、メニューの表示と注文のための API が必要です。
- API Gateway で HTTP API を作り、GET /menu と POST /orders がそれぞれ Lambda 関数を呼び出すようにします。
- ユーザーは Cognito でサインインし、API Gateway はリクエストを通す前にトークンを確認します。
- 1 人あたり毎秒数十リクエストまでに制限し、API に大量のリクエストを送りつけられないようにします。
- 深夜はほとんどリクエストがないのでほぼ無料で、昼のピーク時は API が自動で負荷に対応します。
いつ使う?
✓ 使うべき場面
認証とレート制限が組み込まれた serverless API が必要な場合。
✗ 避けるべき場面
コンテナ/EC2 への非常に大きなトラフィック → 通常は ALB の方が安い。
よくある使い方
- Lambda 向けの REST/HTTP API
- チャットやリアルタイム通知向けの WebSocket
- パートナー向け API ゲートウェイ: API key、usage plan、レート制限
API Gateway・ELB・Lambda の違い
| 役割 | API の窓口:認証、制限、変換 | インスタンス/コンテナへのトラフィック分散 | リクエストを処理するコードを実行 |
|---|---|---|---|
| 組み込みの認証 | Cognito、IAM、Lambda オーソライザー、API キー | OIDC/Cognito(ALB) | なし(自分で実装) |
| 課金 | リクエスト単位 | 時間料金 + キャパシティユニット | リクエスト数 + 実行時間 |
| 向いている用途 | サーバーレス API、多数のクライアント | EC2/ECS への非常に大量のトラフィック | API Gateway の後ろのロジック |
料金はいくら?
- 課金方式: HTTP API 約 $1.00/100 万 request、REST API 約 $3.50/100 万、WebSocket はメッセージ数 + 接続時間(分)で課金、キャッシュは時間課金。
- 無料枠: 旧アカウント: 12 か月間 100 万 request/月。
HTTP API で十分なのに REST API を使う(3.5 倍高い)。dev 環境でキャッシュを有効にする。コンテナへの非常に大きなトラフィックなら ALB の方が安い。
Lambda の前に置くサーバーレス API なら API Gateway です。大量アクセスの防止やクォータ制のプラン販売ならスロットリングと API キー付きの使用量プラン、別ドメインからブラウザで呼ぶとブロックされるなら CORS を有効にします。
本番の試験問題に挑戦
1A company hosts its static website by using Amazon S3. The company wants to add a contact form to its webpage. The contact form will have dynamic server-side components for users to input their name, email address, phone number, and user message. The company anticipates that there will be fewer than 100 site visits each month. Which solution will meet these requirements MOST cost-effectively?
- AHost the dynamic contact form in Amazon Elastic Container Service (Amazon ECS). Set up Amazon Simple Email Service (Amazon SES) to connect to a third-party email provider.
- BCreate an Amazon API Gateway endpoint that returns the contact form from an AWS Lambda function. Configure another Lambda function on the API Gateway to publish a message to an Amazon Simple Notification Service (Amazon SNS) topic.
- CHost the website by using AWS Amplify Hosting for static content and dynamic content. Use server-side scripting to build the contact form. Configure Amazon Simple Queue Service (Amazon SQS) to deliver the message to the company.
- DMigrate the website from Amazon S3 to Amazon EC2 instances that run Windows Server. Use Internet Information Services (IIS) for Windows Server to host the webpage. Use client-side scripting to build the contact form. Integrate the form with Amazon WorkMail.
答えを見る
正解: B
月 100 回未満のアクセスなら、従量課金のサーバーレスを選びます。フォームの処理は API Gateway + Lambda、通知の送信は SNS です。常時動く ECS や EC2 は、誰も使っていない間も料金がかかります。
2A recently created startup built a three-tier web application. The front end has static content. The application layer is based on microservices. User data is stored as JSON documents that need to be accessed with low latency. The company expects regular traffic to be low during the first year, with peaks in traffic when it publicizes new features every month. The startup team needs to minimize operational overhead costs. What should a solutions architect recommend to accomplish this?
- AUse Amazon S3 static website hosting to store and serve the front end. Use AWS Elastic Beanstalk for the application layer. Use Amazon DynamoDB to store user data.
- BUse Amazon S3 static website hosting to store and serve the front end. Use Amazon Elastic Kubernetes Service (Amazon EKS) for the application layer. Use Amazon DynamoDB to store user data.
- CUse Amazon S3 static website hosting to store and serve the front end. Use Amazon API Gateway and AWS Lambda functions for the application layer. Use Amazon DynamoDB to store user data.
- DUse Amazon S3 static website hosting to store and serve the front end. Use Amazon API Gateway and AWS Lambda functions for the application layer. Use Amazon RDS with read replicas to store user data.
答えを見る
正解: C
静的なフロントエンドは S3、処理は API Gateway + Lambda、JSON データは DynamoDB。すべてサーバーレスで従量課金なので、毎月のピークにも自動で対応します。EKS や Elastic Beanstalk では運用すべきサーバーが残ります。
よくある質問
REST API、HTTP API、WebSocket API の違いは何ですか?
HTTP API は安くて速く、Lambda や HTTP バックエンドを呼ぶほとんどの API に十分です。REST API には API キーと使用量プラン、リクエストの検証、キャッシュ、AWS サービスとの直接統合などの機能が追加されています。WebSocket API はチャットのような双方向の接続に使います。
API Gateway の料金はどのように決まりますか?
API リクエスト数(HTTP API は REST API より安い)と外部へのデータ転送量で課金されます。REST API でキャッシュを有効にすると時間料金が追加されます。WebSocket API はメッセージ数と接続時間(分)で課金されます。
AWS 認定試験では API Gateway について何が問われますか?
スロットリングと使用量プラン、Cognito、Lambda オーソライザー、IAM による認証、CORS の有効化、ステージとステージ変数、キャッシュ、統合のタイムアウト制限などです。