Skip to content

About

Terraform GPT AI based Terraform Importer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

TerraformGPT: AI-Powered Azure Infrastructure Management

This project provides an AI-powered assistant for Azure infrastructure management using Terraform. It features a multi-agent system for importing, analyzing, optimizing, and deploying Azure resources with Terraform.

Features

  • Import Azure resources into Terraform using Terraformer
  • Analyze Azure infrastructure
  • Optimize Terraform code
  • Apply Terraform best practices
  • Create reusable Terraform modules
  • Deploy infrastructure to Azure
  • Interactive wizard for infrastructure creation
  • Multi-agent system for specialized tasks
  • Natural language interface for all operations

Multi-Agent System

The multi-agent system consists of the following specialized agents:

  1. Analyzer Agent: Analyzes Azure resources and infrastructure to understand the current state.
  2. Planner Agent: Creates detailed plans for importing and managing Azure resources with Terraform.
  3. Executor Agent: Executes plans by running Terraformer and Terraform commands to import and manage resources.
  4. Consolidator Agent: Organizes and optimizes the generated Terraform code for readability and maintainability.
  5. Clarification Agent: Handles user interactions and clarifies requirements to ensure effective resource management.
  6. Module Agent: Creates reusable Terraform modules from imported resources.
  7. Deployer Agent: Deploys infrastructure to Azure by creating resource groups and applying Terraform configurations.

Advanced Response Parsing

The system uses LLM-based response parsing to extract structured information from agent responses, providing several benefits:

  • Improved Understanding: Better comprehension of user intentions and resource specifications
  • Robust Confirmation Handling: Accurately detects when users confirm resource group selections
  • Semantic Analysis: Understands the meaning behind responses rather than just matching text patterns
  • Adaptability: Handles variations in phrasing without requiring code changes

This approach ensures a more natural conversation flow and reduces the need for repetitive confirmations.

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd terraform-agent
    
  2. Create a virtual environment:

    python -m venv terraform-agent-venv
    
  3. Activate the virtual environment:

    # Windows
    terraform-agent-venv\Scripts\activate
    
    # Linux/Mac
    source terraform-agent-venv/bin/activate
    
  4. Install dependencies:

    pip install -r requirements.txt
    
  5. Create a .env file with your Azure OpenAI credentials:

    AZURE_OPENAI_API_KEY=your_api_key_here
    AZURE_OPENAI_ENDPOINT=https://your-endpoint.openai.azure.com
    AZURE_OPENAI_DEPLOYMENT_NAME=your_deployment_name
    AZURE_OPENAI_API_VERSION=2023-05-15
    

Usage

TerraformGPT Multi-Agent System

Run the main application with the AI-powered multi-agent system:

python terraform_gpt.py

Command-Line Options

The application supports the following command-line options:

# Show version information
python terraform_gpt.py --version

# Use mock Azure data instead of real Azure resources
python terraform_gpt.py --mock

# Skip dependency checks
python terraform_gpt.py --skip-checks

Terraform Wizard

For users who prefer a structured, step-by-step approach without AI, you can use the Terraform Wizard:

cd scripts
python terraform_wizard.py

Or run the batch file:

scripts\terraform_wizard.bat

The wizard provides an interactive interface to:

  • Generate Terraform configurations using reusable modules
  • Deploy infrastructure to Azure
  • View resources in the Azure Portal
  • Deploy pre-defined infrastructure examples

Utility Scripts

The project includes several utility scripts in the scripts directory:

  • terraform_wizard.bat: Interactive wizard for creating and deploying infrastructure
  • deploy_complete_infrastructure.bat: Deploy pre-defined infrastructure directly
  • destroy_infrastructure.bat: Remove deployed infrastructure

Mock Mode

When running with the --mock option, the application will use simulated Azure resources instead of connecting to real Azure resources. This is useful for:

  • Testing the application without an Azure subscription
  • Demonstrating the application's capabilities
  • Development and testing without affecting real Azure resources

In mock mode, no real Azure resources will be accessed or modified.

Project Structure

