Skip to content

Commit

Permalink
feat: make postgresql on persistent storage
Browse files Browse the repository at this point in the history
make data survice even terraform destroy (volume is detached from VM lifecycle)
data is not migrated, when volume already populated
  • Loading branch information
phandox committed Mar 4, 2023
1 parent 1e08c96 commit f2d26f4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions infra-modules/postgres-vm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ resource "digitalocean_firewall" "db-fw" {
source_addresses = [var.vpc.ip_range]
}
}

resource "digitalocean_volume_attachment" "persistent-volume" {
count = var.volume_id != null ? 1 : 0
droplet_id = digitalocean_droplet.psql-vm[local.instances[0]].id
volume_id = var.volume_id
}
8 changes: 7 additions & 1 deletion infra-modules/postgres-vm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ variable "vpc" {
ip_range = string
})
description = "VPC network to where instance live"
}
}

variable "volume_id" {
type = string
default = null
description = "Volume ID for attaching persistent storage to instance"
}
36 changes: 36 additions & 0 deletions infra-modules/volumes/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
variable "name" {
type = string
description = "Name of network volume"
}
variable "region" {
type = string
default = "fra1"
description = "Region of network volume. Must match droplet"
}
variable "size" {
default = 1
type = number
description = "Volume size in GiB"
}

variable "fs_label" {
default = "example"
type = string
description = "Filesystem label for volume, useful for mounting"

validation {
condition = length(var.fs_label) <= 16
error_message = "Max 16 characters for FS label for ext4 filesystem"
}
}
resource "digitalocean_volume" "volume" {
name = var.name
region = var.region
size = var.size
initial_filesystem_type = "ext4"
initial_filesystem_label = var.fs_label
}

output "volume_id" {
value = digitalocean_volume.volume.id
}

0 comments on commit f2d26f4

Please sign in to comment.