

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將 Amazon API Gateway 與 Amazon SQS 整合，以處理非同步 REST APIs
<a name="integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis"></a>

*Natalia Colantonio Favero 和 Gustavo Martim，Amazon Web Services*

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

部署 REST APIs 時，有時您需要公開用戶端應用程式可以發佈的訊息佇列。例如，您可能會遇到第三方 APIs 延遲和回應延遲的問題，或者您可能想要避免資料庫查詢的回應時間，或避免在有大量並行 APIs時擴展伺服器。在這些情況下，發佈到佇列的用戶端應用程式只需要知道 API 收到資料，而不是收到資料之後會發生什麼情況。

此模式使用 [Amazon API Gateway](https://aws.amazon.com/api-gateway/) 將訊息傳送至 Amazon Simple Queue Service (Amazon SQS) 來建立 REST API 端點。 [Amazon SQS](https://aws.amazon.com/sqs/) 它會在兩個服務之間建立easy-to-implement的整合，以避免直接存取 SQS 佇列。

## 先決條件和限制
<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)

## Architecture
<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/zh_tw/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 APIs。
+ [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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立佇列。 | 若要建立接收來自 REST API 訊息的 SQS 佇列：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 IAM 角色。 | 此 IAM 角色可讓 API Gateway 資源完整存取 Amazon SQS。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 REST API。 | 這是傳送 HTTP 請求的 REST API。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 測試 REST API。 | 執行測試以檢查缺少的組態：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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/zh_tw/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/zh_tw/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/zh_tw/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html)<br />這是因為訊息內文中預設不支援特殊字元。在下一個步驟中，您將設定 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/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 API。 |  <br />若要部署 REST API：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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/zh_tw/prescriptive-guidance/latest/patterns/integrate-amazon-api-gateway-with-amazon-sqs-to-handle-asynchronous-rest-apis.html) | 應用程式開發人員 | 

### 清除
<a name="clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除 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 文章）