Skip to content
32 changes: 32 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
}
9 changes: 1 addition & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
20 changes: 20 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down