Terraform provider reference / guides / lifecycle
A project end to end
The same run as the API guide A server from start to finish,
written as one configuration. Read it before you let a pipeline create and destroy servers on its
own: the interesting part is not the creation, it is what stays behind after destroy.
The configuration
# Operating system images of the server type; the first one is used below.
data "fiberax_images" "os" {
server_type_id = 4
type = "os"
}
resource "fiberax_ssh_key" "laptop" {
name = "laptop"
public_key = file("~/.ssh/id_ed25519.pub")
}
resource "fiberax_network" "backend" {
name = "backend"
cidr = "10.42.0.0/24"
}
resource "fiberax_server" "app" {
name = "app-01"
server_type_id = 4
image = data.fiberax_images.os.images[0].id
cores = 2
memory_mb = 4096
disk_gb = 40
public_ipv4 = { reuse = true }
ssh_keys = [fiberax_ssh_key.laptop.id]
}
# The private card is its own resource: a server may have several, and each one
# is managed separately.
resource "fiberax_network_interface" "app_backend" {
server_id = fiberax_server.app.id
network_id = fiberax_network.backend.id
}
resource "fiberax_server_firewall" "app" {
server_id = fiberax_server.app.id
enabled = true
policy_in = "drop" # anything not allowed below is dropped
policy_out = "accept" # outbound traffic is not restricted
# The rule set is ONE field, replaced as a whole, and the order in it is the
# order the platform checks: the first matching rule wins.
rules = [
{
direction = "in"
action = "accept"
protocol = "tcp"
destination_port = "22"
description = "ssh"
},
{
direction = "in"
action = "accept"
protocol = "tcp"
destination_port = "80,443"
description = "web"
},
]
}
resource "fiberax_server_backup_policy" "app" {
server_id = fiberax_server.app.id
enabled = true
days = ["mon", "wed", "fri"]
hour = 2
minute = 0
# `keep` is read-only here: how many backups are kept comes from the product,
# and writing it is refused as an unknown field.
}
What the order looks like
Terraform works out the order from the references, and there is nothing to arrange by hand: the card
waits for both the network and the server, the firewall and the policy wait for the server. A
private network takes about two minutes to appear on the platform, and the provider waits for it, so
the first apply of a new network is the slowest step of the run.
What destroy does not remove
This is the part worth reading twice.
- A public address taken with
reuse = trueoutlives the server. Destroying the server detaches it, and it stays in the project and stays billed. One taken withnew = trueis released together with the server and cannot be taken back; one named byidbelongs to its ownfiberax_ipresource, whichdestroyreleases. Manage addresses as their own resource when you want to keep them across servers. - Backups outlive the server too. A backup policy is a schedule, not the archives it produced; removing the policy stops future backups and keeps the ones already taken.
- A network with servers still on it refuses to go. Terraform destroys the cards first because they depend on it, so this only bites when something outside the configuration is still attached.
Reading the plan
Two habits save time:
terraform planreads the data sources, so it also proves the token and the mirror work. Run it before you write the rest of the configuration, not after.- A change that the platform cannot make in place shows up as replacement. Server type, image and
the set of GPU cards are like that: they are chosen once, and a different value means a different
server. So is the form of
public_ipv4, and a replacement treats the old server's address as a destroy does: one taken withnew = truegoes back to the shared pool first, and the new server takes a free address from the shared pool, which may or may not be the same one - do not rely on keeping it. The plan warns about this for a change in the configuration, not for-replaceortaint- see Public addresses and what they cost. ssh_keysof a server is NOT like that, although keys are also installed only at creation. A change of the list is recorded in state and touches nothing on the server, and the plan says so. The usual reason for such a change is harmless: afiberax_ssh_keyrenamed in the configuration, or deleted from the pool in the client area and created again, comes back with a new ID and the same key material. To install a new key on a running server usePOST /v1/servers/{id}/actions/add-ssh-keys; to revoke one, remove it inside the guest.- The firewall rule set is a single field. Writing
rules = []is not the same as leaving the field out - an empty list means "remove every rule", and the field is required precisely so that a forgotten one cannot mean that by accident.
A replacement and values known only during apply
A replacement destroys the old server first and creates the new one after it. The rules for a new
server - a network, a name, a password or keys, the image and the limits of the type - are checked
on the plan, but only for values the plan knows. When the image, the type or another such field is
an expression of a resource created in the same run, its value is known only during apply, and it
is checked then - after the old server, its disks and an address taken with new = true are gone.
If the value breaks a rule, the run stops with no server at all. The plan of such a replacement
warns and names the fields - except for terraform taint, and for -replace when only a field that
does not replace the server (name, password, ssh_keys, the size, plan_id) is unknown: the
provider cannot see those replacements coming, so check such values yourself first.
The safe way:
- Apply the resource the value comes from first,
terraform apply -target=<that resource>, then the change. The plan then knows every value and refuses a broken one before anything is destroyed.
lifecycle { create_before_destroy = true } creates the new server first, but ON THE SERVER ALONE
IT IS NOT ENOUGH. Every resource that takes server_id from the server - fiberax_disk,
fiberax_network_interface, fiberax_server_firewall, fiberax_server_backup_policy, and on a
router the fiberax_router_* resources - is replaced together with it and, without a
create_before_destroy of its own, destroyed BEFORE the new server is checked: the old server
stays, its data disk with the data on it does not, and a disk removal shuts the old server down.
Only with create_before_destroy on the server AND on each of those resources does a refusal leave
the old server and everything on it as it was. What it costs:
- for a while both servers exist, and both are billed; so are both disks of each data disk - the new one is created EMPTY on the new server, the data stays on the old one until it is destroyed;
- the order protects the old resources only while the new server is refused. If the new server is created and a later step of the same run fails (a new disk, a card), the old disk may already be destroyed while the old server is still there - the next apply destroys the old server;
- the plan warns that a server with the same name already exists - the old one; this is expected;
- a server with
public_ipv4 = { new = true }gets a new address, and the old one goes back to the shared pool when the old server is destroyed; - a server with
public_ipv4 = { reuse = true }takes another free address of the project, or orders a new billed one when there is none, and the old server's address stays with the project and stays billed after the old server is destroyed; - an address named with
idcannot move this way: it is still attached to the old server when the new one asks for it, so the creation is refused and nothing changes. Keep such a server withoutcreate_before_destroyand use the first way.
Generated from the provider schema for version 0.11.0. How to install the provider: Terraform provider.