VPS with an API: creating and deleting servers from code, Terraform, tokens and billing
A host's API lets a program do everything you usually do with buttons in the panel: create a server, choose an image and location, add an SSH key, reboot, take a snapshot, change the plan and delete the machine. You need it where there are many servers or they live briefly: test environments for every branch, build runners, temporary machines for computations, clusters that grow with load. The table above lists plans from hosts that offer such an API. Below is what to look at before writing the first line of automation.
What to check in the API before renting
Hosts' APIs vary a lot in completeness. Before building processes on one, open the documentation and check:
- The set of operations. Creating and deleting are available almost everywhere, but snapshots, extra disks, private networks, floating IPs and firewall rules are not always.
- cloud-init or user-data. The ability to pass an initial setup script at creation time: a user, keys, packages. Without it every new machine has to be finished off separately.
- Asynchrony. Usually a create request returns a task ID, and the server becomes available tens of seconds or minutes later. The script must poll the status rather than connect over SSH right after the response.
- Rate limits. APIs limit the number of calls per minute or hour. Mass creation of machines in a loop without pauses hits them first.
- Ready-made tools. A provider for Terraform and OpenTofu, Ansible modules, an official command-line utility and libraries in the languages you need.
Terraform, OpenTofu and Ansible
Writing API calls by hand is only worth it for simple scenarios. For infrastructure of several machines it is more convenient to describe it declaratively: Terraform or its open fork OpenTofu keeps the desired state in code, the plan command shows what will change, apply applies the changes, and destroy deletes everything that was created. This protects you from "hand-made" servers that nobody remembers.
Two things people forget. The state file contains resource IDs and often secrets in plain text, so it is not committed to the repository but kept in a remote backend with locking, so that two runs cannot change the infrastructure at the same time. And Terraform is responsible for creating machines but not for what is inside them: installing packages, configuring services and updates are usually handled by Ansible. A typical combination is that Terraform creates the servers and outputs their addresses, and Ansible uses this list to deploy the application. An example of such a setup for a cluster is on the VPS for Kubernetes page, and for temporary build machines there is the VPS for CI/CD runners selection.
Tokens and access rights
An API token can create servers worth thousands of dollars or delete a production database, so treat it like the password to your account:
- If the host allows it, issue tokens with limited rights: read-only for monitoring and cost tracking, write access for the pipeline that actually creates machines.
- Separate projects: a dedicated project or sub-account for test environments, so that a CI token cannot see production.
- Keep the token in CI secrets or a secrets manager, not in code, not in a
.tfvarsfile under Git and not in build logs. - Set an expiry date if supported, and rotate tokens when an employee leaves. Binding to IP addresses, where available, greatly reduces the damage from a leak.
- Enable two-factor authentication on the account: tokens are issued from the panel, and its password should not be the only protection.
Paying for servers created from code
Automation comes into its own with hourly billing: a machine created for two hours costs two hours. But this is exactly where it is easiest to lose money without noticing.
- Orphaned resources. The script crashed between creation and deletion, and the server keeps running. Mark every machine with a tag or label showing its source and lifetime, and run a scheduled cleanup of everything older than it should be.
- Not only servers. Snapshots, separate disks and reserved IP addresses are often billed separately and are not deleted together with the machine. Check them in the same cleanup.
- A stopped server is not free. At most hosts a stopped machine keeps being billed for its disk and address; the only way to stop paying is to delete it.
- Limits and alerts. Set a spending alert threshold and, if the host allows it, a limit on the number of servers in the account. A bug in a loop should not be able to create a hundred machines.
For environments that are brought up for a check and deleted right away, see also the VPS for testing selection.