Skip to content

Commit

Permalink
implement (un)managed volumes for aws, gcp and azure
Browse files Browse the repository at this point in the history
  • Loading branch information
mboisson committed Jan 14, 2025
1 parent 237d6b4 commit e74f90b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
18 changes: 15 additions & 3 deletions aws/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ resource "aws_instance" "instances" {
}

resource "aws_ebs_volume" "volumes" {
for_each = module.design.volumes
for_each = {
for x, values in module.design.volumes : x => values if lookup(values, "managed", true)
}
availability_zone = local.availability_zone
size = each.value.size
type = lookup(each.value, "type", null)
Expand All @@ -140,6 +142,16 @@ resource "aws_ebs_volume" "volumes" {
Name = "${var.cluster_name}-${each.key}"
}
}
data "aws_ebs_volume" "existing_volumes" {
for_each = {
for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true)
}

filter {
name = "tag:Name"
values = ["${var.cluster_name}-${each.key}"]
}
}

locals {
device_names = [
Expand All @@ -151,7 +163,7 @@ locals {
resource "aws_volume_attachment" "attachments" {
for_each = module.design.volumes
device_name = local.device_names[index(module.design.volume_per_instance[each.value.instance], replace(each.key, "${each.value.instance}-", ""))]
volume_id = aws_ebs_volume.volumes[each.key].id
volume_id = try(aws_ebs_volume.volumes[each.key].id, aws_ebs_volume.existing_volumes[each.key].id)
instance_id = aws_instance.instances[each.value.instance].id
skip_destroy = true
}
Expand All @@ -175,7 +187,7 @@ locals {
pv_key => {
for name, specs in pv_values:
name => merge(
{ glob = "/dev/disk/by-id/*${replace(aws_ebs_volume.volumes["${x}-${pv_key}-${name}"].id, "-", "")}" },
{ glob = try("/dev/disk/by-id/*${replace(aws_ebs_volume.volumes["${x}-${pv_key}-${name}"].id, "-", "")}", "/dev/disk/by-id/*${replace(aws_ebs_volume.existing_volumes["${x}-${pv_key}-${name}"].id, "-", "")}") },
specs,
)
} if contains(values.tags, pv_key)
Expand Down
13 changes: 11 additions & 2 deletions azure/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,27 @@ resource "azurerm_linux_virtual_machine" "instances" {
}

resource "azurerm_managed_disk" "volumes" {
for_each = module.design.volumes
for_each = {
for x, values in module.design.volumes : x => values if lookup(values, "managed", true)
}
name = format("%s-%s", var.cluster_name, each.key)
location = var.location
resource_group_name = local.resource_group_name
storage_account_type = lookup(each.value, "type", "Premium_LRS")
create_option = "Empty"
disk_size_gb = each.value.size
}
data "azurerm_managed_disk" "existing_volumes" {
for_each = {
for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true)
}
name = format("%s-%s", var.cluster_name, each.key)
resource_group_name = local.resource_group_name
}

resource "azurerm_virtual_machine_data_disk_attachment" "attachments" {
for_each = module.design.volumes
managed_disk_id = azurerm_managed_disk.volumes[each.key].id
managed_disk_id = try(azurerm_managed_disk.volumes[each.key].id, azurerm_managed_disk.existing_volumes[each.key].id)
virtual_machine_id = azurerm_linux_virtual_machine.instances[each.value.instance].id
lun = index(module.design.volume_per_instance[each.value.instance], replace(each.key, "${each.value.instance}-", ""))
caching = "ReadWrite"
Expand Down
14 changes: 11 additions & 3 deletions gcp/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,25 @@ resource "google_compute_instance" "instances" {
}

resource "google_compute_disk" "volumes" {
for_each = module.design.volumes
for_each = {
for x, values in module.design.volumes : x => values if lookup(values, "managed", true)
}
name = "${var.cluster_name}-${each.key}"
type = lookup(each.value, "type", "pd-standard")
zone = local.zone
size = each.value.size
}
data "google_compute_disk" "existing_volumes" {
for_each = {
for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true)
}
name = "${var.cluster_name}-${each.key}"
}

resource "google_compute_attached_disk" "attachments" {
for_each = module.design.volumes
disk = google_compute_disk.volumes[each.key].self_link
device_name = google_compute_disk.volumes[each.key].name
disk = try(google_compute_disk.volumes[each.key].self_link, google_compute_disk.existing_volumes[each.key].self_link)
device_name = try(google_compute_disk.volumes[each.key].name, google_compute_disk.existing_volumes[each.key].name)
mode = "READ_WRITE"
instance = google_compute_instance.instances[each.value.instance].self_link
}
Expand Down

0 comments on commit e74f90b

Please sign in to comment.