Posts Transition from ARM Templates to Terraform with AI
Post
Cancel

Transition from ARM Templates to Terraform with AI

Terraform for Azure

In our previous article, we explored the basics of getting started with Terraform for Azure. Today, we’re taking a step further by looking into how AI can help ustranslate ARM or Bicep templates to Terraform.

Make sure you check the remainin articles in the series:

  1. Getting Started with Terraform on Azure
  2. Transition from ARM Templates to Terraform with AI (You are here)
  3. Terraform Configuration Essentials: File Types, State Management, and Provider Selection
  4. Writing Your First Azure Terraform Configuration
  5. Modules in Terraform (TBD)
  6. Advanced Terraform Techniques and Best Practices (TBD)
  7. Integrating Terraform with Azure DevOps (TBD)
  8. Terraform Associate Certification Study Guide and Tips (TBD)

As we dive deeper into Infrastructure as Code (IaC), it’s good to start with something familiar. And what is more familiar than ARM templates we already wrote. However, there are no native tools by HashiCorp or Microsoft to directly translate ARM or Bicep templates to Terraform. While there are some promising community-created tools, I haven’t personally tried them.

So, let’s get creative and utilize Artificial Intelligence to do exactly that!

Transitioning from ARM Templates to Terraform: The Why and How

ARM templates have been a staple for deploying resources in Azure, but they come with their own set of challenges:

  • Format: ARM templates are JSON files, which can be verbose and cumbersome to read and write.
  • Complexity: Learning and mastering ARM templates can be daunting due to their intricate syntax and structure.
  • Maintenance: As your infrastructure grows, managing and maintaining ARM templates can become increasingly complicated.
  • Azure-specific: ARM templates are designed specifically for Azure, limiting their use in multi-cloud or hybrid environments.

Comparing to that, Terraform and Azure Bicep offer several advantages over ARM templates:

  • Simpler Syntax: Terraform uses HashiCorp Configuration Language (HCL), which is more human-readable and easier to write compared to JSON.
  • Multi-platform: Terraform supports multiple cloud providers, allowing for consistent IaC practices across different environments.
  • Modularity: Terraform modules enable reusability and better organization of your infrastructure code.
  • State Management: Terraform’s state management provides a clear view of your infrastructure and helps prevent configuration drift.

While the ideal approach is to write clean, well-structured Terraform code from scratch, we often find ourselves with a backlog of existing ARM templates. Rewriting them manually can be time-consuming and error-prone. This is where we can “cheat” a little by leveraging AI to assist in the translation process, providing a starting point for refining and optimizing our Terraform code.

Benefits

  • Efficiency: AI can quickly translate large volumes of code, saving time and effort compared to manual rewriting.
  • Consistency: LLM ensures a standardized approach to translation, reducing the risk of human error and inconsistency.
  • Learning Aid: For those new to Terraform, AI-generated translations can serve as valuable learning tools, helping to understand how ARM templates map to Terraform syntax.

Limitations

  • Accuracy: AI translations may not always be perfect, especially for complex or nuanced code structures. Manual review and adjustments are often necessary.
  • Context Awareness: AI may not fully grasp the context or intent behind certain code segments, leading to potential misinterpretations.
  • Customization: AI-generated code may require further customization to align with specific project requirements or best practices.

How to use PowerShell and ChatGPT to translate ARM Templates to Terraform

We’ll leverage the power of PowerShell combined with the ChatGPT API to translate our ARM or Bicep templates into Terraform code.

If you’re interested in seeing how to use Azure OpenAI for this purpose, check out my session at Come Cloud With Us where I covered that topic in detail.

Prerequisites

  • Ensure you have PowerShell 7 or later installed.
  • Install the PowerShellGet and Az modules if not already installed:

    1
    2
    
    Install-Module -Name PowerShellGet -Force
    Install-Module -Name Az -Force
    

Obtaining the ChatGPT API Key

  1. Sign up for an account on the OpenAI platform.
  2. Navigate to the API keys section and create a new API key.
  3. Store the API key securely, as you’ll need it to authenticate your requests.

Preparing the ARM Template

For demonstration purposes, let’s use a simple ARM template that defines a Resource Group with four tags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2020-06-01",
      "location": "westus",
      "name": "MyResourceGroup",
      "properties": {},
      "tags": {
        "Environment": "Development",
        "Project": "Azure Spring Clean 2024",
        "Owner": "Vukasin Terzic",
        "CostCenter": "12345"
      }
    }
  ]
}

Crafting the PowerShell Script

Create a PowerShell script to read the ARM template, construct the prompt, and call the ChatGPT API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Set your API key
$apiKey = "YOUR_API_KEY_HERE"

# Read the ARM template
$template = Get-Content -Path "path/to/your/arm_template.json" -Raw

# Construct the prompt
$prompt = @"
Translate the following ARM template to Terraform code with comments explaining each resource:

$template
"@

# Set up the API headers
$headers = @{
    "Authorization" = "Bearer $apiKey"
    "Content-Type" = "application/json"
}

# Construct the API request body
$body = @{
    "model" = "text-davinci-003"
    "prompt" = $prompt
    "max_tokens" = 2048
} | ConvertTo-Json

# Call the ChatGPT API
$response = Invoke-RestMethod -Uri "https://api.openai.com/v1/completions" -Method Post -Headers $headers -Body $body

# Extract the Terraform code from the response
$terraformCode = $response.choices[0].text

# Output the Terraform code
Write-Output $terraformCode

Running the Script

Now that we have our simple prompt and constructed script, it is time to test it. Execute the PowerShell script to generate the Terraform code.

As simple as that!

Here is the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Define the provider
provider "azurerm" {
  features {}
}

# Create a resource group
resource "azurerm_resource_group" "my_resource_group" {
  name     = "MyResourceGroup"
  location = "westus"

  # Define tags
  tags = {
    Environment = "Development"
    Project     = "Azure Spring Clean 2024"
    Owner       = "Vukasin Terzic"
    CostCenter  = "12345"
  }
}

Testing and Validation

Once you have translated your ARM templates to Terraform using the ChatGPT API, you need to remember that this approach is not perfect. The translation might require some manual adjustments, especially for complex templates. Always review the generated Terraform code before applying it.

And then you need to verify it. There is no simple way arround it, you really need to test it. This is the same as you would do if you wrote it from scratch.

Best Practices for Validation

  • Iterative Testing: Start with small, simple templates and gradually increase complexity as you gain confidence in the translation process.
  • Peer Review: Have someone else review both the original ARM template and the generated Terraform code, as well as the plan and final deployment.
  • Automated Testing: Consider using automated testing tools for Terraform, such as terraform validate or third-party testing frameworks, to catch errors early in the development process.

Conclusion

We’ve explored how to use AI, specifically the ChatGPT API, to translate ARM templates to Terraform with the help of PowerShell. This approach can save time and reduce the effort required to migrate to Terraform, especially for those with existing ARM templates. This is not a perfect solution and requires some manual review, but it’s a great starting point for automating your infrastructure as code. Remember to test and validate your Terraform code thoroughly to ensure it meets your needs.

Azure Spring Clean 2024

This article is part of the Azure Spring Clean initiative, a community-driven event focused on sharing knowledge and best practices for Azure. Check out Azure Spring Clean for more insightful content from the Azure community.

Azure Spring Clean

Updated Mar 22, 2024 2024-03-22T18:34:12+01:00
This post is licensed under CC BY 4.0