在 Azure 上創建新資源有許多方法。 Bicep 檔案提供一種宣告式、模組化且可重用的方法來定義 Azure 資源。 Bicep 範本與基於 JSON 的 ARM 範本或像 PowerShell 和 Azure CLI 這類命令式腳本方法相比,提供更佳的可讀性、可維護性,並能與 Azure 資源管理員 (ARM) 進行原生整合。 Azure 的 GitHub Copilot 可以幫助你建立 Bicep 檔案。
生成 Bicep 檔案
如果您剛開始使用 Bicep 文件且對語法感到陌生,可能需要學習一下,不過像 Azure 的 GitHub Copilot 這樣的工具可以提供幫助。
範例案例
假設您想要一種簡便的方法來創建重複且自動化的流程,以便建立 Blob 容器和文件共享。 您可能熟悉如何使用 Azure 入口網站來建立 Blob 容器和檔案共用,但可能不確定如何使用 Bicep 文件來完成相同的任務。
範例提示
建立新聊天 (Ctrl + L),然後將以下提示複製並貼上到 GitHub Copilot 的聊天面板中。
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 文件。
Where in my workspace should I create the Azure Bicep file, and what should I call it?
可能會回應並為您的新 Bicep 檔案建議資料夾和檔案名稱。 例如: infra/storageAccount.bicep 。
最後,如果您不確定如何部署 Bicep 文件,您可以建立一個提示來要求指示。
How do I execute the Azure 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 。