

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

# 使用 AWS SAM 自動化巢狀應用程式的部署
<a name="automate-deployment-of-nested-applications-using-aws-sam"></a>

*Rahul Sharad Gaikwad 醫生、Ishwar Chauthaiwale、Dmitry Gulin 和 Tabby Ward，Amazon Web Services*

## 總結
<a name="automate-deployment-of-nested-applications-using-aws-sam-summary"></a>

在 Amazon Web Services (AWS) 上，AWS Serverless Application Model (AWS SAM) 是一種開放原始碼架構，提供速記語法來表達函數、APIs、資料庫和事件來源映射。只要為每個資源幾行，您就可以定義所需的應用程式，並使用 YAML 建立模型。在部署期間，SAM 會將 SAM 語法轉換並擴展為 AWS CloudFormation 語法，您可以用來更快速地建置無伺服器應用程式。

AWS SAM 可簡化 AWS 平台上無伺服器應用程式的開發、部署和管理。它提供標準化架構、更快速的部署、本機測試功能、資源管理、與 開發工具的無縫整合，以及支援社群。這些功能使其成為有效建置無伺服器應用程式的寶貴工具。

此模式使用 AWS SAM 範本來自動化巢狀應用程式的部署。巢狀應用程式是另一個應用程式中的應用程式。父應用程式會呼叫其子應用程式。這些是無伺服器架構鬆散耦合的元件。 

