Membuat file Bicep menggunakan GitHub Copilot untuk Azure

Ada banyak cara untuk membuat sumber daya baru di Azure. File Bicep menyediakan pendekatan deklaratif, modular, dan dapat digunakan kembali untuk menentukan sumber daya Azure. Templat Bicep menawarkan peningkatan keterbacaan, keberlanjutan, dan integrasi asli dengan Azure Resource Manager (ARM) dibandingkan dengan templat ARM berbasis JSON atau metode pembuatan skrip imperatif seperti PowerShell dan Azure CLI. GitHub Copilot untuk Azure dapat membantu Anda membuat file Bicep.

Menghasilkan file Bicep

Mulai menggunakan file Bicep mungkin memerlukan beberapa pembelajaran jika Anda baru menggunakan sintaks, tetapi alat seperti GitHub Copilot for Azure dapat membantu.

Contoh skenario

Misalkan Anda menginginkan cara mudah untuk membuat proses otomatis yang dapat diulang untuk membuat kontainer Blob dan berbagi file. Anda mungkin terbiasa dengan cara membuat kontainer Blob dan berbagi file menggunakan portal Microsoft Azure, tetapi tidak yakin cara menggunakan file Bicep untuk menyelesaikan tugas yang sama.

Contoh perintah

Buat Obrolan Baru (Ctrl + L), lalu salin dan tempel perintah berikut ke panel obrolan GitHub Copilot.

Show me a Bicep file that creates an Azure Storage account with a blob container and a file share.

Contoh respons

Nota

Respons berikut adalah contohnya. GitHub Copilot untuk respons Azure bersifat nondeterministik, sehingga Anda mungkin mendapatkan respons yang berbeda dari yang ditunjukkan di sini.

GitHub Copilot untuk Azure menghasilkan file Bicep yang mirip dengan yang berikut ini.

@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
  }
}

Buat file main.bicep baru, lalu salin respons dari GitHub Copilot untuk Azure dan tempelkan ke file baru.

Saat menggunakan Azure CLI untuk menjalankan file Bicep, Anda dapat mengatur param yang dideklarasikan sebagai storageAccountName, , blobContainerNamedan fileShareName.

Atau, Anda dapat mengedit file Bicep dengan menginisialisasi parameter storageAccountName ke nilai yang Anda inginkan.

param storageAccountName string = '<your-storage-account-name>'

Memperbaiki respons

Namun, jika Anda baru menggunakan file Bicep, Anda mungkin memerlukan informasi lebih lanjut. Terus memberikan dorongan dalam sesi obrolan yang sama agar dapat memahami langkah berikutnya.

Misalnya, Anda mungkin bertanya di mana file Bicep ini harus dibuat di ruang kerja Visual Studio Code Anda.

Where in my workspace should I create the Azure Bicep file, and what should I call it?

Sistem mungkin akan merespons dengan menyarankan folder dan nama file untuk file Bicep terbaru Anda. Contohnya, infra/storageAccount.bicep.

Terakhir, jika Anda tidak yakin tentang cara menyebarkan file Bicep, Anda dapat membuat perintah yang meminta instruksi.

How do I execute the Azure Bicep file?

Anda mungkin menerima respons yang mirip dengan yang berikut ini.

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.

Saat mengikuti instruksi ini, jika Anda buntu atau menghadapi masalah, lanjutkan mengajukan pertanyaan kepada GitHub Copilot dengan membuat perintah yang termasuk kata azure di panel obrolan.