diff --git a/locals.tf b/locals.tf index 9c1dff6..41061a0 100644 --- a/locals.tf +++ b/locals.tf @@ -4,4 +4,36 @@ locals { security_group_name = "sgContainerInstance" ecs_for_ec2_service_role_name = "${var.environment}ContainerInstanceProfile" ecs_service_role_name = "ecs${title(var.environment)}ServiceRole" + + root_device = { + device_name = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)}" + + ebs = [{ + volume_type = "${var.root_block_device_type}" + volume_size = "${var.root_block_device_size}" + }] + } + + data_device = { + device_name = "${var.data_block_device_name}" + + ebs = [{ + volume_type = "${var.data_block_device_type}" + volume_size = "${var.data_block_device_size}" + delete_on_termination = "${var.data_block_device_delete_on_termination}" + }] + } + + volume_devices_without_data = [ + "${local.root_device}", + ] + + volume_devices_with_data = [ + "${local.root_device}", + "${local.data_device}", + ] + + // https://github.com/hashicorp/terraform/issues/12453#issuecomment-311611817 + volume_end_index = "${var.enable_data_block_device ? 2 : 1}" + volume_devices = "${slice(local.volume_devices_with_data, 0, local.volume_end_index)}" } diff --git a/main.tf b/main.tf index 5ed16c4..8068cc6 100644 --- a/main.tf +++ b/main.tf @@ -140,14 +140,7 @@ data "aws_ami" "user_ami" { } resource "aws_launch_template" "container_instance" { - block_device_mappings { - device_name = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)}" - - ebs { - volume_type = "${var.root_block_device_type}" - volume_size = "${var.root_block_device_size}" - } - } + block_device_mappings = ["${local.volume_devices}"] credit_specification { cpu_credits = "${var.cpu_credit_specification}" diff --git a/variables.tf b/variables.tf index aa8262d..60acc1b 100644 --- a/variables.tf +++ b/variables.tf @@ -48,6 +48,26 @@ variable "root_block_device_size" { default = "8" } +variable "enable_data_block_device" { + default = 0 +} + +variable "data_block_device_name" { + default = "/dev/xvdb" +} + +variable "data_block_device_type" { + default = "gp2" +} + +variable "data_block_device_size" { + default = "50" +} + +variable "data_block_device_delete_on_termination" { + default = 1 +} + variable "instance_type" { default = "t2.micro" }