

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon API Gateway を Amazon SQS と統合して非同期 REST API を処理する
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis"></a>

*Amazon Web Services、Natalia Colantonio Favero および Gustavo Martim*

## 概要
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis-summary"></a>

REST API をデプロイするときに、クライアントアプリケーションが公開できるメッセージキューの公開が必要となる場合があります。例えば、サードパーティー API のレイテンシーや応答の遅延の問題が発生したり、データベースクエリの応答時間を回避したり、同時実行 API が多数ある場合にサーバーのスケーリングを回避したりすることが必要となる場合があります。このようなシナリオでは、キューに公開するクライアントアプリケーションは、API がデータを受信したことだけを認識していればよく、データの受信後に何が起こるかを認識する必要はありません。

このパターンでは、[Amazon API Gateway](https://aws.amazon.com/api-gateway/) を使用して [Amazon Simple Queue Service (Amazon SQS)](https://aws.amazon.com/sqs/) にメッセージを送信することで、REST API エンドポイントを作成します。これにより、SQS キューへの直接アクセスを回避しながら、2 つのサービス間の統合を簡単に実装できるようになります。

## 前提条件と制限事項
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis-prereqs"></a>
+ [アクティブな AWS アカウント](https://portal.aws.amazon.com/billing/signup/iam)

## アーキテクチャ
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis-architecture"></a>

![API Gateway を Amazon SQS と統合するためのアーキテクチャ](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/images/pattern-img/70984dee-e49f-4446-9d52-49ce826c3909/images/737ba0b2-da8f-4478-8c54-0a4835fd69f9.png)


次の図は、これらのステップを示しています。

1. Postman などのツール、別の API、またはその他のテクノロジーを使用して、POST REST API エンドポイントをリクエストします。

1. API Gateway は、リクエストの本文で受信したメッセージをキューにポストします。

1. Amazon SQS はメッセージを受信し、成功または失敗のコードを含む応答を API Gateway に送信します。

## ツール
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis-tools"></a>
+ [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) は、任意のスケールで REST、HTTP、WebSocket API を作成、公開、維持、監視、保護する上で役立ちます。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) は、誰を認証し、誰に使用する権限を付与するかを制御することで、 AWS リソースへのアクセスを安全に管理するのに役立ちます。
+ 「[Amazon Simple Queue Service (Amazon SQS)](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html)」は、安全で耐久性があり、配信ソフトウェアシステムとコンポーネントを統合および分離できる利用可能なホスト型キューを提供します。  

## エピック
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis-epics"></a>

### SQS キューを作成する
<a name="create-an-sqs-queue"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| キューを作成する。 | REST API からメッセージを受信する SQS キューを作成するには:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 

### Amazon SQS へのアクセスを提供する
<a name="provide-access-to-sqs"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| IAM ロールを作成します。 | この IAM ロールは、API Gateway リソースに Amazon SQS へのフルアクセスを付与します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者、AWS 管理者 | 

### REST API を作成する
<a name="create-a-rest-api"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| REST API を作成する | これは、HTTP リクエストが送信される REST API です。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 
| API Gateway を Amazon SQS に接続します。 | このステップにより、メッセージが HTTP リクエストの本文内から Amazon SQS に流れるようになります。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 

### REST API をテストする
<a name="test-the-rest-api"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| REST API をテストします。 | 不足している設定がないかをチェックするテストを実行します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 
| API 統合を変更して、リクエストを Amazon SQS に適切に転送します。 | 統合エラーを修正するには、設定を完了します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 
| Amazon SQS でメッセージをテストして検証します。 | テストを実行し、テストが正常に完了したことを確認します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 
| 特殊文字を使用して API Gateway をテストします。 | メッセージでは許容されない特殊文字 (& など) を含むテストを実行します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html)<br />This is because special characters aren't supported by default in the message body. 次のステップでは、特殊文字をサポートするように API Gateway を設定します。コンテンツタイプの変換の詳細については、「[API Gateway ドキュメント](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-workflow.html)」を参照してください。 | アプリ開発者 | 
| 特殊文字をサポートするように API 設定を変更します。 | メッセージ内の特殊文字を許容するように設定を調整します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html)<br />新しいメッセージには特殊文字を含める必要があります。 | アプリ開発者 | 

### REST API をデプロイする
<a name="deploy-the-rest-api"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| API をデプロイします。 |  <br />REST API をデプロイするには、以下を行います。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 
| 外部ツールを使用してテストします。 | 外部ツールを使用してテストを実行し、メッセージが正常に受信されたことを確認します。[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | アプリ開発者 | 

### クリーンアップ
<a name="clean-up"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| API を削除します。 | [API Gateway コンソール](https://console.aws.amazon.com/apigateway/)で、作成した API を選択し、**[削除]** を選択します。 | アプリ開発者 | 
| IAM ロールを削除します。 | [IAM コンソール](https://console.aws.amazon.com/iam/)の **[ロール]** ペインで、**AWSGatewayRoleForSQS** を選択し、**[削除]** を選択します。 | アプリ開発者 | 
| SQS キューを削除します。 | [Amazon SQS コンソール](https://console.aws.amazon.com/sqs/)の **[キュー]** ペインで、作成した SQS キューを選択し、**[削除]** を選択します。 | アプリ開発者 | 

## 関連リソース
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis-resources"></a>
+ [SQS-SendMessage](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-aws-services-reference.html#SQS-SendMessage) (API Gateway ドキュメント)
+ [API Gateway でのコンテンツタイプの変換](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-workflow.html) (API Gateway ドキュメント)
+ [$util 変数](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#util-template-reference) (API Gateway ドキュメント)
+ [API Gateway REST API を Amazon SQS と統合して一般的なエラーを解決する方法 (](https://repost.aws/knowledge-center/api-gateway-rest-api-sqs-errors)AWS re:Post 記事)