使用巢狀應用程式，您可以重複使用獨立撰寫和維護但使用 AWS SAM 和 Serverless Application Repository 組成的服務或元件，快速建置高度複雜的無伺服器架構。巢狀應用程式可協助您建置功能更強大的應用程式，避免重複的工作，並確保團隊和組織的一致性和最佳實務。為了示範巢狀應用程式， 模式會部署[範例 AWS 無伺服器購物車應用程式](https://github.com/aws-samples/aws-sam-nested-stack-sample)。

## 先決條件和限制
<a name="automate-deployment-of-nested-applications-using-aws-sam-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 現有的虛擬私有雲端 (VPC) 和子網路
+ 整合的開發環境，例如 Visual Studio Code （如需詳細資訊，請參閱[在 AWS 上建置的工具](https://aws.amazon.com/getting-started/tools-sdks/#IDE_and_IDE_Toolkits))
+ 如果尚未安裝 Python wheel 程式庫，請使用 pip 安裝 wheel 安裝

**限制**
+ 可在無伺服器應用程式中巢狀化的應用程式數量上限為 200。
+ 巢狀應用程式的參數數目上限可以有 60 個。

**產品版本**
+ 此解決方案建置在 AWS SAM 命令列界面 (AWS SAM CLI) 1.21.1 版上，但此架構應與更新的 AWS SAM CLI 版本搭配使用。

## Architecture
<a name="automate-deployment-of-nested-applications-using-aws-sam-architecture"></a>

**目標技術堆疊**
+ Amazon API Gateway
+ AWS SAM
+ Amazon Cognito
+ Amazon DynamoDB
+ AWS Lambda
+ Amazon Simple Queue Service (Amazon SQS) 佇列

**目標架構**

下圖顯示使用者如何透過呼叫 APIs 向購物服務提出請求。使用者的請求，包括所有必要的資訊，會傳送至 Amazon API Gateway 和 Amazon Cognito 授權方，該授權方會執行 APIs 的身分驗證和授權機制。

在 DynamoDB 中新增、刪除或更新項目時，會將事件放入 DynamoDB Streams，進而啟動 Lambda 函數。為了避免在同步工作流程中立即刪除舊項目，訊息會放入 SQS 佇列，這會啟動工作者函數來刪除訊息。

![從 API Gateway 到 Lambda 函數到 DynamoDB 和產品服務的 POST 和 PUT 操作。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/218adecc-b5b8-4193-9012-b5d584e2e128/images/5b454bae-5fd4-405d-a37d-6bafc3fcf889.png)


在此解決方案設定中，AWS SAM CLI 做為 AWS CloudFormation 堆疊的界面。AWS SAM 範本會自動部署巢狀應用程式。父 SAM 範本會呼叫子範本，而父 CloudFormation 堆疊會部署子堆疊。每個子堆疊都會建置 AWS SAM CloudFormation 範本中定義的 AWS 資源。

![使用 AWS SAM CLI 搭配父系和三個子系 CloudFormation 堆疊的四步驟程序。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/218adecc-b5b8-4193-9012-b5d584e2e128/images/5828026e-72ad-4a3f-a5f2-bffac0f13e42.png)


1. 建置和部署堆疊。

1. Auth CloudFormation 堆疊包含 Amazon Cognito。

1. 產品 CloudFormation 堆疊包含 Lambda 函數和 Amazon API Gateway

1. 購物 CloudFormation 堆疊包含 Lambda 函數、Amazon API Gateway、SQS 佇列和 Amazon DynamoDB 資料庫。

## 工具
<a name="automate-deployment-of-nested-applications-using-aws-sam-tools"></a>

**工具**
+ [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) 可協助您建立、發佈、維護、監控和保護任何規模的 REST、HTTP 和 WebSocket APIs。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在 AWS 帳戶和區域的整個生命週期中管理這些資源。
+ [Amazon Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html) 為 Web 和行動應用程式提供身分驗證、授權和使用者管理。
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一項全受管 NoSQL 資料庫服務，可提供快速、可預期且可擴展的效能。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，可協助您執行程式碼，而無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Serverless Application Model (AWS SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) 是一種開放原始碼架構，可協助您在 AWS 雲端中建置無伺服器應用程式。
+ [Amazon Simple Queue Service (Amazon SQS)](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html) 提供安全、耐用且可用的託管佇列，可協助您整合和分離分散式軟體系統和元件。

**Code**

此模式的程式碼可在 GitHub [AWS SAM 巢狀堆疊範例](https://github.com/aws-samples/aws-sam-nested-stack-sample)儲存庫中使用。

## 史詩
<a name="automate-deployment-of-nested-applications-using-aws-sam-epics"></a>

### 安裝 AWS SAM CLI
<a name="install-aws-sam-cli"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 安裝 AWS SAM CLI。 | 若要安裝 AWS SAM CLI，請參閱 [AWS SAM 文件](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)中的說明。 | DevOps 工程師 | 
| 設定 AWS 登入資料。 | 若要設定 AWS 登入資料，讓 AWS SAM CLI 可以代表您呼叫 AWS 服務，請執行 `aws configure`命令並依照提示操作。<pre>$aws configure<br />AWS Access Key ID [None]: <your_access_key_id><br />AWS Secret Access Key [None]: your_secret_access_key<br />Default region name [None]:<br />Default output format [None]:</pre>如需設定登入資料的詳細資訊，請參閱[身分驗證和存取登入資料](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-authentication.html)。 | DevOps 工程師 | 

### 初始化 AWS SAM 專案
<a name="initialize-the-aws-sam-project"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製 AWS SAM 程式碼儲存庫。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-deployment-of-nested-applications-using-aws-sam.html) | DevOps 工程師 | 
| 部署 範本以初始化專案。 | 若要初始化專案，請執行 `SAM init`命令。當系統提示您選擇範本來源時，請選擇 `Custom Template Location`。 | DevOps 工程師 | 

### 編譯和建置 SAM 範本程式碼
<a name="compile-and-build-the-sam-template-code"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢閱 AWS SAM 應用程式範本。 | 檢閱巢狀應用程式的範本。此範例使用以下巢狀應用程式範本：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-deployment-of-nested-applications-using-aws-sam.html) | DevOps 工程師 | 
| 檢閱父範本。 | 檢閱將調用巢狀應用程式範本的範本。在此範例中，父範本為 `template.yml`。所有個別的應用程式都會巢狀在單一父範本 中`template.yml`。 | DevOps 工程師 | 
| 編譯並建置 AWS SAM 範本程式碼。 | 使用 AWS SAM CLI，執行下列命令。<pre>sam build</pre> | DevOps 工程師 | 

### 部署 AWS SAM 範本
<a name="deploy-the-aws-sam-template"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署應用程式。 | 若要啟動建立巢狀應用程式 CloudFormation 堆疊並在 AWS 環境中部署程式碼的 SAM 範本程式碼，請執行下列命令。<pre>sam deploy --guided --stack-name shopping-cart-nested-stack --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND</pre>命令將提示幾個問題。使用 回答所有問題`y`。 | DevOps 工程師 | 

### 驗證部署
<a name="verify-the-deployment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證堆疊。 | 若要檢閱 AWS CloudFormation 範本中定義的 AWS CloudFormation 堆疊和 AWS 資源，請執行下列動作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-deployment-of-nested-applications-using-aws-sam.html) | DevOps 工程師 | 

## 相關資源
<a name="automate-deployment-of-nested-applications-using-aws-sam-resources"></a>

**參考**
+ [AWS Serverless 應用程式模型 (AWS SAM)](https://aws.amazon.com/serverless/sam/#:~:text=The%20AWS%20Serverless%20Application%20Model,and%20model%20it%20using%20YAML.)
+ [GitHub 上的 AWS SAM](https://github.com/aws/serverless-application-model)
+ [無伺服器購物車微服務](https://github.com/aws-samples/aws-serverless-shopping-cart) (AWS 範例應用程式）

**教學課程和影片**
+ [建置無伺服器應用程式](https://youtu.be/Hv3YrP8G4ag)
+ [AWS Online Tech Talks：使用 AWS SAM 進行無伺服器應用程式建置和部署](https://youtu.be/1NU7vyJw9LU)

## 其他資訊
<a name="automate-deployment-of-nested-applications-using-aws-sam-additional"></a>

所有程式碼都就緒後，範例會有下列目錄結構：
+ [sam\_stacks](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html) – 此資料夾包含 `shared.py` layer。layer 是檔案封存，其中包含程式庫、自訂執行時間或其他相依性。透過 layer，您可以在函數中使用程式庫，而無需將其包含在部署套件中。
+ *product-mock-service* – 此資料夾包含所有產品相關的 Lambda 函數和檔案。
+ *shopping-cart-service* – 此資料夾包含所有與購物相關的 Lambda 函數和檔案。