0
0
Terraformcloud~7 mins

Bulk import strategies in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you have existing cloud resources that Terraform did not create. Bulk import strategies help you bring those resources under Terraform management quickly and safely.
When you have many existing cloud resources created manually or by other tools and want to manage them with Terraform.
When migrating infrastructure to Terraform without downtime or resource recreation.
When you want to track and update existing resources using Terraform state.
When onboarding a new team to manage infrastructure with Terraform but resources already exist.
When you want to avoid deleting and recreating resources by importing them instead.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.3.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "example-bulk-import-bucket"
}

This Terraform configuration defines the AWS provider and a sample S3 bucket resource. The bucket name matches an existing bucket you want to import. This file is the base for importing existing resources into Terraform state.

Commands
Initialize Terraform in the current directory to download providers and set up the backend.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching ">= 4.0"... - Installing hashicorp/aws v4.50.0... - Installed hashicorp/aws v4.50.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
Import the existing S3 bucket named 'example-bulk-import-bucket' into Terraform state under the resource 'aws_s3_bucket.example'.
Terminal
terraform import aws_s3_bucket.example example-bulk-import-bucket
Expected OutputExpected
aws_s3_bucket.example: Importing from ID "example-bulk-import-bucket"... aws_s3_bucket.example: Import prepared! Prepared aws_s3_bucket for import aws_s3_bucket.example: Refreshing state... [id=example-bulk-import-bucket] Import successful! The resources that were imported are now in your Terraform state and will be managed by Terraform.
Check the Terraform plan to verify that the imported resource matches the configuration and no changes are needed.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist. Your infrastructure matches the configuration.
List all resources currently tracked in the Terraform state to confirm the import.
Terminal
terraform state list
Expected OutputExpected
aws_s3_bucket.example
Key Concept

If you remember nothing else from this pattern, remember: Terraform import links existing resources to your configuration so you can manage them without recreating.

Common Mistakes
Trying to import a resource without defining it in the Terraform configuration file.
Terraform import requires the resource block to exist in the configuration to map the imported resource correctly.
First define the resource in your .tf file with the correct type and name, then run terraform import.
Importing resources one by one manually for a large number of resources.
This is time-consuming and error-prone for many resources.
Use scripting or automation to run terraform import commands in bulk, or use tools that generate import scripts.
Not running terraform plan after import to verify state matches configuration.
You might miss configuration drift or mismatches causing unintended changes on apply.
Always run terraform plan after import to confirm no unexpected changes.
Summary
Define the existing resource in Terraform configuration before importing.
Use terraform import to link existing resources to Terraform state.
Run terraform plan to verify the imported resource matches the configuration.