Import existing infrastructure into Terraform state
✓Works with OpenClaudeYou are a Terraform infrastructure engineer. The user wants to import existing cloud resources into Terraform state files so they can be managed by Terraform going forward.
What to check first
- Run
terraform versionto confirm Terraform is installed and check your version (import syntax varies slightly between versions) - Run
aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId'(or equivalent for your cloud provider) to list resource IDs you want to import - Verify the resource type exists in your Terraform configuration with a
resource "aws_instance" "example" {}block (resource must be declared, not yet configured)
Steps
- Create or update your Terraform configuration file with an empty resource block matching the resource type you're importing (e.g.,
resource "aws_instance" "web_server" {}) - Initialize your Terraform working directory with
terraform initif you haven't already - Run
terraform import aws_instance.web_server i-1234567890abcdef0where the first argument isresource_type.resource_nameand the second is the actual resource ID from your cloud provider - After successful import, run
terraform state show aws_instance.web_serverto view the imported resource attributes - Update your resource block in the
.tffile with the attributes shown in state (copy relevant arguments likeami,instance_type,security_groups, etc.) - Run
terraform planto verify Terraform recognizes no changes needed—if plan shows modifications, adjust your configuration to match the actual resource - Repeat steps 1–6 for each additional resource you need to import
- Run
terraform applyonce all imports are complete and your plan shows no changes
Code
# Step 1: Declare empty resource blocks for resources you'll import
resource "aws_instance" "web_server" {
# Attributes will be populated after import
}
resource "aws_security_group" "allow_http" {
# Attributes will be populated after import
}
resource "aws_s3_bucket" "app_data" {
# Attributes will be populated after import
}
# Step 2: Bash script to import multiple resources
#!/bin/bash
set -e
# Define your resources as "resource_type.resource_name:actual_resource_id"
declare -a RESOURCES=(
"aws_instance.web_server:i-1234567890abcdef0"
"aws_security_group.allow_http:sg-0987654321fedcba0"
"aws_s3_bucket.app_data:my-existing-bucket"
)
for resource in "${RESOURCES[@]}"
do
IFS=':' read -r tf_resource aws_id <<< "$resource"
echo "Importing $tf_resource with ID $aws_id..."
terraform import "$tf_resource" "$aws_id"
echo "✓ Successfully imported $tf_resource"
done
echo
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Terraform Skills
Other Claude Code skills in the same category — free to download.
Terraform Module
Create reusable Terraform modules with variables and outputs
Terraform State
Manage Terraform state with remote backends (S3, Azure, GCS)
Terraform Workspace
Configure Terraform workspaces for multi-environment management
Terraform Provider
Write custom Terraform providers with Go
Terraform Testing
Write Terraform tests with Terratest and terraform test
Terraform CI/CD
Set up Terraform CI/CD with GitHub Actions and Atlantis
Terraform Security
Scan Terraform for security issues with tfsec and Checkov
Want a Terraform skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.