Importing Existing Resources
Learn how to import already existing cloud infrastructure into your Terraform state, bringing pre-existing resources under IaC management.
Importing Existing Resources is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Bring Existing Infra to IaC
Sometimes you have cloud resources that were created manually or by other tools. Terraform import allows you to bring these existing resources under Terraform's management.
This means Terraform will then track their state and future changes can be applied via your configuration files. It's a key step for adopting Infrastructure as Code (IaC) in a "brownfield" environment.
When You Need to Import
Importing is useful in several situations:
- Migrating existing infrastructure: Bringing resources created outside Terraform into your IaC workflow.
- Adopting Terraform gradually: Starting with existing resources rather than recreating everything.
- Recovering from state loss: Re-associating Terraform with resources if your state file is lost (though this is rare with remote state).
Mastering the Import Command
The basic structure of the terraform import command is straightforward. It tells Terraform to find an existing resource in your cloud provider and add it to your state file, associating it with a resource block in your configuration.
It requires two main pieces of information: the Terraform address of the resource and the cloud provider's ID for that resource.
What's a Resource Address?
The resource address is how Terraform identifies a specific resource within your configuration. It follows the pattern resource_type.resource_name.
resource_type: The type of resource (e.g.,aws_s3_bucket,azurerm_resource_group).resource_name: The local name you give the resource in your Terraform configuration (e.g.,my_bucket,web_app).
This address must already exist in your .tf files.
Locating the Cloud ID
Every resource in a cloud provider (like AWS, Azure, GCP) has a unique identifier, often called an ID. You'll need this ID to tell Terraform *which* specific existing resource to import.
You can find this ID in the cloud provider's console, via their CLI, or through their API. For example, an AWS S3 bucket's ID is its name, while an EC2 instance has an instance ID like i-0abcdef1234567890.
Define HCL Before Import
Crucial Step: Before running terraform import, you must have a corresponding resource block defined in your Terraform configuration files (.tf files).
This block tells Terraform what type of resource to expect and what attributes it should have. Terraform uses this definition to understand the resource's schema during import.
HCL for an S3 Bucket
Let's say you have an existing AWS S3 bucket named my-existing-coddykit-bucket-123. First, define a resource block for it in your main.tf:
resource "aws_s3_bucket" "my_imported_bucket" {
bucket = "my-existing-coddykit-bucket-123"
# Add other expected attributes if known
# e.g., acl = "private"
}Importing the S3 Bucket
Now that you have the HCL definition, you can run the terraform import command. Replace my-existing-coddykit-bucket-123 with the actual name/ID of your existing bucket.
Make sure you've already run terraform init in your directory!
terraform import aws_s3_bucket.my_imported_bucket my-existing-coddykit-bucket-123Check Terraform State
After a successful import, Terraform will have added the resource to your state file. You can verify this using terraform state show, which displays the attributes of the imported resource as seen by Terraform.
It's crucial to then run terraform plan to ensure there are no unexpected differences between your HCL and the imported resource's actual state.
terraform state show aws_s3_bucket.my_imported_bucketQuick Check: Import Order
You want to import an existing AWS EC2 instance into Terraform. Which of the following steps MUST be completed *before* running the terraform import command?
Recap: Bringing Infra Home
You've learned how to bring existing cloud resources under Terraform's management using terraform import.
- First, define the resource block in your HCL.
- Then, identify the cloud provider's resource ID.
- Finally, run
terraform import ADDRESS ID. - Always follow up with
terraform planto reconcile any differences.
This powerful command helps bridge the gap between manually managed infrastructure and a fully IaC-driven environment.
Frequently asked questions
Is the “Importing Existing Resources” lesson free?
Yes — the full text of “Importing Existing Resources” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Importing Existing Resources”?
Learn how to import already existing cloud infrastructure into your Terraform state, bringing pre-existing resources under IaC management. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Importing Existing Resources” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Remote State Backends
- State Locking and Consistency
- Importing Existing Resources
- Refactoring State with moved and removed