Skip to main content
Terraform is not limited to resources it creates. Infrastructure that already exists — whether created manually in the Gcore Customer Portal, through the API, or by another Terraform configuration — can be brought under Terraform management without being recreated. This process is called import and is commonly used when teams migrate existing environments to infrastructure-as-code, recover from lost state files, or reorganize infrastructure between configurations.

Import process

Without import, Terraform does not know about resources outside its state file, so terraform plan shows them as + create — meaning it would create duplicates. Import solves this:
  1. Terraform reads the resource’s current state from the Gcore API using the resource ID.
  2. It writes that state into terraform.tfstate.
  3. From that point, terraform plan compares the configuration against the recorded state and reports only the actual differences.

Prepare for import

Every import requires a resource type and a resource ID; find both in the Import section of each resource’s page on the Terraform Registry. Terraform offers two import methods — choose based on scope and team workflow:

Run an import command

The terraform import command binds an existing resource to a resource block in the configuration and writes its state immediately. It works with all Terraform CLI versions and requires no changes to .tf files beyond adding the resource block.

Step 1. Write a resource block

Create a resource block in the .tf configuration with at minimum the required fields, listed in the resource’s schema on Terraform Registry. For a Gcore project:

Step 2. Run terraform import

The import command format is:
Where <resource_address> is <resource_type>.<local_name> — matching the resource block written in Step 1.
Expected output:

Step 3. Run terraform plan after import

Run plan to verify there is no attribute drift between the configuration and the imported state:
If terraform plan still shows changes, the resource block in .tf has attributes that do not match the imported state. Update the configuration until terraform plan reports no changes.

Add an import block to configuration

The import block is the recommended approach for team environments. It makes the import operation visible in terraform plan, so the intent is reviewable before state is modified.

Step 1. Write an import block

Add an import block to any .tf file alongside the target resource block:
The to field must match the <resource_type>.<local_name> of the resource block.

Step 2. Run terraform plan

Run plan to preview the full resource state before the import is applied:
The plan shows the full resource state that will be written to the state file.

Step 3. Apply

After reviewing the plan, apply to write the resource into state:
After a successful import, remove the import block from the configuration — it is no longer needed and will cause an error if left in place after the resource is already in state.

Generate configuration automatically

When importing a resource that has many attributes, writing the resource block manually is time-consuming. Use the -generate-config-out flag to generate the block automatically.

Step 1. Write only the import block (no resource block)

Add only an import block — no resource block. Terraform generates it automatically in the next step:

Step 2. Run terraform plan with -generate-config-out

Pass the flag to specify the output file name. Terraform generates the resource block and writes it to that file:

Step 3. Review and prune generated.tf

Terraform writes the full resource schema to generated.tf:
Remove read-only and default-value attributes from generated.tf, then move the pruned configuration into the main .tf files and delete generated.tf.
Config generation is marked experimental. The generated output may include read-only attributes that Terraform does not accept as input. Remove any attribute that the provider schema marks as read-only (client_id, created_at, id, is_default, state in this example) before running terraform apply.

Import ID reference

The import ID format varies by resource. Common Gcore resources: Find project and region IDs in the Gcore Customer Portal or via the data "gcore_cloud_project" and data "gcore_cloud_region" data sources; resource UUIDs appear in the portal URL and in Gcore API responses. Check the Import section of the specific resource page on Terraform Registry for the exact format.

Inspect and manage state

After import, use terraform state subcommands to inspect what is recorded:
terraform state rm removes the resource from Terraform’s tracking only. The actual Gcore resource is not deleted. Use it to stop managing a resource with Terraform without destroying it.