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.

Reading the plan

Two habits save time:

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:

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:

Generated from the provider schema for version 0.11.0. How to install the provider: Terraform provider.