Select Page

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).

Login to Azure using Azure PowerShell

Create a resource group

After creating a resource group, we are almost ready to deploy the Bicep template. Copy the template to a file called main.bicep.

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.

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.