共用方式為


使用 Azure Copilot 生成 Azure 上的 Terraform 組態

Azure Copilot 可以幫助你產生定義 Azure 基礎架構的 Terraform 設定。 描述您想要部署的基礎結構,而 Copilot 會使用 AzureRM 提供者產生 Terraform 組態。 組態會自動包含主要資源和任何必要的相依性,以確保設定可部署。 您可以反覆進行後續提示來定義輸出。

在本文中,你會學習如何在 Azure 入口網站中使用 Azure Copilot ,以及在 Visual Studio Code 中使用 GitHub Copilot for Azure 。 我們也提供範例 Terraform 提示,讓您視需要使用 as-is 或編輯。

Tip

為了獲得最佳效果,請將提示設置於少於八種主要的 Terraform 資源類型。 Copilot 在一般設定中表現良好。 複雜或大規模的架構可能會產生不完整或較不精確的結果。

Note

Copilot 目前廣泛支援 AzureRM 提供者資源。 AzAPI 提供者的支援正在演進,可能尚未完全可用。 如果不支援需求的資源類型,Copilot 會退回到範例結構或解釋這些限制。

在 Azure 入口網站中使用 Azure Copilot

  1. 開啟 Azure 入口網站

  2. 選取右上角的 Copilot 圖示。

    Azure 入口網站中 Azure Copilot 圖示的螢幕快照。

  3. 輸入與 Terraform 相關的提示,例如下列範例。

    Create a Terraform configuration for a Cognitive Services instance 
    named "mycognitiveservice" and the S0 pricing tier
    
  4. <Enter> 鍵。

    範例 Azure Copilot 提示的螢幕快照。

  5. Azure Copilot 回應後,您可以選擇 「開啟完整檢視 」以全螢幕模式查看設定代碼區塊。

    Azure Copilot 全螢幕模式的截圖,顯示在 Azure 入口網站。

  6. 選取 [複製 ] 圖示,將新的設定複製到剪貼簿。

    Azure Copilot 複製圖示的螢幕快照。

  7. 將程式代碼貼到編輯器中。

從 Visual Studio Code 使用 GitHub Copilot for Azure

  1. 開啟 Visual Studio Code。

  2. 從 [活動列] 中,選取 [ 擴充功能],然後搜尋 copilot

    導覽列中 VS Code 延伸模組圖示的螢幕快照。

  3. 確定已安裝 GitHub Copilot 擴充功能。 如果沒安裝,請安裝。

  4. 確定已安裝 GitHub Copilot Chat 擴充功能。 如果沒安裝,請安裝。

  5. 選取 [切換聊天]。

    VS Code 中 Copilot 切換聊天選項的螢幕快照。

  6. 輸入以 @azure 開頭的 Terraform 組態配置指令。 例如,下列提示會建立具有各種設定的內容傳遞網路 (CDN) 資源。

    @azure Use Terraform to create an Azure CDN Front Door profile named "myCDN profile"
    with a custom domain association for "example.com". Configure a CDN Front Door route 
    that links to the default domain, and create a CDN endpoint named "myEndpoint" 
    associated with the custom domain. Ensure that a security policy is applied for 
    enhanced protection in production, and verify that the route is correctly configured
    with the appropriate origin group.
    
  7. <Enter> 鍵。

    在 VS Code 中使用 Copilot 的 Terraform 設定提示螢幕快照。

  8. 適用於 Azure 的 GitHub Copilot 會以互動方式引導您完成此程式,並建立設定所需的檔案。

    VS Code 中 Copilot 產生的檔案螢幕快照。

  9. 建立或更新檔案之後,GitHub Copilot for Azure 會針對產生的組態執行 terraform initterraform validate 命令。

    Copilot 選項執行各種 Terraform 命令的螢幕截圖。

查看和使用範例 Terraform 指令

本節包含數個可用來產生 Terraform 組態的範例提示。 根據您的案例修改這些提示,或嘗試其他提示來建立不同類型的查詢。

Create a Terraform configuration for a Cognitive Services instance with 
name "mycognitiveservice" and S0 pricing tier.
Create a Terraform configuration that deploys a Linux virtual machine 
running Ubuntu 22.04 LTS, with 8 GB of RAM. The virtual machine should 
be located in the West US region and assigned a public IP address. 
It must be connected to a virtual network that includes a subnet and is 
secured by a network security group.
Create a Terraform configuration for a Container App resource named 
"myApp" using the quick start image. Set the container app environment name 
to "awesomeAzureEnv" and the container name to "myQuickStartContainer".
Create a Terraform configuration for an Azure Databricks workspace named 
"myworkspace" with the premium SKU. The workspace should be deployed in 
the West US region.
Create a Terraform configuration for an Azure OpenAI deployment that uses 
the "gpt-4" model. Specify the model version as "2024-05-01-preview" and 
set the deployment name to "myOpenAIModel".

Tip

更多範例提示,請參見 使用 Azure Copilot 生成 Terraform 與 Bicep 配置

後續步驟