有很多方法可以在 Azure 上创建新资源。 Bicep 文件提供了一种声明性、模块化和可重用的方法来定义 Azure 资源。 与基于 JSON 的 ARM 模板或 PowerShell 和 Azure CLI 等命令性脚本方法相比,Bicep 模板可提供更好的可读性和可维护性,并且可更好地与 Azure 资源管理器 (ARM) 本机集成。 GitHub Copilot for Azure 可以帮助你创建 Bicep 文件。
生成 Bicep 文件
如果你不熟悉语法,使用 Bicep 文件开始操作可能需要进行一些学习,但 GitHub Copilot for Azure 等工具可以提供帮助。
示例方案
假设你想要一种简单的方法来创建一个可重复的自动化流程来创建 Blob 容器和文件共享。 你可能熟悉如何使用 Azure 门户创建 Blob 容器和文件共享,但不确定如何使用 Bicep 文件来完成相同的任务。
示例提示
创建一个新的聊天 (Ctrl + L),然后将以下提示复制并粘贴到 GitHub Copilot 的聊天窗格中。
@azure Show me a Bicep file that creates an Azure Storage account with a blob container and a file share.
示例响应
注释
以下是示例响应。 GitHub Copilot for Azure 响应是不确定的,因此你可能会收到与此处显示的响应不同的响应。
GitHub Copilot for Azure 会生成类似于以下内容的 Bicep 文件。
@description('Specifies the name of the Azure Storage account.')
param storageAccountName string
@description('Specifies the name of the blob container to create.')
param blobContainerName string
@description('Specifies the name of the file share to create.')
param fileShareName string
@description('Specifies the ___location in which the Azure Storage resources should be deployed.')
param ___location string = resourceGroup().___location
resource sa 'Microsoft.Storage/storageAccounts@2023-01-01' = {
name: storageAccountName
___location: ___location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
accessTier: 'Hot'
}
}
resource blobServices 'Microsoft.Storage/storageAccounts/blobServices@2023-01-01' = {
parent: sa
name: 'default'
}
resource blobContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' = {
parent: blobServices
name: blobContainerName
properties: {
publicAccess: 'None'
}
}
resource fileServices 'Microsoft.Storage/storageAccounts/fileServices@2023-01-01' = {
parent: sa
name: 'default'
}
resource fileShare 'Microsoft.Storage/storageAccounts/fileServices/shares@2023-01-01' = {
parent: fileServices
name: fileShareName
properties: {
shareQuota: 100 // Quota in MB
}
}
创建一个新的 main.bicep 文件,然后从 GitHub Copilot for Azure 复制响应并粘贴到新文件中。
使用 Azure CLI 执行 Bicep 文件时,可以设置声明为 storageAccountName
、blobContainerName
和 fileShareName
的参数。
或者,你可以编辑 Bicep 文件,并将 storageAccountName
参数初始化为所需的值。
param storageAccountName string = '<your-storage-account-name>'
优化回复
但是,如果不熟悉 Bicep 文件,则可能需要更多信息。 在同一聊天会话中继续提示以了解后续步骤。
例如,你可能会问应该在 VS Code 工作区中的什么位置创建此 Bicep 文件。
@azure Where in my workspace should I create the Bicep file and what should I call it?
它可能会通过为新的 Bicep 文件建议文件夹和文件名来做出响应。 例如,infra/storageAccount.bicep
。
最后,如果你不确定如何部署 Bicep 文件,则可以创建一个提示来询问说明。
@azure How do I execute the Bicep file?
你可能会收到类似于下面的响应。
To execute the Bicep file, you can use the Azure CLI with the following steps:
1. **Install Azure CLI and Bicep CLI**: Ensure you have the Azure CLI and Bicep CLI installed. You can install Bicep using the Azure CLI with the command:
az bicep install
2. **Log in to Azure**: Use the Azure CLI to log in to your Azure account:
az login
3. **Deploy the Bicep file**: Use the `az deployment group create` command to deploy the Bicep file to a resource group. Replace `<resource-group-name>` with your resource group name and `<___location>` with your desired Azure region:
az deployment group create --resource-group <resource-group-name> --template-file infra/storageAccount.bicep --parameters storageAccountName=<your-storage-account-name> containerName=<your-container-name> fileShareName=<your-file-share-name> ___location=<___location>
This command will deploy the resources defined in your Bicep file to the specified resource group in Azure.
按照这些说明操作时,如果你遇到困难或问题,请继续向 GitHub Copilot 提问,并在聊天窗格中创建带有前缀 @azure
的提示。