Import existing resources
Import records an existing resource in the state, so the next plan manages it instead of creating a second one. It changes nothing in VirtScale.
The steps
Section titled “The steps”- Write the resource block, with the attributes the resource has today.
- Import it:
tofu import <address> <id>. - Run
tofu plan. The goal is a plan with no changes. If it shows changes, they are the differences between your block and what exists. Adjust the block until the plan is empty.
tofu import virtscale_instance.web <instance id>
Attributes the API cannot return are left empty. Each resource’s registry page says which.
Import ids
Section titled “Import ids”| Resource | Import id |
|---|---|
virtscale_instance |
<instance id> |
virtscale_instance_firewall |
<instance id> |
virtscale_project |
<project id> |
virtscale_public_ip |
<public ip id> |
virtscale_s3_access_key |
<access key uuid> |
virtscale_s3_bucket |
<bucket id> |
virtscale_ssh_key |
<ssh key id> |
virtscale_vpc |
<project id>/<vpc id> |
A VPC is addressed through its project, so its id has two parts:
tofu import virtscale_vpc.net <project id>/<vpc id>
Where to find an id
Section titled “Where to find an id”Each list is a GET with the same API key. $ORG is your organization id.
| Resource | List |
|---|---|
| Instances | /v1/orgs/$ORG/compute/instances, see List instances |
| Public IPs | /v1/orgs/$ORG/compute/public-ips, see List public IPs |
| SSH keys | /v1/orgs/$ORG/compute/ssh-keys |
| Projects | /v1/orgs/$ORG/projects |
| VPCs | /v1/projects/$PROJECT/vpcs |
| Buckets | /v1/orgs/$ORG/s3/buckets, see List buckets |
| Access keys | /v1/orgs/$ORG/s3/access-keys, see List S3 access keys |
curl -s "https://api.virtscale.nl/v1/orgs/$VIRTSCALE_ORGANIZATION/compute/instances" \
-H "Authorization: Bearer $VIRTSCALE_API_KEY"
The reference page for listing instances still shows the older path, /compute/vms. It is an alias that is
being retired: use /compute/instances.
Per resource
Section titled “Per resource”-
Instance. The image is a create-time input, so import seeds it once from what the instance runs. The hostname and the SSH key names are read back, so an imported instance does not plan a replacement for them.
user_datais never read back: the API does not return it, so an imported instance has none in the state. If your block setsuser_data, the plan replaces the instance. Keep the plan empty withignore_changes:resource "virtscale_instance" "web" { # ... the attributes the instance has today ... user_data = file("cloud-init.yaml") lifecycle { ignore_changes = [user_data] } } -
Firewall. Import it before you apply. The first apply replaces whatever rules the instance holds, without showing them. Import, run
tofu plan, and read the difference: that is what the apply would change. -
SSH key. The state holds the API’s canonical form of the key, without its comment. Your configuration holds the file with its comment. The plan that follows an import is an in-place update that makes no API call. It never replaces the key.
-
VPC. VPCs are not switched on in production yet (
503 not_ready). Once they are, an imported VPC that has a description needs the samedescriptionin your block, or the plan replaces it. -
Project. The default project can be imported to read it. Destroying it is refused with
409 default_project. -
S3 access key. The secret cannot be read back, so an imported key has none.
-
S3 bucket.
force_destroyis the provider’s own and the API does not store it, so import leaves itfalse.
A create that lost its answer
Section titled “A create that lost its answer”Creating an instance or importing an SSH key can reach the API and still lose the answer, on a gateway
error or a dropped connection. The resource then exists and is not in the state. The error says so. The
next apply reports name_taken. Find the resource with a list above, then import it:
tofu import virtscale_ssh_key.laptop <ssh key id>
A public IP can end up the same way: it is never retried after a 5xx or a lost connection, so the address
may exist without being in the state. List the public IPs and import it.