When managing infrastructure at scale, you often encounter a common challenge: existing cloud resources that were created manually or through other means need to be brought under Terraform management. The traditional approach requires manually writing import blocks—a tedious process that involves looking up resource IDs, identifying the correct import syntax, and ensuring the generated Terraform code matches the existing configuration. This is where AI coding assistants can significantly accelerate your workflow.
This guide shows you how to use AI tools to generate Terraform import blocks for existing resources, reducing hours of manual work to minutes.
Why Import Blocks Matter in Terraform
Terraform import blocks allow you to bring existing infrastructure under Terraform’s control without destroying and recreating resources. This is essential for:
-
Migrating legacy infrastructure to Infrastructure as Code
-
Recovering state from accidentally deleted Terraform state files
-
Managing resources created through console, CLI, or other IaC tools
-
Onboarding projects where resources predate your Terraform implementation
The import block syntax in Terraform looks like this:
import {
to = aws_instance.example
id = "i-0abc123456789def0"
}
For each resource you need to import, you must know the resource type, the target resource address, and the unique identifier from your cloud provider. When managing hundreds of resources across multiple accounts, this becomes overwhelming quickly.
Using AI to Generate Import Blocks
AI assistants can help in several ways: identifying what resources exist in your cloud environment, determining the correct resource types, and generating the appropriate import blocks. Here’s how to approach this effectively.
Step 1: Gather Your Resource Information
Before asking AI for help, collect basic information about the resources you want to import:
-
Cloud provider (AWS, Azure, GCP, etc.)
-
Resource types you know exist (EC2 instances, S3 buckets, etc.)
-
Output from
terraform state pullif you have any existing state -
Cloud provider CLI output listing resources
For AWS, you might run:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].InstanceId' --output text
aws s3api list-buckets --query 'Buckets[].Name' --output text
Provide this information to your AI assistant as context.
Step 2: Craft an Effective Prompt
The quality of AI-generated import blocks depends heavily on how you frame your request. Here’s an effective prompt structure:
“Generate Terraform import blocks for the following AWS resources. Use the current Terraform AWS provider syntax. For each resource, create an import block and the corresponding resource definition:
- 3 EC2 instances: i-0abc123456789def0, i-0def123456789abcd, i-0ghi1234567891234
- 2 S3 buckets: my-production-bucket, my-backend-archive
- 1 RDS instance: db-prod-01
Format the output as complete Terraform code that can be applied directly.”
Step 3: Review AI-Generated Code Carefully
AI generates correct import blocks most of the time, but you must verify the output. Check these common issues:
Resource type accuracy: Ensure the AI chose the correct resource type. For example, aws_instance for EC2, not aws_ec2_instance (the legacy naming).
Identifier format: Some resources require compound identifiers. An S3 bucket import might need:
import {
to = aws_s3_bucket.example
id = "my-bucket-name"
}
But an EC2 instance with a specific subnet might need:
import {
to = aws_instance.example
id = "i-0abc123456789def0"
}
Provider configuration: Verify the provider in the generated code matches your setup. AWS resources might need:
provider "aws" {
region = "us-east-1"
}
Practical Example: Importing an AWS VPC
Suppose you have an existing VPC with ID vpc-0a1b2c3d4e5f6g7h8 that you need to manage with Terraform. Here’s how to work with AI to generate the import:
Ask your AI assistant:
Generate Terraform code to import an existing AWS VPC with ID vpc-0a1b2c3d4e5f6g7h8. Include:
1. The import block
2. The resource definition with minimal required arguments
3. Use us-east-1 as the region
The AI should produce:
import {
to = aws_vpc.main
id = "vpc-0a1b2c3d4e5f6g7h8"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
After generating this code, run terraform plan to verify Terraform can read the existing VPC and reconcile any differences between your configuration and the actual state.
Advanced AI Strategies for Bulk Imports
When dealing with dozens or hundreds of resources, adjust your approach:
Batch by resource type: Group similar resources together in your prompts. Ask for all EC2 instances first, then S3 buckets, then RDS instances. This reduces confusion and makes the AI’s output more organized.
Use state data sources: For resources already tracked in partial Terraform state, use data sources to read current configuration:
data "aws_instance" "existing" {
instance_id = "i-0abc123456789def0"
}
import {
to = aws_instance.managed
id = data.aws_instance.existing.id
}
Cross-reference documentation: When AI generates import blocks for unfamiliar resource types, ask it to include comments referencing the official Terraform provider documentation.
Limitations to Understand
AI tools have boundaries you should recognize:
-
Complex resource dependencies: Resources with intricate relationships (like autoscaling groups with launch configurations) may need manual intervention
-
Secret values: Passwords, API keys, and sensitive values stored in resource attributes won’t be automatically discovered
-
Custom resources: Third-party or custom Terraform resources may not be in the AI’s training data
-
Drift detection: AI generates import blocks based on current state, but won’t detect configuration drift between actual resource state and desired configuration
Best Practices for AI-Assisted Imports
-
Always run
terraform planbefore applying import blocks to verify what will change -
Use separate import files per resource type or environment for organization
-
Add meaningful tags to imported resources for better management
-
Version your provider explicitly to avoid unexpected behavior changes
-
Backup your state before running imports on production resources
Related Articles
- How to Use AI to Resolve Python Import Circular Dependency E
- AI Tools for Creating Dbt Documentation Blocks
- How Accurate Are AI Tools for Rust Unsafe Code Blocks
- AI Tools for Interpreting Terraform Plan Errors with Provide
- AI Tools for Interpreting Terraform Plan Errors
Built by theluckystrike — More at zovo.one