Skip to content

Commit

Permalink
add 4 modules in here
Browse files Browse the repository at this point in the history
  • Loading branch information
TerribleDev committed May 1, 2017
1 parent dd3dab2 commit c26fcad
Show file tree
Hide file tree
Showing 36 changed files with 1,431 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This would be an example file that uses our modules. This creates an alb, ecs cl
# This makes a load balancer
module "alb" {
source = "git::https://github.com/Janus-vistaprint/tf_alb.git"
source = "git::https://github.com/Janus-vistaprint/terraform-autoscale-ecs.git//tf_alb"
# the load balancers name
lb_name = "${var.app_name}"
Expand All @@ -32,7 +32,7 @@ module "alb" {
# This makes an ecs cluster
module "ecs" {
source = "git::https://github.com/Janus-vistaprint/tf_ecs_cluster.git"
source = "git::https://github.com/Janus-vistaprint/terraform-autoscale-ecs.git//tf_ecs_cluster"
aws_region = "${var.aws_region}"
# how much disk should a server have in gb
Expand All @@ -55,7 +55,7 @@ module "ecs" {
# This registers a "service" (a set of containers) in the cluster made above with the image tag specified.
module "ecs_service" {
source = "git:https://github.com/Janus-vistaprint/tf_ecs_default_service.git"
source = "git::https://github.com/Janus-vistaprint/terraform-autoscale-ecs.git//tf_ecs_default_service"
vpc_id = "YOUR VPCID"
# the port in the container we should forward traffic to
Expand Down
1 change: 1 addition & 0 deletions tf_alb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tf_alb
5 changes: 5 additions & 0 deletions tf_alb/main.tf
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}"]
}
15 changes: 15 additions & 0 deletions tf_alb/outputs.tf
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}"
}
13 changes: 13 additions & 0 deletions tf_alb/route53.tf
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
}
}
23 changes: 23 additions & 0 deletions tf_alb/security.tf
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",
]
}
}
29 changes: 29 additions & 0 deletions tf_alb/variables.tf
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 = ""
}
1 change: 1 addition & 0 deletions tf_ecs_cluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tf_ecs_cluster
10 changes: 10 additions & 0 deletions tf_ecs_cluster/ami.tf
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
}
156 changes: 156 additions & 0 deletions tf_ecs_cluster/asg-scaling.tf
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",
]
}
58 changes: 58 additions & 0 deletions tf_ecs_cluster/asg.tf
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
}
}
Loading

0 comments on commit c26fcad

Please sign in to comment.