-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd3dab2
commit c26fcad
Showing
36 changed files
with
1,431 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# tf_alb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "aws_alb" "main" { | ||
name = "alb-${var.lb_name}" | ||
subnets = ["${var.public_subnets}"] | ||
security_groups = ["${aws_security_group.lb_sg.id}"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
output "lb_security_group" { | ||
value = "${aws_security_group.lb_sg.id}" | ||
} | ||
|
||
output "lb_id" { | ||
value = "${aws_alb.main.id}" | ||
} | ||
|
||
output "lb_dns_name" { | ||
value = "${aws_alb.main.dns_name}" | ||
} | ||
|
||
output "lb_arn" { | ||
value = "${aws_alb.main.arn}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# create DNS record for our LB in Route53 | ||
resource "aws_route53_record" "www" { | ||
count = "${var.route53_dns_name == "" ? 0 : 1}" | ||
zone_id = "${var.route53_dns_zone_id}" | ||
name = "${var.route53_dns_name}" | ||
type = "A" | ||
|
||
alias { | ||
name = "${aws_alb.main.dns_name}" | ||
zone_id = "${aws_alb.main.zone_id}" | ||
evaluate_target_health = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
resource "aws_security_group" "lb_sg" { | ||
description = "controls access to the application ELB" | ||
|
||
vpc_id = "${var.vpc_id}" | ||
name = "tf-ecs-lbsg-${var.lb_name}" | ||
|
||
ingress { | ||
protocol = "tcp" | ||
from_port = "${element(var.lb_port, count.index)}" | ||
to_port = "${element(var.lb_port, count.index)}" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
|
||
cidr_blocks = [ | ||
"0.0.0.0/0", | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
variable "public_subnets" { | ||
description = "" | ||
type = "list" | ||
} | ||
|
||
variable "lb_name" { | ||
description = "lb name" | ||
type = "string" | ||
} | ||
|
||
variable "lb_port" { | ||
default = [80] | ||
} | ||
|
||
variable "vpc_id" { | ||
type = "string" | ||
} | ||
|
||
variable "route53_dns_name" { | ||
description = "Public DNS name used to refer to this ALB" | ||
type = "string" | ||
default = "" | ||
} | ||
|
||
variable "route53_dns_zone_id" { | ||
description = "Zone ID for Route 53" | ||
type = "string" | ||
default = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# tf_ecs_cluster |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
data "aws_ami" "stable_ecs" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "name" | ||
values = ["*ecs-optimized*"] | ||
} | ||
|
||
owners = ["amazon"] # CoreOS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
resource "aws_autoscaling_policy" "cpu-scale-up" { | ||
name = "asg-${var.cluster_name}-cpu-scale-up" | ||
scaling_adjustment = 1 | ||
adjustment_type = "ChangeInCapacity" | ||
cooldown = 300 | ||
autoscaling_group_name = "${aws_autoscaling_group.app.name}" | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_cloudwatch_metric_alarm" "cpu-high" { | ||
alarm_name = "cpu-util-high-asg-${var.cluster_name}" | ||
comparison_operator = "GreaterThanOrEqualToThreshold" | ||
evaluation_periods = "2" | ||
metric_name = "CPUUtilization" | ||
namespace = "AWS/EC2" | ||
period = "300" | ||
statistic = "Average" | ||
threshold = "60" | ||
alarm_description = "This metric monitors ec2 cpu for high utilization on ECS hosts" | ||
|
||
alarm_actions = [ | ||
"${aws_autoscaling_policy.cpu-scale-up.arn}", | ||
] | ||
|
||
dimensions { | ||
AutoScalingGroupName = "${aws_autoscaling_group.app.name}" | ||
} | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_autoscaling_policy" "cpu-scale-down" { | ||
name = "asg-${var.cluster_name}-cpu-scale-down" | ||
scaling_adjustment = -1 | ||
adjustment_type = "ChangeInCapacity" | ||
cooldown = 300 | ||
autoscaling_group_name = "${aws_autoscaling_group.app.name}" | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_cloudwatch_metric_alarm" "cpu-low" { | ||
alarm_name = "cpu-util-low-asg-${var.cluster_name}" | ||
comparison_operator = "LessThanOrEqualToThreshold" | ||
evaluation_periods = "3" | ||
metric_name = "CPUUtilization" | ||
namespace = "AWS/EC2" | ||
period = "300" | ||
statistic = "Average" | ||
|
||
# keeping this very low, as we should let ecs reservations to mostly control this | ||
threshold = "5" | ||
alarm_description = "This metric monitors ec2 cpu for low utilization on ECS hosts" | ||
|
||
alarm_actions = [ | ||
"${aws_autoscaling_policy.cpu-scale-down.arn}", | ||
] | ||
|
||
dimensions { | ||
AutoScalingGroupName = "${aws_autoscaling_group.app.name}" | ||
} | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_autoscaling_policy" "mem-scale-up" { | ||
name = "ECS-${var.cluster_name}-mem-scale-up" | ||
|
||
scaling_adjustment = 1 | ||
|
||
adjustment_type = "ChangeInCapacity" | ||
|
||
cooldown = 300 | ||
|
||
autoscaling_group_name = "${aws_autoscaling_group.app.name}" | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_autoscaling_policy" "mem-scale-down" { | ||
name = "ECS-${var.cluster_name}-mem-scale-down" | ||
|
||
scaling_adjustment = -1 | ||
|
||
adjustment_type = "ChangeInCapacity" | ||
|
||
cooldown = 300 | ||
|
||
autoscaling_group_name = "${aws_autoscaling_group.app.name}" | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_cloudwatch_metric_alarm" "memory-high" { | ||
alarm_name = "mem-util-high-asg-${var.cluster_name}" | ||
|
||
comparison_operator = "GreaterThanOrEqualToThreshold" | ||
evaluation_periods = "2" | ||
metric_name = "MemoryUtilization" | ||
namespace = "System/Linux" | ||
period = "300" | ||
statistic = "Average" | ||
threshold = "80" | ||
alarm_description = "This metric monitors ec2 memory for high utilization on ECS hosts" | ||
|
||
alarm_actions = [ | ||
"${aws_autoscaling_policy.mem-scale-up.arn}", | ||
] | ||
|
||
dimensions { | ||
AutoScalingGroupName = "${aws_autoscaling_group.app.name}" | ||
} | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} | ||
|
||
resource "aws_cloudwatch_metric_alarm" "memory-low" { | ||
alarm_name = "mem-util-low-asg-${var.cluster_name}" | ||
comparison_operator = "LessThanOrEqualToThreshold" | ||
evaluation_periods = "2" | ||
metric_name = "MemoryUtilization" | ||
namespace = "System/Linux" | ||
period = "300" | ||
statistic = "Average" | ||
|
||
# keeping this very low, as we should let ecs reservations to mostly control this | ||
threshold = "5" | ||
alarm_description = "This metric monitors ec2 memory for low utilization on ECS hosts" | ||
|
||
alarm_actions = [ | ||
"${aws_autoscaling_policy.mem-scale-down.arn}", | ||
] | ||
|
||
dimensions { | ||
AutoScalingGroupName = "${aws_autoscaling_group.app.name}" | ||
} | ||
|
||
depends_on = [ | ||
"aws_autoscaling_group.app", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
resource "aws_launch_configuration" "app" { | ||
security_groups = [ | ||
"${aws_security_group.instance_sg.id}", | ||
] | ||
|
||
image_id = "${data.aws_ami.stable_ecs.id}" | ||
instance_type = "${var.instance_type}" | ||
iam_instance_profile = "${aws_iam_instance_profile.ecs.name}" | ||
associate_public_ip_address = false | ||
key_name = "${var.key_name}" | ||
|
||
# ec2 optimized instances | ||
|
||
user_data = <<EOF | ||
#!/bin/bash | ||
echo ECS_CLUSTER=${aws_ecs_cluster.main.name} > /etc/ecs/ecs.config | ||
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https zip unzip wget perl-Digest-SHA.x86_64 | ||
cd /home/ec2-user | ||
wget http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip | ||
unzip CloudWatchMonitoringScripts-v1.1.0.zip | ||
rm CloudWatchMonitoringScripts-v1.1.0.zip | ||
chown ec2-user:ec2-user aws-scripts-mon | ||
(crontab -u ec2-user -l 2>/dev/null; echo "*/1 * * * * /home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl --auto-scaling --mem-util --disk-space-util --disk-path=/ --from-cron") | crontab - | ||
EOF | ||
# user_data = "${data.template_file.cloud_config.rendered}" | ||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
root_block_device { | ||
volume_size = "${var.asg_disk_size}" | ||
} | ||
} | ||
|
||
### Compute | ||
|
||
resource "aws_autoscaling_group" "app" { | ||
name = "tf-${var.cluster_name}" | ||
vpc_zone_identifier = ["${var.private_subnets}"] | ||
min_size = "${var.asg_min}" | ||
max_size = "${var.asg_max}" | ||
desired_capacity = "${var.asg_desired}" | ||
launch_configuration = "${aws_launch_configuration.app.name}" | ||
termination_policies = ["OldestLaunchConfiguration", "OldestInstance"] | ||
depends_on = ["aws_launch_configuration.app"] | ||
|
||
/* | ||
in 0.9.3 deletes are not handled properly when lc, and asg's have create before destroy | ||
https://github.com/hashicorp/terraform/issues/13517 | ||
lifecycle { | ||
create_before_destroy = true | ||
}*/ | ||
|
||
tag { | ||
key = "Name" | ||
value = "tf-${var.cluster_name}" | ||
propagate_at_launch = true | ||
} | ||
} |
Oops, something went wrong.