An intelligent Terraform code generator that uses OpenAI GPT-4 to create, validate, and automatically fix Azure infrastructure configurations. This project demonstrates the integration of AI-powered infrastructure-as-code generation with automated validation and error correction.
- AI-Powered Code Generation: Uses OpenAI GPT-4 to generate production-ready Terraform code
- Automatic Error Detection & Fixing: Self-healing infrastructure code with intelligent retry mechanisms
- Multi-Environment Support: Pre-configured for SIT, UAT, and Production environments
- Security Best Practices: Built-in security configurations including private endpoints, NSGs, and Key Vault integration
- Azure Resource Compliance: Follows Azure naming conventions and governance standards
- CI/CD Integration: GitHub Actions workflow for automated deployment
- Prompt Engineering: Sophisticated prompt templates for consistent, high-quality code generation
ai-terraform-poc/
├── agent/
│ ├── prompt_terraform.py
│ └── validate_fix.py
├── environments/
│ ├── sit/terraform.tfvars
│ ├── urt/terraform.tfvars
│ └── prod/terraform.tfvars
├── modules/
│ └── main.tf
├── prompts/
│ ├── config.json
│ ├── system_prompt.txt
│ ├── user_prompt.txt
│ ├── user_prompt_template.txt
│ └── fix_prompt.txt
├── .github/workflows/
│ └── ci-cd.yml
└── requirements.txt
- Python 3.8+
- Terraform 1.0+
- Azure CLI (authenticated)
- OpenAI API Key
-
Clone the repository:
git clone <your-repository-url> cd ai-terraform-poc
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
export OPENAI_API_KEY="your-openai-api-key" export ARM_CLIENT_ID="your-azure-client-id" export ARM_CLIENT_SECRET="your-azure-client-secret" export ARM_SUBSCRIPTION_ID="your-azure-subscription-id" export ARM_TENANT_ID="your-azure-tenant-id"
-
Generate Infrastructure Code:
cd ai-terraform-code python agent/prompt_terraform.py -
Deploy to Specific Environment:
cd modules terraform init terraform plan -var-file=../environments/sit/terraform.tfvars terraform apply -var-file=../environments/sit/terraform.tfvars
- Interprets natural language infrastructure requirements
- Generates production-ready Terraform code following Azure best practices
- Implements security controls and governance standards automatically
- Detects Terraform validation errors
- Automatically generates fixes using AI
- Supports up to 3 retry attempts with progressive improvements
- Creates diagnostic reports for persistent issues
Edit prompts/user_prompt.txt to modify infrastructure requirements:
PROJECT DETAILS:
- Project Name: Your Project
- Environment: Development/Production
- Business Unit: Your Unit
- Owner: Your Team
- Cost Center: Your Cost Center
RESOURCE REQUIREMENTS:
- Describe your infrastructure needs...
NAMING & GOVERNANCE:
- Naming Prefix: your-prefix
- Tags: Your tagging strategy
The GitHub Actions workflow (ci-cd.yml) provides:
- Automated Code Generation: Runs AI agent on code changes
- Terraform Validation: Validates generated code
- Multi-Environment Deployment: Supports SIT → UAT → Production flow
- Security Scanning: Built-in security checks
Configure these secrets in your GitHub repository:
OPENAI_API_KEYARM_CLIENT_IDARM_CLIENT_SECRETARM_SUBSCRIPTION_IDARM_TENANT_ID
- Private Endpoints: Secure access to Azure services
- Network Security Groups: Controlled network access
- Azure Key Vault Integration: Secure secrets management
- Managed Identity Support: Enhanced security for Azure resources
- Resource Tagging: Compliance and governance tracking
-
OpenAI API Errors:
- Verify
OPENAI_API_KEYis set correctly - Check API quota and billing status
- Verify
-
Terraform Validation Errors:
- Review
modules/diagnostic_report.txtfor detailed error analysis - Check Azure provider version compatibility
- Review
-
Authentication Issues:
- Ensure Azure Service Principal has appropriate permissions
- Verify all ARM environment variables are set
This project is licensed under the MIT License
- Support for additional cloud providers (AWS, GCP)
- Integration with Terraform Cloud/Enterprise
- Advanced cost optimization suggestions
- Infrastructure drift detection and correction
- Multi-region deployment strategies
- Enhanced security scanning and recommendations
For issues and questions:
- Check existing GitHub Issues
- Create a new issue with detailed description
#terraform #azure #ai #infrastructure-as-code #automation #devops #gpt4 #openai
Made with ❤️ by the DevOps Team
- Suthakar Paramathma
