
{"id":8562,"date":"2025-10-31T12:41:16","date_gmt":"2025-10-31T12:41:16","guid":{"rendered":"https:\/\/rax-stg-wp-aws.com\/blogs\/my-journey-of-building-a-terraform-ai-agent-automating-cloud-infrastructure-with-ai\/"},"modified":"2026-11-03T19:29:50","modified_gmt":"2026-11-03T19:29:50","slug":"my-journey-of-building-a-terraform-ai-agent-automating-cloud-infrastructure-with-ai","status":"publish","type":"blogs","link":"https:\/\/www.rackspace.com\/en-gb\/blogs\/my-journey-of-building-a-terraform-ai-agent-automating-cloud-infrastructure-with-ai\/","title":{"rendered":"My Journey of Building a Terraform AI Agent \u2014 Automating Cloud Infrastructure with AI"},"content":{"rendered":"<p><strong>The vision: Why I built this AI agent<\/strong><\/p>\n<p class=\"MsoNormal\">As a DevOps Engineer passionate about AI, I always seek ways to optimize and&nbsp;automate cloud infrastructure. Managing Terraform code manually was timeconsuming,&nbsp;error-prone, and required constant validation. I wanted to build something&nbsp;that could help my team and customers deploy resources faster, smarter and with&nbsp;fewer mistakes.<\/p>\n<p class=\"MsoNormal\">I shared my idea with my manager who encouraged me to explore this further. I also&nbsp;took feedback from my colleagues which helped refine the approach. This led to the&nbsp;creation of the Terraform AI Agent, a GenAI tool that generates, validates, and deploys&nbsp;Terraform code in Azure automatically.<\/p>\n<p><strong>The journey: From idea to execution<\/strong><\/p>\n<p class=\"MsoNormal\">Two months ago, I started researching how AI could automate Terraform. Initially, my&nbsp;goal was simple: Generate Terraform code automatically and save it into .tf files.<\/p>\n<p class=\"MsoNormal\"><em>The Challenge: AI Hallucination &amp; Duplicate Code<\/em><\/p>\n<p class=\"MsoNormal\">However, I soon faced a major issue, the AI was generating the same code repeatedly&nbsp;for similar requests, leading to duplicate resources in Azure. This could cause conflicts&nbsp;and unnecessary costs.<\/p>\n<p class=\"MsoNormal\"><em>The Solution: Smart Code Generation &amp; Validation<\/em><\/p>\n<p class=\"MsoNormal\">To fix this, I implemented a function that checks existing Terraform infrastructure before&nbsp;generating new code. This ensures:<\/p>\n<p class=\"MsoNormal\">\u2714 No duplicate resources are created.<br \/>\n\u2714 Only unique and necessary code is generated.<br \/>\n\u2714 Custom modules &amp; templates can be used to meet compliance requirements.<br \/>\n\u2714 AI hallucinations (incorrect code generation) are eliminated.<br \/>\nAfter solving the code generation problem, I focused on validating and deploying the&nbsp;Terraform code seamlessly.<\/p>\n<p class=\"MsoNormal\"><strong>How the Terraform AI agent works?<\/strong><\/p>\n<p><strong>Step 1: Enter infrastructure request<\/strong><\/p>\n<p class=\"MsoNormal\">Instead of manually writing Terraform code, users enter their infrastructure request in a&nbsp;simple prompt.<\/p>\n<ul>\n<li>Example: \u201cCreate a resource group named aiagent-terraform-rg in uksouth.\u201d<\/li>\n<li>The AI instantly generates Terraform code while checking for existing<\/li>\n<li>infrastructure to prevent duplicates.<\/li>\n<li>Supports custom modules &amp; templates to ensure compliance.<\/li>\n<\/ul>\n<p><strong>Step 2: Validate and auto-fix code<\/strong><\/p>\n<p>Before deployment, the AI performs multiple checks using function:<br \/>\n\u2714 Runs terraform fmt to format the code.<br \/>\n\u2714 Uses terraform validate to ensure correctness.<br \/>\n\u2714 Applies TFLint to detect security misconfigurations.<br \/>\n\u2714 Auto-fixes errors to maintain best practices.<\/p>\n<p><strong>Step 3: Deploy via GitHub Actions<\/strong><\/p>\n<p class=\"MsoNormal\">With a single click on \u201cSave &amp; Deploy\u201d function, the Terraform AI Agent: Pushes&nbsp;validated Terraform code to GitHub and Triggers GitHub Actions, which run:<\/p>\n<ul>\n<li>terraform init<\/li>\n<li>terraform validate<\/li>\n<li>terraform plan<\/li>\n<li>terraform apply<\/li>\n<li>Deploys infrastructure in Azure and stores the Terraform state in Blob Storage.<\/li>\n<\/ul>\n<p class=\"MsoNormal\"><strong>Terraform AI agent architecture and workflow<\/strong><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_technical_images\/27901-body-Terraform-Agent-Blog.png\" alt=\"terraform-ai-agent-rackspace-technology\" \/><\/p>\n<p><strong>GitHub repository: <\/strong><a href=\"https:\/\/github.com\/anoopkum\/terraform-automation\">https:\/\/github.com\/anoopkum\/terraform-automation<\/a><\/p>\n<p><strong>Step-by-step implementation<\/strong><\/p>\n<p class=\"MsoNormal\">The heart of our solution is the AI agent that converts natural language into Terraform&nbsp;code. Looking at the repository, we can see how this is implemented in aiagent.py.&nbsp;Initially I use OpenAI\u2019s GPT-4o model but later changed to O3-mini which is good in&nbsp;reasoning\/code to convert natural language prompts into structured Terraform code.<\/p>\n<p><strong>User interface workflow<\/strong><\/p>\n<p class=\"MsoNormal\">UI has three main action buttons that guide users through the infrastructure creation process:<\/p>\n<p><strong>1. Generate Terraform code button<\/strong><\/p>\n<p class=\"MsoNormal\">When users enter their infrastructure requirements as a natural-language prompt, they can click the \u201cGenerate Terraform Code\u201d button. This triggers the AI agent to:<\/p>\n<ul>\n<li>Process the natural language description<\/li>\n<li>Convert it into properly structured Terraform code<\/li>\n<li>Display the generated code for review<\/li>\n<\/ul>\n<p class=\"MsoNormal\">The generated code appears in a code editor interface where users can review it and make any desired adjustments.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/27901-body-Picture2.png\" alt=\"Terraform AI Agent Pic 2\" \/><\/p>\n<p><strong>2. Validate Terraform code button<\/strong><\/p>\n<p class=\"MsoNormal\">After generating the code, users can click the \u201cValidate Terraform Code\u201d button to&nbsp;initiate the validation process.<\/p>\n<p class=\"MsoNormal\">This button triggers:<\/p>\n<ul>\n<li>Code formatting with terraform fmt<\/li>\n<li>Syntax checking with terraform validate<\/li>\n<li>Best practices verification with tflint<\/li>\n<\/ul>\n<p class=\"MsoNormal\">Green checkmarks indicate successful validations, while red alerts highlight areas&nbsp;needing attention.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/27901-body-Picture3.png\" alt=\"Terraform AI Agent Pic 3 \" \/><\/p>\n<p><strong>3. Save &amp; deploy button:<\/strong><\/p>\n<p class=\"MsoNormal\">Once the code passes validation, users can click the \u201cSave &amp; Deploy\u201d button to initiate&nbsp;the deployment process. This button:<\/p>\n<ul>\n<li>Saves the code into terraform file (main.tf)<\/li>\n<li>Creates a new pull request in the GitHub repository<\/li>\n<li>Triggers the GitHub Actions workflow<\/li>\n<li>Automates the approval and merge process<\/li>\n<li>Deploys the infrastructure to Azure<\/li>\n<\/ul>\n<p class=\"MsoNormal\">The interface provides real-time status updates during the deployment process,&nbsp;showing the progress from plan generation to resource creation.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/27901-body-Picture4.png\" alt=\"Terraform AI Agent Pic 4\" \/><\/p>\n<p><strong>Terraform state management<\/strong><\/p>\n<p class=\"MsoNormal\">A critical aspect of Terraform infrastructure management is the state file. In our&nbsp;architecture, the Terraform state is stored securely in Azure Blob Storage. The GitHub Actions workflow initializes Terraform with backend configuration that&nbsp;points to the Azure Storage Account and container. The storage account access key is&nbsp;securely fetched from Azure Key Vault during deployment.<\/p>\n<p><strong>Key vault during deployment<\/strong><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/27901-body-Picture5.png\" alt=\"Terraform AI Agent Pic 5\" \/><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/blog_article_images\/27901-body-Picture6.png\" alt=\"Terraform AI Agent Pic 6\" \/><\/p>\n<p><strong>Frontend implementation<\/strong><\/p>\n<p>The frontend interface is implemented using a modern React application. Let\u2019s look at how the core user interface component is structured:<\/p>\n<ul>\n<li>UI implementation (<a href=\"https:\/\/github.com\/anoopkum\/terraform-automation\/blob\/main\/streamlit_app.py\">streamlit_app.py<\/a>)<\/li>\n<\/ul>\n<p class=\"MsoNormal\">This Streamlit app implements the step-by-step workflow with the three main buttons and manages the entire process from prompt input to deployment.<\/p>\n<p class=\"MsoNormal\"><em>View the live demo of<a href=\"https:\/\/www.youtube.com\/watch?v=VU9irhDMWKU&amp;t=101s\"> Terraform AI agent here<\/a><\/em><br \/>\n<strong>Business impact<\/strong><\/p>\n<ul>\n<li>Reduces deployment time from hours to minutes<\/li>\n<li>Eliminates human errors with auto-validation and fixes<\/li>\n<li>Ensures compliance with pre-configured modules and security checks<\/li>\n<li>Improves efficiency for DevOps and cloud engineers<\/li>\n<\/ul>\n<p><strong>Future enhancements<\/strong><\/p>\n<p class=\"MsoNormal\">Currently, the Terraform AI agent supports resource creation. The next phase will include:<\/p>\n<ul>\n<li>Modify and delete capabilities for the full infrastructure lifecycle management<\/li>\n<li>Support for additional cloud platforms beyond Azure<\/li>\n<\/ul>\n<p><strong>Tech stack and AI models<\/strong><\/p>\n<ul>\n<li>Azure OpenAI (GPT-4o\/O3-mini) for code generation.<\/li>\n<li>Terraform for infrastructure as code.<\/li>\n<li>GitHub Actions for CI\/CD automation.<\/li>\n<li>Python (FastAPI, Streamlit) for AI agent logic &amp; UI.<\/li>\n<li>Github as a repository.<\/li>\n<li>Azure Cloud.<\/li>\n<\/ul>\n<p>Check out the&nbsp;<a href=\"https:\/\/github.com\/anoopkum\/terraform-automation\">terraform-automation repository<\/a>&nbsp;to get your hands dirty and learn to create the AI agent.<\/p>\n<p>If you need any help or have questions? Feel free to reach out, I\u2019m happy to help!<br \/>\n<em>If you found this helpful, give it a thumbs up, support, and share it with others.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My Journey of Building a Terraform AI Agent \u2014 Automating Cloud Infrastructure with AI<\/p>\n","protected":false},"author":2,"featured_media":8564,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","format":"standard","meta":{"content-type":"","nid":"27901","content_type":"blog","created_by":"10130","authored_on":"2025-10-27T12:41:16+00:00","field_article_industries":"SaaS & ISV \/ Technology","tag_category":"Technical","field_blog_author_text":"By Anoop Kumar, Lead DevOps Consultant, Rackspace Technology","seo_description":"Learn how a DevOps engineer built an AI-powered Terraform agent that automates Azure infrastructure deployment, eliminating errors and reducing deployment time.","field_featured_image":"","insights_image":"\/wp-content\/uploads\/2026\/blog_technical_images\/27901-insights_image-general-code-technical-blog-3-insights.jpg","field_partners":"Microsoft","rxt_platforms":"","product":"","publisheddate":"2025-10-31T12:41:16","rxt_solutions":"","opengraph_tag":"","x_tag":"","teaser_flag":"Technical Blog","seo_titile":"Automating Cloud Infrastructure with AI | Rackspace Technology","field_topics":"AI, Public Cloud","field_slug":"building-terraform-ai-agent-automating-cloud-infrastructure-with-ai","footnotes":""},"categories":[178],"tags":[179,182,183,195,204],"class_list":["post-8562","blogs","type-blogs","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-ai","tag-microsoft","tag-public-cloud","tag-saas-isv-technology","tag-technical"],"_links":{"self":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs\/8562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs"}],"about":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/types\/blogs"}],"author":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/comments?post=8562"}],"version-history":[{"count":4,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs\/8562\/revisions"}],"predecessor-version":[{"id":17108,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/blogs\/8562\/revisions\/17108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/media\/8564"}],"wp:attachment":[{"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/media?parent=8562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/categories?post=8562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rackspace.com\/en-gb\/wp-json\/wp\/v2\/tags?post=8562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}