Skip to content

Commit

Permalink
module: Add EBS extra volume support
Browse files Browse the repository at this point in the history
Allow extra EBS volumes to be created for each AWS instance.
  • Loading branch information
jieyu committed Jan 29, 2019
1 parent db55c08 commit 17492f4
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/aws/dcos
Submodule dcos updated from 0c0959 to f20c59
75 changes: 75 additions & 0 deletions modules/aws/ebs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* AWS EBS
* =======
* This module creates additional ebs volumes and attaches them to the instances.
*
* EXAMPLE
* -------
*
*```hcl
* module "instance-volume" {
* source = "dcos-terraform/ebs/aws"
*
* instances = ["id-ablsldhfa123", "id-aidrkdkek131"]
* num = "3"
* size = "500"
* type = "gp2"
* iops = "3000"
* }
*```
*/

provider "aws" {}

data "aws_instance" "instance" {
count = "${var.num_instances}"
instance_id = "${element(var.instances, count.index)}"
}

locals {
device_names = [
"/dev/xvdi",
"/dev/xvdj",
"/dev/xvdk",
"/dev/xvdm",
"/dev/xvdn",
"/dev/xvdo",
"/dev/xvdp",
"/dev/xvdq",
"/dev/xvdr",
"/dev/xvds",
]
}

resource "aws_ebs_volume" "volume" {
# We group volumes from one instance first. For instance:
# - var.num = 2
# - var.num_instances = 3
#
# We will get:
# - volume.0 (instance 0)
# - volume.1 (instance 0)
# - volume.2 (instance 1)
# - volume.3 (instance 1)
# - volume.4 (instance 2)
# - volume.5 (instance 2)
count = "${var.num * var.num_instances}"
availability_zone = "${element(data.aws_instance.instance.*.availability_zone, count.index / (var.num > 0 ? var.num : 1))}"
size = "${var.size}"
type = "${var.type}"
iops = "${var.iops}"

tags = "${merge(var.tags, map(
"Name", format(var.ebs_name_format,
var.cluster_name,
element(var.instances, count.index / (var.num > 0 ? var.num : 1))),
"Cluster", var.cluster_name))}"
}

resource "aws_volume_attachment" "volume-attachment" {
count = "${var.num * var.num_instances}"
device_name = "${element(local.device_names, count.index % (var.num > 0 ? var.num : 1))}"
volume_id = "${element(aws_ebs_volume.volume.*.id, count.index)}"
instance_id = "${element(var.instances, count.index / (var.num > 0 ? var.num : 1))}"
force_detach = true
}
4 changes: 4 additions & 0 deletions modules/aws/ebs/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "volumes" {
description = "List of volume IDs"
value = ["${aws_ebs_volume.volume.*.id}"]
}
42 changes: 42 additions & 0 deletions modules/aws/ebs/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variable "instances" {
description = "List of instance IDs"
type = "list"
}

variable "num_instances" {
description = "Number of instances"
}

variable "num" {
description = "The number of EBS volumes to create for each instance"
}

variable "size" {
description = "The size in GB for each volume"
default = "120"
}

variable "type" {
description = "The type for each volume"
default = ""
}

variable "iops" {
description = "The iops for each volume"
default = "0"
}

variable "cluster_name" {
description = "Name of the DC/OS cluster"
}

variable "tags" {
description = "Add custom tags to all resources"
type = "map"
default = {}
}

variable "ebs_name_format" {
description = "Printf style format for naming the EBS volume. Gets truncated to 32 characters. (input cluster_name and instance ID)"
default = "ebs-%s-%s"
}
2 changes: 1 addition & 1 deletion modules/aws/infrastructure
2 changes: 1 addition & 1 deletion modules/aws/instance
2 changes: 1 addition & 1 deletion modules/aws/private-agents

0 comments on commit 17492f4

Please # to comment.