terraform_agent/
├── .env                        # Environment variables
├── config/                     # Configuration files
│   ├── agent_config.json       # Agent configuration
│   ├── prompts.json            # System prompts
│   └── settings.py             # Application settings
├── core/                       # Core functionality implementation
│   ├── azure_sdk.py            # Azure SDK functionality
│   ├── terraform.py            # Terraform functionality
│   ├── terraformer.py          # Terraformer functionality
│   └── terraform_module.py     # Terraform module functionality
├── tools/                      # LangChain tool wrappers
│   ├── azure_sdk_tools.py      # Azure SDK tools
│   ├── terraform_tools.py      # Terraform tools
│   ├── terraformer_tools.py    # Terraformer tools
│   └── terraform_module_tools.py # Terraform module tools
├── multi_agent/                # Multi-agent implementation
│   ├── analyzer_agent.py       # Azure resource analyzer
│   ├── base_agent.py           # Base agent class
│   ├── clarification_agent.py  # User interaction handler
│   ├── consolidator_agent.py   # Code optimizer
│   ├── deployer_agent.py       # Infrastructure deployer
│   ├── executor_agent.py       # Command executor
│   ├── module_agent.py         # Module creator
│   ├── orchestrator.py         # Multi-agent orchestrator
│   └── planner_agent.py        # Plan creator
├── scripts/                    # Utility scripts
│   ├── terraform_wizard.py     # Interactive infrastructure generator
│   ├── terraform_wizard.bat    # Batch file for the wizard
│   ├── deploy_complete_infrastructure.bat # Deploy pre-defined infrastructure
│   ├── destroy_infrastructure.bat # Remove deployed infrastructure
│   └── README.md               # Scripts documentation
├── utils/                      # Utility functions
│   ├── azure_subscription.py   # Azure subscription utilities
│   ├── dependency_utils.py     # Dependency checking
│   ├── file_utils.py           # File operations
│   └── llm_parser.py           # LLM-based response parsing
├── terraform_output/           # Generated Terraform files
│   └── generated_config/       # Generated configurations
├── terraform_modules/          # Terraform modules
│   └── examples/               # Example implementations
│       └── complete_infrastructure/ # Pre-defined infrastructure example
├── terraform_gpt_cli.py        # CLI interface for multi-agent system
├── terraform_gpt.py            # Main entry point
├── terraform_gpt.bat           # Batch file for main entry point
├── requirements.txt            # Project dependencies
├── README.md                   # Project documentation
└── TROUBLESHOOTING.md          # Troubleshooting guide

Customization

Custom Prompts

Edit config/prompts.json to customize the system prompts and messages used by the agents.

Tool Configuration

Edit config/agent_config.json to configure which tools are available to the agents.

Azure Authentication

The system uses your existing Azure authentication. You can authenticate using:

  1. Azure CLI: Run az login in your terminal before running the application
  2. Environment Variables: Set the following environment variables:
    AZURE_SUBSCRIPTION_ID=your_subscription_id
    AZURE_TENANT_ID=your_tenant_id
    AZURE_CLIENT_ID=your_client_id
    AZURE_CLIENT_SECRET=your_client_secret
    

Authentication Requirements

By default, the application requires valid Azure authentication to run. If you're not authenticated to Azure, the application will display an error message and exit.

If you want to run the application without Azure authentication, use the --mock option:

python run_multi_agent.py --mock

This will run the application in mock mode, using simulated Azure resources instead of connecting to real Azure resources.

Dependencies

  • Azure SDK: For interacting with Azure resources
  • Terraform: For managing infrastructure as code
  • Terraformer: For importing existing Azure resources into Terraform
  • OpenAI: For LLM-based response parsing and semantic understanding

Why Azure OpenAI?

This project uses Azure OpenAI Service rather than the general OpenAI API for several important reasons:

Data Privacy and Security

  • Data Residency: All data remains within your Azure subscription boundary
  • No Training: Microsoft does not use your prompts and completions to train or improve models
  • Compliance: Adheres to Microsoft's enterprise-grade compliance and security standards

Integration Benefits

  • Azure Ecosystem: Seamless integration with other Azure services
  • Authentication: Uses the same authentication mechanisms as other Azure services
  • Monitoring: Integrated with Azure's monitoring and logging capabilities

Enterprise Features

  • Private Networking: Can be configured with Azure Private Link
  • Role-Based Access: Integrates with Azure Active Directory for access control
  • Resource Management: Managed through Azure Resource Manager

Using Azure OpenAI ensures that your infrastructure data and prompts remain private and secure within your Azure environment, which is especially important when dealing with sensitive infrastructure configurations.

Terraform Module Structure

Generated Terraform modules follow this structure:

modules/
├── module_name/
│   ├── main.tf           # Main module resources
│   ├── variables.tf      # Input variables
│   ├── outputs.tf        # Output values
│   └── examples/         # Example implementations
│       └── basic/
│           └── main.tf   # Basic usage example

Troubleshooting

See TROUBLESHOOTING.md for common issues and solutions.

License

MIT License

About

Terraform GPT AI based Terraform Importer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages