Skip to content

Commit e74f90b

Browse files
committed
implement (un)managed volumes for aws, gcp and azure
1 parent 237d6b4 commit e74f90b

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

aws/infrastructure.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ resource "aws_instance" "instances" {
130130
}
131131

132132
resource "aws_ebs_volume" "volumes" {
133-
for_each = module.design.volumes
133+
for_each = {
134+
for x, values in module.design.volumes : x => values if lookup(values, "managed", true)
135+
}
134136
availability_zone = local.availability_zone
135137
size = each.value.size
136138
type = lookup(each.value, "type", null)
@@ -140,6 +142,16 @@ resource "aws_ebs_volume" "volumes" {
140142
Name = "${var.cluster_name}-${each.key}"
141143
}
142144
}
145+
data "aws_ebs_volume" "existing_volumes" {
146+
for_each = {
147+
for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true)
148+
}
149+
150+
filter {
151+
name = "tag:Name"
152+
values = ["${var.cluster_name}-${each.key}"]
153+
}
154+
}
143155

144156
locals {
145157
device_names = [
@@ -151,7 +163,7 @@ locals {
151163
resource "aws_volume_attachment" "attachments" {
152164
for_each = module.design.volumes
153165
device_name = local.device_names[index(module.design.volume_per_instance[each.value.instance], replace(each.key, "${each.value.instance}-", ""))]
154-
volume_id = aws_ebs_volume.volumes[each.key].id
166+
volume_id = try(aws_ebs_volume.volumes[each.key].id, aws_ebs_volume.existing_volumes[each.key].id)
155167
instance_id = aws_instance.instances[each.value.instance].id
156168
skip_destroy = true
157169
}
@@ -175,7 +187,7 @@ locals {
175187
pv_key => {
176188
for name, specs in pv_values:
177189
name => merge(
178-
{ glob = "/dev/disk/by-id/*${replace(aws_ebs_volume.volumes["${x}-${pv_key}-${name}"].id, "-", "")}" },
190+
{ 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, "-", "")}") },
179191
specs,
180192
)
181193
} if contains(values.tags, pv_key)

azure/infrastructure.tf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,27 @@ resource "azurerm_linux_virtual_machine" "instances" {
128128
}
129129

130130
resource "azurerm_managed_disk" "volumes" {
131-
for_each = module.design.volumes
131+
for_each = {
132+
for x, values in module.design.volumes : x => values if lookup(values, "managed", true)
133+
}
132134
name = format("%s-%s", var.cluster_name, each.key)
133135
location = var.location
134136
resource_group_name = local.resource_group_name
135137
storage_account_type = lookup(each.value, "type", "Premium_LRS")
136138
create_option = "Empty"
137139
disk_size_gb = each.value.size
138140
}
141+
data "azurerm_managed_disk" "existing_volumes" {
142+
for_each = {
143+
for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true)
144+
}
145+
name = format("%s-%s", var.cluster_name, each.key)
146+
resource_group_name = local.resource_group_name
147+
}
139148

140149
resource "azurerm_virtual_machine_data_disk_attachment" "attachments" {
141150
for_each = module.design.volumes
142-
managed_disk_id = azurerm_managed_disk.volumes[each.key].id
151+
managed_disk_id = try(azurerm_managed_disk.volumes[each.key].id, azurerm_managed_disk.existing_volumes[each.key].id)
143152
virtual_machine_id = azurerm_linux_virtual_machine.instances[each.value.instance].id
144153
lun = index(module.design.volume_per_instance[each.value.instance], replace(each.key, "${each.value.instance}-", ""))
145154
caching = "ReadWrite"

gcp/infrastructure.tf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,25 @@ resource "google_compute_instance" "instances" {
143143
}
144144

145145
resource "google_compute_disk" "volumes" {
146-
for_each = module.design.volumes
146+
for_each = {
147+
for x, values in module.design.volumes : x => values if lookup(values, "managed", true)
148+
}
147149
name = "${var.cluster_name}-${each.key}"
148150
type = lookup(each.value, "type", "pd-standard")
149151
zone = local.zone
150152
size = each.value.size
151153
}
154+
data "google_compute_disk" "existing_volumes" {
155+
for_each = {
156+
for x, values in module.design.volumes : x => values if ! lookup(values, "managed", true)
157+
}
158+
name = "${var.cluster_name}-${each.key}"
159+
}
152160

153161
resource "google_compute_attached_disk" "attachments" {
154162
for_each = module.design.volumes
155-
disk = google_compute_disk.volumes[each.key].self_link
156-
device_name = google_compute_disk.volumes[each.key].name
163+
disk = try(google_compute_disk.volumes[each.key].self_link, google_compute_disk.existing_volumes[each.key].self_link)
164+
device_name = try(google_compute_disk.volumes[each.key].name, google_compute_disk.existing_volumes[each.key].name)
157165
mode = "READ_WRITE"
158166
instance = google_compute_instance.instances[each.value.instance].self_link
159167
}

0 commit comments

Comments
 (0)