This Azure AI Services post will show how to retrieve Azure AI Services keys and Endpoint using Bicep deployment code.
If you are not familiar with Azure Bicep, it is a Specific Domain Language (DSL) for infrastructure-as-code (iac) deployments in Azure only.
Unlike other tools like Terraform, Bicep offers access to the latest Azure API from day one. By default (the only option), Bicep saves the deployment’s state files in Azure and does not use a local copy.
Retrieving Azure AI Services Keys and Endpoints Using Bicep
When deploying services to Azure that contains a security key and endpoint URL, we can use native Bicep functionality to retrieve those details after the deployment is complete.
The following Bicep code will deploy an Azure AI Services resource and output the key and Endpoint details.
Before running this code, Install Bicep on your machine using the following command (Windows only).
winget install -e --id Microsoft.Bicep
Login to Azure using Azure PowerShell
Connect-AzAccount -UseDeviceAuthentication
Create a resource group
New-AzResourceGroup -Name "RG-NAME" -Location "australiasoutheast"
After creating a resource group, we are almost ready to deploy the Bicep template. Copy the template to a file called main.bicep.
resource aiservice 'Microsoft.CognitiveServices/accounts@2023-05-01' = {
name: 'CognitiveServices'
location: 'southeastasia'
sku: {
name: 'S0'
}
kind: 'CognitiveServices'
properties: {
}
}
output cognitiveServiceEndpoint string = aiservice.properties.endpoint
output cognitiveServiceKey string = listKeys(aiservice.id, '2022-12-01').key1
After saving the file from the terminal window, open the location of the directory with the Bicep file and run the following command to deploy the service and retrieve the key and endpoint.
New-AzResourceGroupDeployment -ResourceGroupName "RG-NAME" -TemplateFile .\main.bicep
Note: In a production environment, it is not recommended that the key be outputted to the terminal. Ideally, you should use a secure string that outputs the key but does not save it in the terminal history.
Trackbacks/Pingbacks