

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

# 使用 Visual Studio 的 CloudFormation 範本編輯器
<a name="tkv-cfn-editor"></a>

Toolkit for Visual Studio 包含 Visual Studio 的 CloudFormation 範本編輯器和 CloudFormation 範本專案。支援的功能包括：
+ 使用提供的範本專案類型建立新的 CloudFormation 範本 （空白或從現有堆疊或範本複製）。
+ 使用自動 JSON 驗證、自動完成、程式碼摺疊和語法反白來編輯範本。
+ 自動建議範本中欄位值的內部函數和資源參考參數。
+ 從 Visual Studio 為範本執行常見動作的選單項目。

**Topics**
+ [在 Visual Studio 中建立 CloudFormation 範本專案](tkv-cfn-editor-new-project.md)
+ [在 Visual Studio 中部署 CloudFormation 範本](tkv-cfn-editor-deploy-template.md)
+ [在 Visual Studio 中格式化 CloudFormation 範本](tkv-cfn-editor-format.md)

# 在 Visual Studio 中建立 CloudFormation 範本專案
<a name="tkv-cfn-editor-new-project"></a>

 **建立範本專案** 

1. 在 Visual Studio 中，選擇**檔案**，選擇**新增**，然後選擇**專案**。

1. **針對 Visual Studio 2017**：

   在**新增專案**對話方塊中，展開**已安裝**，然後選取 **AWS**。  
![\[New Project dialog box showing CloudFormation and Lambda Function project options.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/CreateNewProject-04-CloudFormation-VS2017.png)

   **針對 Visual Studio 2019**：

   在**新增專案**對話方塊中，確保**語言**、**平台**和**專案類型**下拉式方塊設定為「全部...」，並在**搜尋**欄位中輸入 **aws**。  
![\[Project template options for AWS 服務, including Lambda, CloudFormation, and Serverless applications.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/CreateNewProject-04-CloudFormation-VS2019.png)

1. 選取 **AWS CloudFormation 專案**範本。

1. **針對 Visual Studio 2017**：

   輸入範本專案所需的**名稱**、**位置**等，然後按一下**確定**。

   **針對 Visual Studio 2019**：

   按一下 **Next (下一步)**。在下一個對話方塊中，輸入範本專案所需的**名稱**、**位置**等，然後按一下**建立**。

1. 在**選取專案來源**頁面上，選擇您將建立的範本來源：
   +  **使用空白範本建立** 會產生新的空白 CloudFormation 範本。
   +  **從現有 AWS \$1CFN\$1 堆疊建立** 會從您 AWS 帳戶中的現有堆疊產生範本。（堆疊不需要狀態為 `CREATE_COMPLETE`。)
   +  **選取範例範本**會從其中一個 CloudFormation 範例範本產生範本。  
![\[CloudFormation project creation window with options for template source selection.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/vs-editor-new-template-empty-2.png)

1. 若要完成 CloudFormation 範本專案的建立，請選擇**完成**。

# 在 Visual Studio 中部署 CloudFormation 範本
<a name="tkv-cfn-editor-deploy-template"></a>

 **部署 CFN 範本** 

1. 在 Solution Explorer 中，開啟您要部署之範本的內容 （按一下滑鼠右鍵） 選單，然後選擇**部署。 AWS CloudFormation**  
![\[Solution Explorer showing CloudFormation template with context menu options for deployment.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/vs-editor-solution-explorer-deploy.png)

   或者，若要部署目前正在編輯的範本，請從**範本**功能表中，選擇**部署至 AWS CloudFormation** 。  
![\[Template menu options including Deploy to CloudFormation and Estimate Cost.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/vs-editor-template-menu-deploy.png)

1. 在**部署範本**頁面上，選擇要 AWS 帳戶 用來啟動堆疊的 ，以及要啟動堆疊的區域。  
![\[Deploy Template interface showing options to create or update a stack with account and region selection.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/vs-editor-cfn-deploy.png)

1. 選擇**建立新堆疊**，然後輸入堆疊的名稱。

1. 選擇下列其中任何一個選項 (或不選擇)：
   + 若要接收有關堆疊進度的通知，請從 **SNS 主題**下拉式清單中選擇 SNS 主題。您也可以選擇**建立新主題並在方塊中輸入電子郵件地址，以建立 SNS 主題**。
   + 使用**建立逾時**來指定在宣告失敗之前 CloudFormation ，應允許建立堆疊的時間長度 （並復原，除非已清除**轉返失敗**選項）。
   + 如果您希望堆疊在**失敗時轉返** （也就是刪除本身），請在失敗時使用轉返。如果您希望堆疊保持作用中狀態以進行偵錯，即使堆疊無法完成啟動，也請保持清除此選項。

1. 選擇**完成**以啟動堆疊。

# 在 Visual Studio 中格式化 CloudFormation 範本
<a name="tkv-cfn-editor-format"></a>
+ 在 Solution Explorer 中，開啟範本的內容 （按一下滑鼠右鍵） 選單，然後選擇**格式範本**。

  或者，若要格式化目前正在編輯的範本，請從**範本**功能表中選擇**格式範本**。  
![\[Dropdown menu showing options to deploy to CloudFormation, estimate cost, and format template.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/vs-editor-template-menu-format.png)

  您的 JSON 程式碼將會格式化，以清楚顯示其結構。  
![\[JSON code structure for CloudFormation template with UserData and Properties sections.\]](http://docs.aws.amazon.com/zh_tw/toolkit-for-visual-studio/latest/user-guide/images/vs-editor-cfn-formatting.png)