使用 Copilot 在 Azure 配置上生成 Terraform

Copilot 可帮助生成定义 Azure 基础结构的 Terraform 配置。 描述要部署的基础结构,Copilot 使用 AzureRM 提供程序生成 Terraform 配置。 配置会自动包括主要资源和任何必需的依赖项,以确保配置可部署。 可以通过迭代方式进行后续提示来定义输出。

本文介绍如何在 Azure 门户中使用 Copilot, 以及如何在 Visual Studio Code 中使用 适用于 Azure 的 GitHub Copilot 。 我们还提供示例 Terraform 提示,让你根据需要使用 as-is 或编辑。

Tip

为获得最佳结果,请将提示中的主要 Terraform 资源类型限制为少于 8 种。 Copilot 在常见配置中表现良好。 复杂的或大规模的体系结构可能会产生不完整或不太准确的结果。

Note

Copilot 目前广泛支持 AzureRM 提供者资源。 对 AzAPI 提供程序的支持正在不断发展,可能尚未完全可用。 如果不支持所需的资源类型,Copilot 要么回退到示例结构,要么解释这些限制。

在 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 门户中 Azure Copilot 全屏模式的屏幕截图。

  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. GitHub Copilot for Azure 以交互方式指导你完成此过程,并为配置创建所需的文件。

    VS Code 中 Copilot 生成的文件的屏幕截图。

  9. 创建或更新文件后,GitHub Copilot for Azure 提供针对生成的配置运行 terraform initterraform validate 命令。

    用于运行各种 Terraform 命令的 Copilot 选项的屏幕截图。

查看并使用 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".

Next steps