Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions _outputs.tf
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
output "alb_id" {
value = "${aws_lb.ecs.*.id}"
value = aws_lb.ecs.*.id
}

output "alb_arn" {
value = "${aws_lb.ecs.*.arn}"
value = aws_lb.ecs.*.arn
}

output "alb_dns_name" {
value = "${aws_lb.ecs.*.dns_name}"
value = aws_lb.ecs.*.dns_name
}

output "alb_zone_id" {
value = "${aws_lb.ecs.*.zone_id}"
value = aws_lb.ecs.*.zone_id
}

output "ecs_iam_role_arn" {
value = "${aws_iam_role.ecs.arn}"
value = aws_iam_role.ecs.arn
}

output "ecs_iam_role_name" {
value = "${aws_iam_role.ecs.name}"
value = aws_iam_role.ecs.name
}

output "ecs_service_iam_role_arn" {
value = "${aws_iam_role.ecs_service.arn}"
value = aws_iam_role.ecs_service.arn
}

output "ecs_service_iam_role_name" {
value = "${aws_iam_role.ecs_service.name}"
value = aws_iam_role.ecs_service.name
}

output "ecs_task_iam_role_arn" {
value = "${aws_iam_role.ecs_task.arn}"
value = aws_iam_role.ecs_task.arn
}

output "ecs_task_iam_role_name" {
value = "${aws_iam_role.ecs_task.name}"
value = aws_iam_role.ecs_task.name
}

output "ecs_id" {
value = "${aws_ecs_cluster.ecs.id}"
value = aws_ecs_cluster.ecs.id
}

output "ecs_arn" {
value = "${aws_ecs_cluster.ecs.arn}"
value = aws_ecs_cluster.ecs.arn
}

output "ecs_name" {
value = "${aws_ecs_cluster.ecs.name}"
value = aws_ecs_cluster.ecs.name
}

output "alb_listener_https_arn" {
value = "${aws_lb_listener.ecs_https.*.arn}"
value = aws_lb_listener.ecs_https.*.arn
}

output "ecs_nodes_secgrp_id" {
value = "${aws_security_group.ecs_nodes.id}"
value = aws_security_group.ecs_nodes.id
}

output "alb_secgrp_id" {
value = "${aws_security_group.alb.*.id}"
value = aws_security_group.alb.*.id
}

output "alb_cloudfront_key" {
value = "${random_string.alb_cloudfront_key.result}"
value = random_string.alb_cloudfront_key.result
}
10 changes: 5 additions & 5 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ variable "vpc_id" {
}

variable "private_subnet_ids" {
type = "list"
type = list(string)
description = "List of private subnet IDs for ECS instances"
}

variable "public_subnet_ids" {
type = "list"
type = list(string)
description = "List of public subnet IDs for ECS ALB"
}

variable "secure_subnet_ids" {
type = "list"
type = list(string)
description = "List of secure subnet IDs for EFS"
}

Expand All @@ -50,7 +50,7 @@ variable "certificate_arn" {}
# == OPTIONAL VARS

