Skip to main content

Import existing resources

Import lets Terraform manage a resource that you created on FPT Cloud Portal without deleting or recreating that resource. After the import, you change the resource with Terraform like any other resource, and your configuration must describe the current state of the resource exactly.

Prerequisites​

  • ✅ You have completed Set up and configure.
  • ✅ Terraform 1.5 or later, to use the import block and generate configuration.
  • ✅ FPT Cloud provider version 0.3.81 or later, the first version that supports importing Instances and disks.
  • ✅ The resource is in a stable state and is not in the middle of an operation such as create, resize or delete.

Resources that support import​

ResourceImport ID formatSince provider version
fptcloud_instancevpc/<vpc_id>/instance/<instance_id> or vpc/<vpc_id>/instance_name/<instance_name>0.3.81
fptcloud_storagevpc/<vpc_id>/storage/<storage_id>0.3.81
fptcloud_snapshotvpc/<vpc_id>/snapshot/<snapshot_id>0.3.74
fptcloud_ssh_key<ssh_key_id>See the note below.
fptcloud_tagging<tag_id>—

Get the IDs from the resource details page on the Portal, or with the matching data source.

warning

After you import fptcloud_ssh_key, the provider does not read public_key back from FPT Cloud. If you do not handle this, the plan marks the SSH key with forces replacement. Set public_key to the exact content of the existing key and add lifecycle { ignore_changes = [public_key] } to the resource, or use the fptcloud_ssh_key data source instead of importing.

Import an Instance​

  1. Create an import.tf file with an import block:

    import {
    to = fptcloud_instance.legacy_app
    id = "vpc/<vpc_id>/instance/<instance_id>"
    }
  2. Let Terraform generate the starting configuration:

    terraform plan -generate-config-out=generated.tf
  3. Open generated.tf and fill in ssh_key or password with the exact value used when the Instance was created. FPT Cloud does not return these two values. If image_name is null, fill in the name of the image used to create the Instance.

  4. Run terraform plan again. Continue only when the plan no longer contains a forces replacement line.

  5. Run terraform apply to save the import result to the state.

The first plan after the import may report that ssh_key or password is added. Applying this plan only saves the value to the state, and the Instance is not changed.

danger

Do not apply a plan that reports forces replacement for a resource you have just imported. Terraform would delete the running Instance together with the data on its root disk. Edit the configuration until the plan no longer recreates the resource.

Some notes when you import an Instance:

  • If several Instances have the same name, import by ID.
  • Instances that use an NVMe disk do not support import yet.

Import a data disk​

import {
to = fptcloud_storage.legacy_data
id = "vpc/<vpc_id>/storage/<storage_id>"
}

Only an EXTERNAL disk in the ready state can be imported.

warning

If the disk is attached to an Instance, keep the instance_id argument pointing to that Instance in the configuration. Without instance_id, the plan reports that instance_id changes to null, and apply detaches the disk from the Instance.

Import several resources at once​

Declare several import blocks in the same file, then generate the configuration once:

import {
to = fptcloud_instance.web["web-01"]
id = "vpc/<vpc_id>/instance_name/web-01"
}

import {
to = fptcloud_instance.web["web-02"]
id = "vpc/<vpc_id>/instance_name/web-02"
}

With an address such as fptcloud_instance.web["web-01"], declare the fptcloud_instance.web resource yourself with for_each = toset(["web-01", "web-02"]) in the configuration before you run terraform plan.

Import with the terraform import command​

With Terraform older than 1.5, use the terraform import command. Declare the resource in the configuration first, then run:

terraform import fptcloud_instance.legacy_app vpc/<vpc_id>/instance/<instance_id>

Resources that do not support import yet​

Subnets, Security Groups, Security Group rules, Floating IPs and Instance Groups cannot be imported yet. To use these resources in your configuration, reference them with a data source:

data "fptcloud_subnet" "existing" {
vpc_id = data.fptcloud_vpc.example.id

filter {
key = "name"
values = ["legacy-subnet"]
}
}

Then use data.fptcloud_subnet.existing.subnets[0].id wherever subnet_id is needed. See also Look up information with data sources.

Next steps​