You are currently viewing How to Import Existing Resources into Terraform State
How to Import Existing Resources into Terraform State

How to Import Existing Resources into Terraform State

Sometimes, you create resources outside of Terraform and later want to manage them using Terraform. In this case, you can use the terraform import command to import those existing resources into Terraform state.

The terraform import command requires two arguments: the resource type and the resource ID. The resource type is the type of resource you want to import, such as aws_instance or google_compute_instance. The resource ID is a unique identifier for the resource that you want to import.

Here’s an example of how to import an existing AWS instance into Terraform state:

  1. First, create a new Terraform configuration file that defines the AWS instance resource, but leave the resource block empty:
resource "aws_instance" "example" {
}

2. Next, run the terraform import command, specifying the resource type and ID:

terraform import aws_instance.example i-0123456789abcdefg
  1. Terraform will fetch the current state of the resource and create a new state file with the imported resource. You can verify that the resource has been imported by running terraform plan or terraform apply.

Terraform does not support importing all resource types and attributes. Check the documentation before importing a resource to confirm it’s supported.

Use caution when importing resources into Terraform state, as it helps bring existing infrastructure under Terraform management. Make sure you understand the implications of importing resources, such as potential conflicts with existing resources in your Terraform configuration, before proceeding.

https://www.youtube.com/@techknowledgehuborg

Leave a Reply