

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

# 使用 Infrastructure Composer 建置您的第一個應用程式
<a name="getting-started-build"></a>

在本教學課程中，您會使用 AWS Infrastructure Composer 建置建立、讀取、更新和刪除 (CRUD) 無伺服器應用程式，以管理資料庫中的使用者。

在本教學課程中，我們會在 中使用 Infrastructure Composer AWS 管理主控台。我們建議您使用 Google Chrome或 Microsoft Edge，以及全螢幕瀏覽器視窗。

**您是第一次使用無伺服器嗎？**  
我們建議您對下列主題有基本的了解：  
[事件驅動型架構](what-is-concepts.md#what-is-concepts-terms-eda)
[基礎設施即程式碼 (IaC)](what-is-concepts.md#what-is-concepts-terms-iac)
[無伺服器技術](what-is-concepts.md#what-is-concepts-terms-serverless)
如需詳細資訊，請參閱 [的無伺服器概念 AWS Infrastructure Composer](what-is-concepts.md)。

**Topics**
+ [資源屬性參考](#getting-started-build-reference)
+ [步驟 1：建立您的專案](#getting-started-build-start)
+ [步驟 2：將卡片新增至畫布](#getting-started-build-rest-cards)
+ [步驟 3：設定您的 API Gateway REST API](#getting-started-build-rest)
+ [步驟 4：設定 Lambda 函數](#getting-started-build-functions)
+ [步驟 5：連接您的卡片](#getting-started-build-connect)
+ [步驟 6：整理畫布](#getting-started-build-organize)
+ [步驟 7：新增和連接 DynamoDB 資料表](#getting-started-build-table)
+ [步驟 8：檢閱您的 AWS CloudFormation 範本](#getting-started-build-template)
+ [步驟 9：整合到您的開發工作流程](#getting-started-build-integrate)
+ [後續步驟](#getting-started-build-next)

## 資源屬性參考
<a name="getting-started-build-reference"></a>

建置應用程式時，請使用此表格做為參考，以設定 Amazon API Gateway AWS Lambda 和資源的屬性。


| Method | 路徑 | 函數名稱 | 
| --- | --- | --- | 
|  GET  |  /項目  |  getItems  | 
|  GET  |  /items/\$1id\$1  |  getItem  | 
|  PUT  |  /items/\$1id\$1  |  updateItem  | 
|  POST  |  /項目  |  addItem  | 
|  DELETE  |  /items/\$1id\$1  |  deleteItem  | 

## 步驟 1：建立您的專案
<a name="getting-started-build-start"></a>

若要開始使用 CRUD 無伺服器應用程式，請在 Infrastructure Composer 中建立新的專案，並啟用**本機同步**。

**建立新的空白專案**

1. 登入 [Infrastructure Composer 主控台](https://console.aws.amazon.com/composer/home)。

1. 在**首頁**上，選擇**建立專案**。

如下圖所示， Infrastructure Composer 會開啟視覺化畫布，並載入啟動 （空白） 應用程式範本。

![\[具有空白視覺化畫布的 Infrastructure Composer。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_01.png)


**啟用本機同步**

1. 從基礎設施編寫器**功能表中**，選取**儲存** > **啟用本機同步**。  
![\[選取啟用本機同步的基礎設施編寫器功能表。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_09.png)

1. 針對**專案位置**，按下**選取資料夾**，然後選擇目錄。這是 Infrastructure Composer 會在您設計時儲存和同步範本檔案和資料夾的位置。

   專案位置不得包含現有的應用程式範本。
**注意**  
**本機同步**需要支援檔案系統存取 API 的瀏覽器。如需詳細資訊，請參閱[Data Infrastructure Composer 可以存取](reference-fsa.md#reference-fsa-access)。

1. 出現允許存取的提示時，請選取**檢視檔案**。

1. 按下**啟用**以開啟**本機同步**。出現儲存變更的提示時，請選取**儲存變更**。

   啟用時，**Autosave** 指標會顯示在畫布的左上方區域。

## 步驟 2：將卡片新增至畫布
<a name="getting-started-build-rest-cards"></a>

開始使用增強型元件卡來設計應用程式架構，從 API Gateway REST API 和五個 Lambda 函數開始。

**將 API Gateway 和 Lambda 卡新增至畫布**

從**資源**面板的**增強型元件**區段下，執行下列動作：

1. 將 **API Gateway** 卡拖曳到畫布上。

1. 將 **Lambda 函數**卡拖曳到畫布上。重複此步驟，直到您將五張 **Lambda 函數**卡新增至畫布。

![\[具有一個 API Gateway 和五個 Lambda 函數卡的 Infrastructure Composer 畫布檢視。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_02.png)


## 步驟 3：設定您的 API Gateway REST API
<a name="getting-started-build-rest"></a>

接著，在您的 API Gateway 卡中新增五個路由。

**將路由新增至 API Gateway 卡**

1. 開啟 **API Gateway** 卡**的資源屬性**面板。若要開啟面板，請按兩下卡片。或者，選取卡片，然後選擇**詳細資訊**。

1. 在**資源屬性**面板的 **Routes** 下，執行下列動作：
**注意**  
對於下列每個路由，請使用[資源屬性參考表中](#getting-started-build-reference)指定的 HTTP 方法和路徑值。

   1. 針對**方法**，選擇指定的 HTTP 方法。例如，**GET**。

   1. 針對**路徑**，輸入指定的路徑。例如 **/items**。

   1. 選擇 **Add route** (新增路由)。

   1. 重複上述步驟，直到您新增所有五個指定的路由為止。

1. 選擇**儲存**。

![\[Infrastructure Composer 視覺化畫布搭配具有五個路由的 API Gateway 資源。Resource properties 面板會顯示方法、路徑和新增路由的選項。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_03.png)


## 步驟 4：設定 Lambda 函數
<a name="getting-started-build-functions"></a>

依[資源屬性參考表中](#getting-started-build-reference)所指定，為五個 Lambda 函數命名。

**命名 Lambda 函數**

1. 開啟 **Lambda 函數**卡**的資源屬性**面板。若要開啟面板，請按兩下卡片。或者，選取卡片，然後選擇**詳細資訊**。

1. 在**資源屬性**面板中，針對**邏輯 ID**，輸入指定的函數名稱。例如 **getItems**。

1. 選擇**儲存**。

1. 重複上述步驟，直到您已命名所有五個函數。

![\[Infrastructure Composer 視覺化畫布，具有五個名為 Lambda 函數的資源卡。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_04.png)


## 步驟 5：連接您的卡片
<a name="getting-started-build-connect"></a>

將 **API Gateway** 卡上的每個路由連接到其相關的 **Lambda 函數**卡，如[資源屬性參考表](#getting-started-build-reference)所指定。

**連接您的卡片**

1. 按一下 **API Gateway** 卡上的右連接埠，並將其拖曳至指定 **Lambda 函數**卡的左連接埠。例如，按一下 **GET /items** 連接埠，並將其拖曳至 **getItems** 的左側連接埠。

1. 重複上述步驟，直到您已將 **API Gateway** 卡上的全部五個路由連接到對應的 **Lambda 函數**卡。

![\[Infrastructure Composer 視覺化畫布，其中 REST API 連接到五個 Lambda 函數。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_05.png)


## 步驟 6：整理畫布
<a name="getting-started-build-organize"></a>

透過將 Lambda 函數分組並排列所有卡來組織視覺化畫布。

**將函數分組**

1. 按住 **Shift**，然後在畫布上選取每個 **Lambda 函數**卡。

1. 選擇**群組**。

**為您的 群組命名**

1. 按兩下群組名稱 (**群組**) 附近的群組頂端。

   **群組屬性**面板隨即開啟。

1. 在**群組屬性**面板的**群組名稱**中，輸入 **API**。

1. 選擇**儲存**。

**安排您的卡片**

在畫布的主檢視區域上方，選擇**安排**。

Infrastructure Composer 會排列並對齊視覺化畫布上的所有卡片，包括您的新群組 (**API**)，如下所示：

![\[Infrastructure Composer 視覺化畫布，將所有 Lambda 函數分組在一起。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_06.png)


## 步驟 7：新增和連接 DynamoDB 資料表
<a name="getting-started-build-table"></a>

現在，將 DynamoDB 資料表新增至應用程式架構，並將其連接至 Lambda 函數。

**新增和連接 DynamoDB 資料表**

1. 從資源調色盤 (**資源**) 的**增強元件**區段下，將 **DynamoDB Table** 卡拖曳到畫布上。

1. 按一下 **Lambda 函數**卡上的右連接埠，並將其拖曳至 **DynamoDB 資料表**卡的左連接埠。

1. 重複上一個步驟，直到您將所有五個 **Lambda 函數**卡連接到 **DynamoDB 資料表**卡。

1. （選用） 若要重新組織和重新對齊畫布上的卡片，請選擇**排列**。

![\[Infrastructure Composer 視覺化畫布，具有連接至群組 API 的 DynamoDB 資料表。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_07.png)


## 步驟 8：檢閱您的 AWS CloudFormation 範本
<a name="getting-started-build-template"></a>

恭喜您！您已成功設計準備好進行部署的無伺服器應用程式。最後，選擇**範本**以檢閱 Infrastructure Composer 為您自動產生的 AWS CloudFormation 範本。

在 範本中， Infrastructure Composer 已定義下列項目：
+ 此`Transform`宣告會將範本指定為 AWS Serverless Application Model (AWS SAM) 範本。如需詳細資訊，請參閱《 *AWS Serverless Application Model 開發人員指南*》中的[AWS SAM 範本結構](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html)。
+ `AWS::Serverless::Api` 資源，指定具有五個路由的 API Gateway REST API。
+ 五個`AWS::Serverless::Function`資源，指定 Lambda 函數的組態，包括其環境變數和許可政策。
+ `AWS::DynamoDB::Table` 資源，指定您的 DynamoDB 資料表及其屬性。
+ `Metadata` 區段，其中包含資源群組 (**API**) 的相關資訊。如需本節的詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[中繼資料](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html)。

![\[Infrastructure Composer 範本檢視顯示應用程式的範本程式碼。\]](http://docs.aws.amazon.com/zh_tw/infrastructure-composer/latest/dg/images/aac_t2_08.png)


## 步驟 9：整合到您的開發工作流程
<a name="getting-started-build-integrate"></a>

使用 Infrastructure Composer 建立的範本檔案和專案目錄，以進一步測試和部署。
+ 透過**本機同步**，您可以將 Infrastructure Composer 連線到本機電腦上的 IDE，以加速開發。如需詳細資訊，請參閱 [將 Infrastructure Composer 主控台與本機 IDE 連接](other-services-ide.md)。
+ 透過**本機同步**，您可以在本機電腦上使用 AWS Serverless Application Model 命令列界面 (AWS SAM CLI) 來測試和部署應用程式。如需詳細資訊，請參閱 [將您的 Infrastructure Composer 無伺服器應用程式部署至 AWS 雲端](other-services-cfn.md)。

## 後續步驟
<a name="getting-started-build-next"></a>

您現在可以使用 Infrastructure Composer 建置自己的應用程式。如需使用 Infrastructure Composer 的深入詳細資訊，請參閱 [如何在 中編寫 AWS Infrastructure Composer](using-composer-basics.md)。當您準備好部署應用程式時，請參閱 [將您的 Infrastructure Composer 無伺服器應用程式部署至 AWS 雲端](other-services-cfn.md)。