Low Hanging Fruit: Using AI to Generate Terraform Import Blocks
I’ve been using Terraform for many years. I’ve encountered this error message more times than I can count:
Error: A resource with the ID “/subscriptions/a8dc551f-cbe8–47e9–87c1-d9570ac6d69d/resourceGroups/rg-svc-foo-processor-dev/providers/Microsoft.Web/sites/func-svc-foo-processor-dev/config/virtualNetwork” already exists — to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for “azurerm_app_service_virtual_network_swift_connection” for more information.
I always sigh and ask myself the age old question: Is it worth it?
Meaning, should I go through the trouble of building the import statement and bringing that resource under management or should I just go out into the Azure Portal, pop it in the head, and re-run terraform apply.
I don’t know if I am sorry to say this but in most situations I opt for popping it in the head.
This happened to me just the other day and I thought, this should be super easy. Why couldn’t ChatGPT do this for me?
Deep down as I prepared the extremely simple prompt, I sensed doubt, in the pit of my stomach I couldn’t believe that ChatGPT could make such a painful task go away completely.
So after painstakingly crafting the most brilliant prompt known to man:
create a terraform import block based on this error…
Yep. That’s it. Then you copy pasta the terminal output from Terraform and you get the following beautiful thing:
import {
to = azurerm_app_service_virtual_network_swift_connection.main
id = "/subscriptions/a8dc551f-cbe8-47e9-87c1-d9570ac6d69d/resourceGroups/rg-svc-foo-processor-dev/providers/Microsoft.Web/sites/func-svc-foo-processor-dev/config/virtualNetwork"
}
Now, my question is, HashiCorp, better be getting Terraform Cloud hooked up to Azure Open AI service and building this into their Infrastructure Cloud platform — if they haven’t done already.
I mean just look at this sweet perfection:
If this isn’t a prompt that could be easily integrated into a SaaS offering, I don’t know what one looks like! Does anybody know if they have?
Because, if they haven’t I’m sure there would be plenty of people at Microsoft willing to help them migrate to Azure to avoid the egress costs — or at the very least integrate with Azure Open AI.