Using the Import Block
Why
There are various reasons that may lead you to using the import block. For example:
- Fixing click ops drift
- After removing a changed resource from state and wanting to import it back in
- Our code base did not previous support a configuration, but now it does and you want to manage the resource via code
The import block allows you to import an existing resource into the existing state file, allowing you to manage it by code. This differs from the data block in that the data block only allows reference to the resource and not management of the resource.
Note
Import is not supported for all Terraform resources, and Hashicorp is adding support for modules with new Terraform versions.
You must confirm if import of a specific resource is supported by reviewing the Hashicorp Terraform Registry. If it is, a section titled Import will be present at the bottom of the page.
How
Expect the unexpected
It goes without saying, but you should always run a plan to confirm your changes will not clobber existing infrastructure, or to make sure that all components you expect to import actually import.
-
First, you will define the resource in your code. This is an important step -- the import block brings the resource into state, but you must define it in code as if you were creating it from scratch. We will use an example of importing an AWS security group.
Warning
If there are any differences between your code and the existing resource, your code will overwrite the existing resource.
Tip
Keep note of lines 2, 4, 5, and 6. They will be used in the next step.
In this example, you will notice that Line 6 is being created in code and is not defined in the UI screenshot below. This is expected, and in the
terraform planwill appear as a change.Screenshot - Correlating info in AWS

-
If it does not exist, create
./src/imports.tf. This file will contain information on the resource you are importing. -
Populate
imports.tfwith resource information for the resource(s) you wish to import.- Obtain the Security Group Rule ID from the AWS console.

- Confirm the path to map the resource to. You can either reference a similar resource in the output of a
terraform planor track it down in code, as shown here. You will then concatenate that path with the resource information defined in Step 1. This example results in a full name ofmodule.vpc_security_group_rule.aws_vpc_security_group_ingress_rule_vpc_security_group_ingress_rule["Epic.HSWSG.Allow_SMB.ingress"].
- Now you can write your import block.
- Obtain the Security Group Rule ID from the AWS console.
-
After configuring the
imports.tfand.tfvars, save your changes and run aterraform plan. The results of your plan will include # of items to be imported or changed. Be sure to carefully review the results for the expected changes. -
If the results are as expected, run your
terraform apply. -
After the apply, you must clean up your import. Either comment out or delete the contents of your
imports.tffile, or delete the file in it's entirety.