variable "security_group_ids" {
type = "list"
type = list(string)
default = []
description = "Extra security groups for instances"
}
Expand Down Expand Up @@ -97,7 +97,7 @@ variable "expire_backup_efs" {

variable "target_group_arns" {
default = []
type = "list"
type = list(string)
description = "List of target groups for ASG to register"
}

Expand Down
16 changes: 8 additions & 8 deletions alb.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_lb" "ecs" {
count = "${var.alb ? 1 : 0}"
count = var.alb ? 1 : 0

load_balancer_type = "application"
internal = false
Expand All @@ -18,13 +18,13 @@ resource "aws_lb" "ecs" {
}

resource "aws_lb_listener" "ecs_https" {
count = "${var.alb ? 1 : 0}"
count = var.alb ? 1 : 0

load_balancer_arn = aws_lb.ecs[0].arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = "${var.certificate_arn}"
certificate_arn = var.certificate_arn

default_action {
type = "forward"
Expand All @@ -33,7 +33,7 @@ resource "aws_lb_listener" "ecs_https" {
}

resource "aws_lb_listener" "ecs_http_redirect" {
count = "${var.alb ? 1 : 0}"
count = var.alb ? 1 : 0

load_balancer_arn = aws_lb.ecs[0].arn
port = "80"
Expand All @@ -51,19 +51,19 @@ resource "aws_lb_listener" "ecs_http_redirect" {
}

resource "aws_lb_target_group" "ecs_default_http" {
count = "${var.alb ? 1 : 0}"
count = var.alb ? 1 : 0

name = "ecs-${var.name}-default-http"
port = 80
protocol = "HTTP"
vpc_id = "${var.vpc_id}"
vpc_id = var.vpc_id
}

resource "aws_lb_target_group" "ecs_default_https" {
count = "${var.alb ? 1 : 0}"
count = var.alb ? 1 : 0

name = "ecs-${var.name}-default-https"
port = 80
protocol = "HTTP"
vpc_id = "${var.vpc_id}"
vpc_id = var.vpc_id
}
24 changes: 12 additions & 12 deletions asg.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ resource "aws_autoscaling_group" "ecs" {
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = "${aws_launch_template.ecs.id}"
launch_template_id = aws_launch_template.ecs.id
version = "$Latest"
}

override {
instance_type = "${var.instance_type_1}"
instance_type = var.instance_type_1
}

override {
instance_type = "${var.instance_type_2}"
instance_type = var.instance_type_2
}

override {
instance_type = "${var.instance_type_3}"
instance_type = var.instance_type_3
}
}

instances_distribution {
spot_instance_pools = 3
on_demand_base_capacity = "${var.on_demand_base_capacity}"
on_demand_percentage_above_base_capacity = "${var.on_demand_percentage}"
on_demand_base_capacity = var.on_demand_base_capacity
on_demand_percentage_above_base_capacity = var.on_demand_percentage
}
}

vpc_zone_identifier = var.private_subnet_ids

min_size = "${var.asg_min}"
max_size = "${var.asg_max}"
min_size = var.asg_min
max_size = var.asg_max

tags = [
"${map("key", "Name", "value", "ecs-node-${var.name}", "propagate_at_launch", true)}",
map("key", "Name", "value", "ecs-node-${var.name}", "propagate_at_launch", true)
]

target_group_arns = var.target_group_arns
Expand All @@ -48,14 +48,14 @@ resource "aws_autoscaling_group" "ecs" {
resource "aws_autoscaling_policy" "ecs_memory_tracking" {
name = "ecs-${var.name}-memory"
policy_type = "TargetTrackingScaling"
autoscaling_group_name = "${aws_autoscaling_group.ecs.name}"
autoscaling_group_name = aws_autoscaling_group.ecs.name
estimated_instance_warmup = "180"

target_tracking_configuration {
customized_metric_specification {
metric_dimension {
name = "ClusterName"
value = "${aws_ecs_cluster.ecs.name}"
value = aws_ecs_cluster.ecs.name
}

metric_name = "MemoryReservation"
Expand All @@ -64,6 +64,6 @@ resource "aws_autoscaling_policy" "ecs_memory_tracking" {
unit = "Percent"
}

target_value = "${var.asg_memory_target}"
target_value = var.asg_memory_target
}
}
36 changes: 18 additions & 18 deletions cf-exports.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ resource "aws_cloudformation_stack" "tf_exports" {
template_body = templatefile("${path.module}/cf-exports.yml", {
"name" = var.name,
"vars" = {
"AlbId" = "${aws_lb.ecs.*.id[0]}",
"AlbArn" = "${aws_lb.ecs.*.arn[0]}",
"AlbDnsName" = "${aws_lb.ecs.*.dns_name[0]}",
"AlbZoneId" = "${aws_lb.ecs.*.zone_id[0]}",
"AlbSecgrpId" = "${aws_security_group.alb.*.id[0]}"
"EcsIamRoleArn" = "${aws_iam_role.ecs.arn}",
"EcsIamRoleName" = "${aws_iam_role.ecs.name}",
"EcsServiceIamRoleArn" = "${aws_iam_role.ecs_service.arn}",
"EcsServiceIamRoleName" = "${aws_iam_role.ecs_service.name}",
"EcsTaskIamRoleArn" = "${aws_iam_role.ecs_task.arn}",
"EcsTaskIamRoleName" = "${aws_iam_role.ecs_task.name}",
"EcsId" = "${aws_ecs_cluster.ecs.*.id[0]}",
"EcsName" = "${aws_ecs_cluster.ecs.*.name[0]}",
"EcsArn" = "${aws_ecs_cluster.ecs.*.arn[0]}",
"AlbListenerHttpsArn" = "${aws_lb_listener.ecs_https.*.arn[0]}",
"EcsNodesSecGrpId" = "${aws_security_group.ecs_nodes.id}",
"VpcId" = "${var.vpc_id}"
"AlbId" = aws_lb.ecs.*.id[0],
"AlbArn" = aws_lb.ecs.*.arn[0],
"AlbDnsName" = aws_lb.ecs.*.dns_name[0],
"AlbZoneId" = aws_lb.ecs.*.zone_id[0],
"AlbSecgrpId" = aws_security_group.alb.*.id[0]
"EcsIamRoleArn" = aws_iam_role.ecs.arn,
"EcsIamRoleName" = aws_iam_role.ecs.name,
"EcsServiceIamRoleArn" = aws_iam_role.ecs_service.arn,
"EcsServiceIamRoleName" = aws_iam_role.ecs_service.name,
"EcsTaskIamRoleArn" = aws_iam_role.ecs_task.arn,
"EcsTaskIamRoleName" = aws_iam_role.ecs_task.name,
"EcsId" = aws_ecs_cluster.ecs.*.id[0],
"EcsName" = aws_ecs_cluster.ecs.*.name[0],
"EcsArn" = aws_ecs_cluster.ecs.*.arn[0],
"AlbListenerHttpsArn" = aws_lb_listener.ecs_https.*.arn[0],
"EcsNodesSecGrpId" = aws_security_group.ecs_nodes.id,
"VpcId" = var.vpc_id
}
})
}
}
8 changes: 4 additions & 4 deletions cloutwatch-alarms.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_cloudwatch_metric_alarm" "high_memory" {
count = "${length(var.alarm_sns_topics) > 0 ? 1 : 0}"
count = length(var.alarm_sns_topics) > 0 ? 1 : 0

alarm_name = "${data.aws_iam_account_alias.current.account_alias}-ecs-${var.name}-high-memory"
comparison_operator = "GreaterThanOrEqualToThreshold"
Expand All @@ -14,12 +14,12 @@ resource "aws_cloudwatch_metric_alarm" "high_memory" {
ok_actions = var.alarm_sns_topics

dimensions = {
ClusterName = "${aws_ecs_cluster.ecs.name}"
ClusterName = aws_ecs_cluster.ecs.name
}
}

resource "aws_cloudwatch_metric_alarm" "high_cpu" {
count = "${length(var.alarm_sns_topics) > 0 ? 1 : 0}"
count = length(var.alarm_sns_topics) > 0 ? 1 : 0

alarm_name = "${data.aws_iam_account_alias.current.account_alias}-ecs-${var.name}-high-cpu"
comparison_operator = "GreaterThanOrEqualToThreshold"
Expand All @@ -34,6 +34,6 @@ resource "aws_cloudwatch_metric_alarm" "high_cpu" {
ok_actions = var.alarm_sns_topics

dimensions = {
ClusterName = "${aws_ecs_cluster.ecs.name}"
ClusterName = aws_ecs_cluster.ecs.name
}
}
20 changes: 10 additions & 10 deletions ec2-launch-template.tf
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
data "template_file" "userdata" {
template = "${file("${path.module}/userdata.tpl")}"
template = file("${path.module}/userdata.tpl")

vars = {
tf_cluster_name = "${aws_ecs_cluster.ecs.name}"
tf_efs_id = "${aws_efs_file_system.ecs.id}"
userdata_extra = "${var.userdata}"
tf_cluster_name = aws_ecs_cluster.ecs.name
tf_efs_id = aws_efs_file_system.ecs.id
userdata_extra = var.userdata
}
}

resource "aws_launch_template" "ecs" {
name_prefix = "ecs-${var.name}-"
image_id = "${data.aws_ami.amzn.image_id}"
instance_type = "${var.instance_type_1}"
image_id = data.aws_ami.amzn.image_id
instance_type = var.instance_type_1

iam_instance_profile {
name = "${aws_iam_instance_profile.ecs.name}"
name = aws_iam_instance_profile.ecs.name
}

block_device_mappings {
device_name = "/dev/xvda"

ebs {
volume_size = "${var.instance_volume_size_root}"
volume_size = var.instance_volume_size_root
}
}

block_device_mappings {
device_name = "/dev/xvdcz"

ebs {
volume_size = "${var.instance_volume_size}"
volume_size = var.instance_volume_size
}
}

vpc_security_group_ids = concat(list(aws_security_group.ecs_nodes.id), var.security_group_ids)

user_data = "${base64encode(data.template_file.userdata.rendered)}"
user_data = base64encode(data.template_file.userdata.rendered)

lifecycle {
create_before_destroy = true
Expand Down
2 changes: 1 addition & 1 deletion ecs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
resource "aws_ecs_cluster" "ecs" {
name = "${var.name}"
name = var.name
}
Loading