In this Azure and Terraform blog post, we’ll show how to Deploy Azure OpenAI Resource and Model with Terraform.
Terraform is an open-source infrastructure as a code software tool that enables you to safely and predictably create, change, and improve infrastructure.
Below is the Terraform code needed to achieve this. We’ll create a resource group, an Azure OpenAI resource, and deploy a model.
- Install Terraform: Before you begin, ensure you have Terraform installed on your machine.
- Provider Configuration: The first step in the Terraform script is to configure the Azure provider. This allows Terraform to interact with your Azure subscription.
- Create a Resource Group: Resource groups in Azure are logical containers where Azure resources are deployed and managed.
- Create an Azure OpenAI Resource: This resource represents the Azure Cognitive Service’s OpenAI instance.
- Deploy a Model: Finally, we’ll deploy the GPT-4 model on the Azure OpenAI resource. Here, you specify the model and the scale settings.
How to Deploy Azure OpenAI Resource and Model with Terraform
Use the code below to deploy the resource and model.
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "rg-terraform-ai"
location = "east US"
}
resource "azurerm_cognitive_account" "example" {
name = "aoi-terraform-ai"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku_name = "S0"
kind = "OpenAI"
}
resource "azurerm_cognitive_deployment" "example" {
name = "gpt4"
cognitive_account_id = azurerm_cognitive_account.example.id
model {
format = "OpenAI"
name = "gpt-4"
#version = "0613" # you can set a model version or remove for default
}
scale {
type = "Standard"
}
}
Applying the Configuration
To apply this Terraform configuration, follow these steps:
- Initialize Terraform: Run
terraform init
to initialize your working directory containing the Terraform configuration files. This step is required to download the provider specified in the configuration.erraform init
- Plan the Deployment: Run
terraform plan
to create an execution plan. This command helps you see the changes that Terraform will make to your infrastructure.terraform plan
- Apply the Configuration: Finally, run
terraform apply
to create the resources defined in your configuration.terraform apply
Confirm the operation by typingyes
when prompted.