Terraform
Import resources overview
If you have existing infrastructure resources, you can import them to your Terraform workspace so that you can begin managing the resources as code.
Workflows
Importing unmanaged resources to your workspace requires an import block that specifies the unique infrastructure resource ID to import. The block also declares an address for the imported resource in state. Additionally, you must create a destination resource block that matches the address declared in the import block. The resource block represents the resource in the configuration.
You can import single resources or small batches of resources or create queries to discover large sets of unmanaged resources to import them in bulk.
Import resources in bulk
When you need to identify and import large sets of infrastructure resources, you can define queries as HCL, add the results to your Terraform configuration, and use the terraform apply command to import the discovered resources into your workspace. Refer to Import resources in bulk for more information.
If your organization uses HCP Terraform, you can view query results and apply import configurations in the UI. Refer to Import existing resources to state for more information.
Import individual resources
The workflow for importing single resources or small batches of resources works best when you can easily access unique infrastructure resource IDs and other attributes from your cloud provider. In this workflow, manually write import and resource blocks and run the terraform apply command to import the resources.
Alternatively, you can write only the import block and run the terraform plan command with the generate-config-out flag to generate the resource blocks. Refer to Import a single resource for more information.
Resource identity
Terraform uniquely identifies resources according to either the ID assigned by the cloud provider or a collection of specific attributes defined by the provider. To reference a resource identity in Terraform configuration, you can use either id or identity attribute.
The identity attribute for s3_bucket resources available in the AWS provider, for example, uniquely identifies buckets according to the following attributes:
To reference the unique identity of an aws_instance resource, you can use the id attribute.
Refer to your provider documentation for information about how to reference resource identities. For information about referencing resource identities in Terraform configuration, refer to the import block reference.