Terraform Cloud Shell 開發Terraform Cloud Shell development

Terraform 非常適合從 macOS 終端機的 Bash 命令列,或在 Windows 或 Linux 上的 Bash 中運作。Terraform works great from a Bash command line such as macOS Terminal or Bash on Windows or Linux. Azure Cloud Shell 的 Bash 體驗中執行您的 Terraform 設定,具有可加速開發週期的一些獨特優點。Running your Terraform configurations in the Bash experience of the Azure Cloud Shell has some unique advantages to speed up your development cycle.

此概念本文章涵蓋 Cloud Shell 功能,能協助您撰寫部署至 Azure 的 Terraform 指令碼。This concepts article covers Cloud Shell features that help you write Terraform scripts that deploy to Azure.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell 可讓您使用 bashPowerShell 以與 Azure 服務搭配使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要啟動 Azure Cloud Shell:To launch Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Launch Cloud Shell.

  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上按 Ctrl+Shift+V;或在 macOS 上按 Cmd+Shift+V,將程式碼貼到 Cloud Shell工作階段中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.

  4. 按下 Enter 鍵執行程式碼。Press Enter to run the code.

自動認證設定Automatic credential configuration

Terraform 已安裝且立即可在 Cloud Shell 中使用。Terraform is installed and immediately available in Cloud Shell. 不使用任何額外的設定登入 Cloud Shell 來管理結礎結構時,使用 Azure 驗證 Terraform 指令碼。Terraform scripts authenticate with Azure when logged in to the Cloud Shell to manage infrastructure without any additional configuration. 自動驗證會略過手動建立 Active Directory 服務主體,及設定 Azure Terraform 提供者變數的必要。Automatic authentication bypasses the need to manually create an Active Directory service principal and configure the Azure Terraform provider variables.

使用模組和提供者Using Modules and Providers

Azure Terraform 模組需要認證才能存取和變更您的 Azure 訂用帳戶中的資源。Azure Terraform modules require credentials to access and make changes to the resources in your Azure subscription. 在 Cloud Shell 中工作時,會將下列程式碼新增至您的指令碼,以便在 Cloud Shell 中使用 Azure Terraform 模組:When working in the Cloud Shell, add the following code to your scripts to use Azure Terraform modules in the Cloud Shell:

# Configure the Microsoft Azure Provider
provider "azurerm" {
}

使用任何 terraform CLI 命令時,Cloud Shell 會透過環境變數傳遞 azurerm 提供者的必要值。The Cloud Shell passes required values for the azurerm provider through environment variables when using any of the terraform CLI commands.

其他 Cloud Shell 開發人員工具Other Cloud Shell developer tools

檔案和殼層狀態會保存在 Cloud Shell 工作階段之間的 Azure 儲存體中。Files and shell states persist in Azure Storage between Cloud Shell sessions. 使用 Azure 儲存體總管,將檔案從本機電腦複製並上傳至 Cloud Shell。Use Azure Storage Explorer to copy and upload files to the Cloud Shell from your local computer.

Azure CLI 隨附於 Cloud Shell,是在完成 terraform applyterraform destroy 後測試設定以及檢查工作的適用工具。The Azure CLI is available in the Cloud Shell and is a great tool for testing configurations and checking your work after a terraform apply or terraform destroy completes.

後續步驟Next steps

使用模組登錄建立小 VM 叢集 使用自訂 HCL 建立小 VM 叢集Create a small VM cluster using the Module Registry Create a small VM cluster using custom HCL