aboutsummaryrefslogtreecommitdiff
path: root/vps.tf
blob: 8579e768b772a80d98b2a16af4ad8d53629646d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Input variables

variable "do_token" {
  type        = string
  description = "DigitalOcean API token."
}

variable "tld" {
  type        = string
  description = "Root Top-Level Domain. Subdomains will be derived from it."
}

variable "nextcloud_tld_prefix" {
  type        = string
  description = "DNS prefix used for the Nextcloud installation. Does not contain a dot at the end."
}

variable "hostname" {
  type        = string
  description = "Human name of the host. This is a pet name, not cattle name :)"
}

variable "volume_name" {
  type        = string
  description = "Name of the volume, which will also be the name of it's mount point."
}

# DigitalOcean

provider "digitalocean" {
  token   = var.do_token
  version = "~> 1.1"
}

resource "digitalocean_ssh_key" "client" {
  name       = "terraform-vps-client"
  public_key = file("${path.module}/secrets/ssh/vps-box-client.pub")
}

## Droplet and volume

resource "digitalocean_droplet" "vps" {
  image      = "ubuntu-18-04-x64"
  name       = var.hostname
  region     = "nyc3"
  size       = "s-1vcpu-1gb"
  backups    = true
  ipv6       = true
  monitoring = true

  user_data = file("${path.module}/generated/user-data.sh")

  ssh_keys = [
    "${digitalocean_ssh_key.client.fingerprint}",
  ]

  connection {
    user        = "root"
    type        = "ssh"
    private_key = file("${path.module}/secrets/ssh/vps-box-client")
    timeout     = "2m"
    host        = digitalocean_droplet.vps.ipv6_address
  }

  provisioner "remote-exec" {
    inline = ["echo 'SSH is up! Noop remote-exec is done.'"]
  }
}

resource "digitalocean_volume" "vps_persistent_volume" {
  region                  = "nyc3"
  name                    = var.volume_name
  size                    = 10
  initial_filesystem_type = "ext4"
  description             = "Persistent disk to store docker volumes contents across droplets being created and destroyed"
}

resource "digitalocean_volume_attachment" "foobar" {
  volume_id  = digitalocean_volume.vps_persistent_volume.id
  droplet_id = digitalocean_droplet.vps.id
}

## DNS and IP configuration

resource "digitalocean_floating_ip" "vps_public_ip" {
  region = digitalocean_droplet.vps.region
}

resource "digitalocean_floating_ip_assignment" "vps_public_ip_assignment" {
  ip_address = digitalocean_floating_ip.vps_public_ip.id
  droplet_id = digitalocean_droplet.vps.id
}

output "public_floating_ip" {
  value = digitalocean_floating_ip.vps_public_ip.ip_address
}

resource "digitalocean_domain" "vps_tld" {
  name = var.tld
}

resource "digitalocean_record" "at_sign" {
  domain = digitalocean_domain.vps_tld.name
  type   = "A"
  name   = "@"
  value  = digitalocean_floating_ip.vps_public_ip.ip_address
}

resource "digitalocean_record" "nextcloud" {
  domain = digitalocean_domain.vps_tld.name
  type   = "CNAME"
  name   = var.nextcloud_tld_prefix
  value  = "${digitalocean_domain.vps_tld.name}."
}