Leveraging Azure OpenAI with Terraform

Leveraging Azure OpenAI with Terraform

In today's rapidly evolving technological landscape, the integration of Artificial Intelligence (AI) capabilities has become a crucial aspect for businesses. OpenAI, a renowned leader in AI research and development, has joined forces with Microsoft to offer its powerful features through Azure, Microsoft's cloud computing platform.

In this article, we will explore how you can leverage Terraform, an Infrastructure as Code (IaC) tool, to swiftly build infrastructure that utilizes Azure OpenAI services. Whether you are familiar with Terraform or already have experience working with Azure, this guide will provide you with the necessary steps to get started.


How to install Terraform

To use Terraform, you need some requirements Azure CLI installed and the terraform program. To install these tools, please follows the instructions in these urls

Azure CLI

Terraform

Once you have installed the Azure CLI, you will need to retrieve essential values such as the tenant ID and subscription ID. To obtain these values, simply execute the following command in your terminal:

$ az auth login        

Let's begin by examining a command that enables you to export a Terraform code based on the list of resources associated with a resource group, allowing you to seamlessly integrate your existing Azure resources into your Terraform infrastructure."

Which commands to know

  • terraform init : This command is used to initialize a Terraform project by setting up the working directory, downloading provider plugins, and configuring backend settings.
  • terraform apply: This command is used to apply the changes defined in your Terraform configuration files, provisioning or modifying the infrastructure resources accordingly.
  • terraform plan: This command is used to generate an execution plan that previews the changes Terraform will make to the infrastructure, allowing you to review and validate the modifications before applying them.
  • terraform destroy: The "terraform destroy" command is used to tear down and remove all the infrastructure resources provisioned by Terraform, effectively dismantling the entire environment created by the Terraform configuration files.

Infra to terraform

Generally, before writing code in Terraform, it is advisable to have already tested and deployed your infrastructure and implemented your services using the Azure portal. If this is the case, you can save a significant amount of time by exporting your Azure resources into Terraform code.


One tool that can assist you in this process is aztfexport, with just a single command, you can generate Terraform code based on your existing Azure resources. Once you have obtained the exported code, it becomes straightforward to adapt and integrate it into your existing architecture and codebase.


$ aztfexport rg <your_resource_group_name>        

Once this command line executed, this tool generated main.tf ready to use.


How to re-use your code

I have developed a collection of free modules specifically designed to simplify the implementation of future infrastructures on Azure. These modules are readily available on my repository, providing a convenient and efficient way to incorporate them into your own Terraform projects.

Below, you will find a step-by-step guide on how to implement your infrastructure using the modules I have created. These modules are designed to streamline the process and enable you to quickly set up a robust and scalable infrastructure on Azure.

//Code to define a resource group
module "resource_group" {
  
  name = "ia"

  location = var.location
  environment = var.environment
  owner = var.owner
  project = var.project

  source = "git::https://github.com/sbonnet-dev/terraform-module-azure_resource_group.git?ref=main"
}

//Code to define a list of cognitive accounts
module "cognitive_account" {
  source = "git::https://github.com/sbonnet-dev/terraform-module_cognitive_account.git?ref=main"

  count = length(var.cognitives_names)

  name    = var.cognitives_names[count.index]
  rg_name = module.resource_group.resource_group_name
  location = var.location

  custom_subdomain_name = "cog-fr-oai"
  kind                  = var.cognitives_kinds[count.index]
  sku_name              = "S0"
  
  owner   = var.owner
  project = var.project
  environment = var.environment


  depends_on = [
    module.resource_group
  ]
}

...        

How to fetch all the modules quickly

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/sbonnet-dev/terraform-sample/main/openai/install.sh)"        

With this command line, all modules will be downloaded in the same directory and ready to use.

Note: this command only works if you have somme git and curl command already installed on your computer.

Once everything is fetched, just go to this directory and execue theses commands

$ cd 
$ terraform init
$ terraform validate        

If you are ready, you can deploy the infrastructure and you will have a simple architecture which allow to manage Azure OpenAI, Azure Form Recognizer, Azure Cognitive Search ...


Some commands lines to know

If you need to indent (format) automatically your code, you can use this command

$ terraform fmt        

How to automatically build a README to give informations about the module usage

terraform-docs markdown . | tee README.md        

To obtain graph image based on your code. You can use the following commands:

$ terraform graph | dot -Tsvg > graph.svg        

The result is not very beautiful but it's a good tool to obtain a quick view of the code.


WYSWYG

Additionally, if you are seeking a user-friendly WYSIWYG software for infrastructure creation, I highly recommend taking a quick look at Brainboard software. It offers a seamless interface and intuitive features that simplify the process of designing and managing your infrastructure.


In an upcoming article, I will provide you with a comprehensive example program that demonstrates how to automate the deployment of Azure OpenAI services. This sample program will showcase the seamless integration of Terraform and Azure, empowering you to effortlessly provision and configure your AI-powered infrastructure on the cloud.



要查看或添加评论,请登录

Sebastien Bonnet的更多文章

社区洞察