So far in this book, we have seen the normal use of Terraform, which is to write a Terraform configuration that is going to be executed and applied by Terraform. This execution will provision or apply changes to an infrastructure that will be reflected in the Terraform state file.
In certain scenarios, however, it may be necessary to import resources that have already been provisioned into the Terraform state file. Examples of such scenarios include the following:
-
Resources have been provisioned manually (or by scripts) and now it is desired that their configuration is in the Terraform configuration and in the state file.
-
A Terraform state file that contains the configuration of an infrastructure has been corrupted or deleted and regeneration is desirable.
In this recipe, we will discuss how, with the assistance of Terraform commands, we can import the configuration of resources that have already been provisioned into the Terraform state file.