0
0
Terraformcloud~10 mins

Writing configuration for imported resources in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you have existing cloud resources created outside Terraform. To manage them with Terraform, you import them and then write configuration that matches their current state. This lets Terraform track and update those resources safely.
When you want to start managing an existing cloud server with Terraform without recreating it
When a resource was created manually or by another tool and you want to bring it under Terraform control
When migrating infrastructure to Terraform step-by-step without downtime
When you need to fix drift by importing resources that got out of sync
When collaborating with a team that created resources outside Terraform but you want a single source of truth
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  # The configuration must match the imported resource's current state
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "example-instance"
  }
}

This file defines the AWS provider and a resource block for an EC2 instance.

The resource block must match the existing instance you imported, including AMI, instance type, and tags.

This configuration allows Terraform to manage the imported instance without changing it unexpectedly.

Commands
Initializes the Terraform working directory and downloads necessary provider plugins.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes.
Imports the existing AWS EC2 instance with ID i-0abcd1234efgh5678 into Terraform state under the resource aws_instance.example.
Terminal
terraform import aws_instance.example i-0abcd1234efgh5678
Expected OutputExpected
aws_instance.example: Importing from ID "i-0abcd1234efgh5678"... aws_instance.example: Import prepared! Prepared aws_instance for import aws_instance.example: Refreshing state... [id=i-0abcd1234efgh5678] Import successful! The resources that were imported are now in your Terraform state and will be managed by Terraform.
Shows the difference between the imported resource's current state and the written configuration to verify no changes will be made.
Terminal
terraform plan
Expected OutputExpected
No changes. Infrastructure is up-to-date. This means that your configuration matches the imported resource exactly.
Applies the configuration to ensure Terraform manages the resource without changes, confirming the import and configuration are correct.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Refreshing state... [id=i-0abcd1234efgh5678] No changes. Your infrastructure matches the configuration. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the apply without interactive confirmation
Key Concept

If you remember nothing else from this pattern, remember: after importing a resource, you must write matching configuration so Terraform can manage it safely.

Common Mistakes
Importing a resource but not writing matching configuration in the Terraform files
Terraform will see the resource as unmanaged or try to recreate it, causing errors or resource duplication
Write a resource block in your configuration that matches the imported resource's current settings exactly
Writing configuration that differs from the imported resource's actual state before running terraform plan
Terraform will plan to change or destroy the resource unexpectedly, risking downtime or data loss
Check the resource's current settings in the cloud console and replicate them exactly in the Terraform configuration before planning
Summary
Initialize Terraform with terraform init to prepare the working directory.
Import the existing resource into Terraform state using terraform import with the resource ID.
Write Terraform configuration that matches the imported resource's current state exactly.
Run terraform plan to verify no changes will be made.
Apply the configuration with terraform apply to confirm Terraform manages the resource safely.