My Digital Garden

Example of creating Ubuntu server in Linode

Example of creating Ubuntu server in Linode

Here is an example Terraform model to build a simple Ubuntu server, suitable for example for website hosting (4 cores, 8MB RAM, 160MB disk).

In real life you would combine this other resources such as DNS entries, Nodebalancer configuration, block or object storage, managed DB etc.

variable "linode_token" {
  description = "Linode API token"
}

variable "ubuntu_username" {
  description = "username for the non-root user"
  sensitive = true
}

variable "ubuntu_password" {
  description = "Password for the Ubuntu user"
  sensitive = true
}

resource "linode_instance" "webserver" {
    //image               = "linode/ubuntu22.04"
    label               = "webserver" 
    region              = "eu-west"
    group               = "Production"
    boot_config_label   = "boot_config"
    private_ip          = true
    resize_disk         = false
    watchdog_enabled    = true
    tags                = []
    type                = "g6-standard-4"
    timeouts {
    }
    alerts {
        cpu            = 267
        io             = 10000
        network_in     = 10
        network_out    = 25
        transfer_quota = 80
    }
    
}


resource "linode_instance_disk" "webserver_boot_disk" {
  label             = "Ubuntu 22.04 LTS Disk"
  linode_id         = linode_instance.webserver.id
  filesystem        = "ext4"
  size              = 79808
  image             = "linode/ubuntu22.04"
  stackscript_id    =  1234 # this should be reference to a stackscript to define initial config
  stackscript_data = {
    USER            = var.ubuntu_username
    USER_PASSWORD   = var.ubuntu_password
    UPGRADE         = "yes"
    SSH_PORT        = 22
    ROOT_LOCK       = "yes"
    HOSTNAME        = "webserver"

  }

  # Any of authorized_keys, authorized_users, and root_pass
  # can be used for provisioning.
  #authorized_keys = [ "ssh-rsa AAAA...Gw== user@example.local" ]
  #authorized_users = [ data.linode_profile.me.username ]
  #root_pass = "terr4form-test"
}

resource "linode_instance_disk" "webserver_swap" {
  label             = "4096MB Swap Image"
  linode_id         = linode_instance.webserver.id
  filesystem        = "swap"
  size              = 4096
  

}

resource "linode_instance_config" "webserver_boot_config" {
  label         = "boot_config"
  linode_id     = linode_instance.webserver.id
  root_device   = "/dev/sda"
  kernel        = "linode/grub2"
  run_level     = "default"
  virt_mode     = "paravirt"
  booted = true

  devices {
    sda {
      disk_id = linode_instance_disk.webserver_boot_disk.id
    }
    sdb {
      disk_id = linode_instance_disk.webserver_swap.id
    }
  }

  helpers {
            devtmpfs_automount = true
            distro             = true
            modules_dep        = true
            network            = true
            updatedb_disabled  = true
        }
}

For an example StackScript that wouild go with this Terraform model, see Linode StackScript for basic Ubuntu configuration

See also