快速入門:使用 Visual Studio Code 建立 ARM 範本

適用於 Visual Studio Code 的 Azure Resource Manager 工具可提供語言支援、資源的程式碼片段和資源自動完成。 這些工具有助於建立及驗證 Azure Resource Manager 範本 (ARM 範本),因此是建議的 ARM 範本建立和設定方法。 在本快速入門中,您將使用擴充功能從頭開始建立 ARM 範本。 執行此作業時,您會用到 ARM 範本程式碼片段、驗證、完成和參數檔案支援等擴充功能。

本快速入門著重於使用 Visual Studio Code 延伸模組來建置 ARM 範本。 如需更著重語法的教學課程,請參閱教學課程:建立及部署第一個 ARM 範本

若要完成本快速入門,您需要已安裝 Azure Resource Manager 工具擴充功能Visual Studio Code。 您也需要安裝 Azure CLIAzure PowerShell 模組,並且通過驗證。

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱快速入門:使用 Visual Studio Code 建立 Bicep 檔案

注意

適用於 Visual Studio Code 的 Azure Resource Manager Tools 延伸模組目前版本無法辨識 languageVersion 2.0 中所做的增強功能。

建立 ARM 範本

使用 Visual Studio Code 建立並開啟名為 azuredeploy.json 的新檔案。 在程式碼編輯器中輸入 arm,以起始用於建立 ARM 範本 Scaffolding 的 Azure Resource Manager 程式碼片段。

選取 arm! 以建立 Azure 資源群組部署範圍內的範本。

Screenshot showing Azure Resource Manager scaffolding snippets.

此程式碼片段會建立 ARM 範本的基本構成要素。

Screenshot showing a fully scaffolded ARM template.

請注意,Visual Studio Code 語言模式已從 JSON 變更為 Azure Resource Manager 範本。 延伸模組包含 ARM 範本特有的語言伺服器,可提供範本專屬的驗證、完成和其他語言服務。

Screenshot showing Azure Resource Manager as the Visual Studio Code language mode.

新增 Azure 資源

擴充功能包含許多適用於 Azure 資源的程式碼片段。 這些程式碼片段可讓您輕鬆地將資源新增至範本部署。

將游標放在範本的 resources 區塊中,輸入 storage,然後選取 [arm-storage] 程式碼片段。

Screenshot showing a resource being added to the ARM template.

此動作會將儲存體資源新增至範本。

Screenshot showing an Azure Storage resource in an ARM template.

您可以使用 tab 鍵來瀏覽儲存體帳戶上可設定的屬性。

Screenshot showing how the tab key can be used to navigate through resource configuration.

完成和驗證

擴充功能最強大的功能之一,就是與 Azure 結構描述整合。 Azure 結構描述提供具有驗證和資源感知完成功能的擴充功能。 讓我們修改儲存體帳戶,以查看驗證和完成如何進行。

首先,將儲存體帳戶種類更新為不正確的值,例如 megaStorage。 請注意,此動作會產生一則警告,指出 megaStorage 不是有效的值。

Screenshot showing an invalid storage configuration.

若要使用完成功能,請移除 megaStorage,將游標放在雙引號內,然後按 ctrl + space。 此動作會顯示有效值的自動完成清單。

Screenshot showing extension auto-completion.

新增範本參數

現在,請建立並使用參數來指定儲存體帳戶名稱。

將游標放置於 parameters 區塊中、新增歸位字元、輸入 ",然後選取 new-parameter 程式碼片段。 此動作會將泛型參數新增至範本。

Screenshot showing a parameter being added to the ARM template.

將參數名稱更新為 storageAccountName,並將描述更新為 Storage Account Name

Screenshot showing the completed parameter in an ARM template.

Azure 儲存體帳戶名稱的長度最少為 3 個字元,最多為 24 個字元。 將 minLengthmaxLength 一起新增至參數,並提供適當的值。

Screenshot showing minLength and maxLength being added to an ARM template parameter.

現在,在儲存體資源上,將 name 屬性更新為使用該參數。 若要這麼做,請移除目前的名稱。 輸入雙引號和左方括弧 [,這會產生 ARM 範本函式的清單。 從清單中,選取 [參數]

Screenshot showing auto-completion when using parameters in ARM template resources.

在括弧內輸入單引號 ' 會產生範本中定義的所有參數清單,在此案例中為 storageAccountName。 選取參數。

Screenshot showing completed parameter in an ARM template resource.

建立參數檔案

ARM 範本參數檔案可讓您儲存環境特有的參數值,並在部署階段以群組的形式傳遞這些值。 例如,您可以有一個包含測試環境特有值的參數檔案,以及另一個用於生產環境的參數檔案。

擴充功能可讓您輕鬆地從現有範本建立參數檔案。 若要這麼做,請在程式碼編輯器中的範本上按一下滑鼠右鍵,然後選取 [Select/Create Parameter File]。

Screenshot showing the right-click process for creating a parameter file from an ARM template.

選取 [New] > [All Parameters] > 選取參數檔案的名稱和位置。

此動作會建立新的參數檔案,並將其與建立該檔案的範本對應。 選取範本時,您可以在 Visual Studio Code 狀態列中查看和修改目前的範本/參數檔案對應。

Screenshot showing the template/parameter file mapping in the Visual Studio Code status bar.

既然參數檔案已對應至範本,接著延伸模組便會同時驗證範本和參數檔案。 若要查看此驗證實務,請將兩個字元的值新增至參數檔案中的 storageAccountName 參數,並儲存檔案。

Screenshot showing an invalidated template due to parameter file issue.

瀏覽回 ARM 範本,您會看到指出值不符合參數準則的錯誤。

Screenshot showing a valid ARM template.

將值更新為適當的內容,儲存檔案,然後瀏覽回範本。 您會發現參數上的錯誤已解決。

部署範本

使用 ctrl + ` 按鍵組合開啟整合式 Visual Studio Code 終端,並使用 Azure CLI 或 Azure PowerShell 模組來部署範本。

az group create --name arm-vscode --location eastus

az deployment group create --resource-group arm-vscode --template-file azuredeploy.json --parameters azuredeploy.parameters.json

清除資源

如果不再需要 Azure 資源,請使用 Azure CLI 或 Azure PowerShell 模組來刪除快速入門資源群組。

az group delete --name arm-vscode

下一步