Commit 28bcf3b5 authored by A-Gordon's avatar A-Gordon

Updated main.tf

parent cfa1d35f
############################################################################################################# #############################################################################################################
# Variables # Variables
############################################################################################################# #############################################################################################################
variable "name" {}
variable "vpc_id" {
description = "ID of the VPC in which the ELB should reside"
}
# Launch Configuration Variables # Launch Configuration Variables
variable "lc_name" {} variable "ami" {
variable "ami_id" {
description = "The AMI to use with the launch configuration" description = "The AMI to use with the launch configuration"
} }
variable "instance_type" { variable "instance_type" {
...@@ -12,29 +17,26 @@ variable "instance_type" { ...@@ -12,29 +17,26 @@ variable "instance_type" {
variable "iam_instance_profile" { variable "iam_instance_profile" {
description = "The IAM role the launched instance will use" description = "The IAM role the launched instance will use"
} }
variable "key_name" {
description = "The SSH public key name (in EC2 key-pairs) to be injected into instances"
default = "Bastion"
}
variable "security_groups" { variable "security_groups" {
description = "ID of SG the launched instance will use" description = "ID of SG the launched instance will use"
type = "list" type = "list"
} }
variable "user_data" { variable "user_data" {
description = "The path to a file with user_data for the instances" description = "The path to a file with user_data for the instances"
} }
# Auto-Scaling Group # Auto-Scaling Group
variable "asg_name" {} variable "minimum_number_of_instances" {
variable "asg_minimum_number_of_instances" {
description = "The minimum number of instances we want in the ASG" description = "The minimum number of instances we want in the ASG"
default = "2" default = "2"
} }
variable "asg_maximum_number_of_instances" { variable "maximum_number_of_instances" {
description = "The maximum number of instances we want in the ASG" description = "The maximum number of instances we want in the ASG"
default = "2" default = "2"
} }
variable "asg_desired_number_of_instances" { variable "desired_number_of_instances" {
description = "The desired number of instances we want in the ASG" description = "The desired number of instances we want in the ASG"
default = "2" default = "2"
} }
...@@ -53,10 +55,14 @@ variable "load_balancers" { ...@@ -53,10 +55,14 @@ variable "load_balancers" {
variable "vpc_zone_identifier" { variable "vpc_zone_identifier" {
type = "list" type = "list"
} }
variable "termination_policies" {
type = "list"
default = ["ClosestToNextInstanceHour", "OldestInstance"]
}
# Auto-Scaling policies # Auto-Scaling policies
variable "scale_up_scaling_adjsutment" { variable "scale_up_scaling_adjsutment" {
default = "100" default = "50"
} }
variable "scale_up_cooldown" { variable "scale_up_cooldown" {
default = "60" default = "60"
...@@ -69,72 +75,195 @@ variable "scale_down_cooldown" { ...@@ -69,72 +75,195 @@ variable "scale_down_cooldown" {
} }
resource "aws_launch_configuration" "default" { # Cloudwatch vars
variable "cpu_high_threshold" {
description = "CPU percentage that must be exceeded to trigger the alarm."
default = "80"
}
variable "cpu_high_period" {
description = "The period in seconds over which the specified statistic is applied."
default = "60"
}
variable "cpu_high_evaluation_periods" {
description = "The number of periods over which data is compared to the specified threshold."
default = "2"
}
variable "cpu_low_threshold" {
description = "CPU percentage that must be exceeded to trigger the alarm."
default = "50"
}
variable "cpu_low_period" {
description = "The period in seconds over which the specified statistic is applied."
default = "300"
}
variable "cpu_low_evaluation_periods" {
description = "The number of periods over which data is compared to the specified threshold."
default = "3"
}
#############################################################################################################
# Security Group
#############################################################################################################
resource "aws_security_group" "main" {
vpc_id = "${var.vpc_id}"
description = "Application EC2 SG"
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = ["${var.security_groups}"]
}
ingress {
from_port = 80 # Change to service port
to_port = 80
protocol = "tcp"
security_groups = ["${var.security_groups}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#############################################################################################################
# Launch Configuration
#############################################################################################################
resource "aws_launch_configuration" "main" {
lifecycle { create_before_destroy = true } lifecycle { create_before_destroy = true }
name_prefix = "${var.lc_name}" name_prefix = "${var.name}-lc-"
image_id = "${var.ami_id}" image_id = "${var.ami}"
instance_type = "${var.instance_type}" instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
iam_instance_profile = "${var.iam_instance_profile}" iam_instance_profile = "${var.iam_instance_profile}"
security_groups = ["${var.security_groups}"] security_groups = ["${var.security_groups}"]
user_data = "${var.user_data}" user_data = "${var.user_data}"
} }
resource "aws_autoscaling_group" "default" { #############################################################################################################
# Autoscaling Groups
#############################################################################################################
resource "aws_autoscaling_group" "main" {
lifecycle { create_before_destroy = true } lifecycle { create_before_destroy = true }
name = "${var.asg_name}" name = "${var.name}"
launch_configuration = "${aws_launch_configuration.default.name}" launch_configuration = "${aws_launch_configuration.main.name}"
load_balancers = ["${var.load_balancers}"] load_balancers = ["${var.load_balancers}"]
vpc_zone_identifier = ["${var.vpc_zone_identifier}"] vpc_zone_identifier = ["${var.vpc_zone_identifier}"]
min_elb_capacity = "${var.asg_minimum_number_of_instances}" min_elb_capacity = "${var.minimum_number_of_instances}"
min_size = "${var.asg_minimum_number_of_instances}" min_size = "${var.minimum_number_of_instances}"
max_size = "${var.asg_maximum_number_of_instances}" max_size = "${var.maximum_number_of_instances}"
desired_capacity = "${var.asg_desired_number_of_instances}" desired_capacity = "${var.desired_number_of_instances}"
health_check_grace_period = "${var.health_check_grace_period}" health_check_grace_period = "${var.health_check_grace_period}"
health_check_type = "${var.health_check_type}" health_check_type = "${var.health_check_type}"
termination_policies = "${var.termination_policies}"
tag { tag {
key = "Name" key = "Name"
value = "${var.asg_name}" value = "${var.name}"
propagate_at_launch = true propagate_at_launch = true
} }
tag { tag {
key = "Launch config" key = "Launch config"
value = "${aws_launch_configuration.default.name}" value = "${aws_launch_configuration.main.name}"
propagate_at_launch = true propagate_at_launch = true
} }
} }
resource "aws_autoscaling_policy" "scale-up" { #############################################################################################################
name = "${var.asg_name}-scale-up" # Autoscaling Policies
#############################################################################################################
resource "aws_autoscaling_policy" "scale_up" {
name = "${var.name}-scale-up"
adjustment_type = "PercentChangeInCapacity" adjustment_type = "PercentChangeInCapacity"
scaling_adjustment = "${var.scale_up_scaling_adjsutment}" scaling_adjustment = "${var.scale_up_scaling_adjsutment}"
cooldown = "${var.scale_up_cooldown}" cooldown = "${var.scale_up_cooldown}"
autoscaling_group_name = "${aws_autoscaling_group.default.name}" autoscaling_group_name = "${aws_autoscaling_group.main.name}"
} }
resource "aws_autoscaling_policy" "scale-down" { resource "aws_autoscaling_policy" "scale_down" {
name = "${var.asg_name}-scale-down" name = "${var.name}-scale-down"
adjustment_type = "PercentChangeInCapacity" adjustment_type = "PercentChangeInCapacity"
scaling_adjustment = "${var.scale_down_scaling_adjsutment}" scaling_adjustment = "${var.scale_down_scaling_adjsutment}"
cooldown = "${var.scale_down_cooldown}" cooldown = "${var.scale_down_cooldown}"
autoscaling_group_name = "${aws_autoscaling_group.default.name}" autoscaling_group_name = "${aws_autoscaling_group.main.name}"
} }
#############################################################################################################
# Cloudwatch Alarms
#############################################################################################################
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "${var.name}-app-cpu-high"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
statistic = "Average"
comparison_operator = "GreaterThanOrEqualToThreshold"
threshold = "${var.cpu_high_threshold}"
period = "${var.cpu_high_period}"
evaluation_periods = "${var.cpu_high_evaluation_periods}"
alarm_description = "This will alarm when an app servers CPU usage is above ${var.cpu_high_threshold}% for ${var.cpu_high_evaluation_periods} * ${var.cpu_high_period}"
alarm_actions = [
"${aws_autoscaling_policy.scale_up.arn}"
]
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.main.id}"
}
}
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
alarm_name = "${var.name}-app-cpu-low"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
statistic = "Average"
comparison_operator = "LessThanOrEqualToThreshold"
threshold = "${var.cpu_low_threshold}"
period = "${var.cpu_low_period}"
evaluation_periods = "${var.cpu_low_evaluation_periods}"
alarm_description = "This will alarm when an app servers CPU usage is below ${var.cpu_low_threshold}% for ${var.cpu_low_evaluation_periods} * ${var.cpu_low_period}"
alarm_actions = [
"${aws_autoscaling_policy.scale_up.arn}"
]
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.main.id}"
}
}
############################################################################################################# #############################################################################################################
# Outputs # Outputs
############################################################################################################# #############################################################################################################
output "launch_config_id" { output "launch_config_id" {
value = "${aws_launch_configuration.default.id}" value = "${aws_launch_configuration.main.id}"
} }
output "asg_id" { output "asg_id" {
value = "${aws_autoscaling_group.default.id}" value = "${aws_autoscaling_group.main.id}"
} }
output "asg_name" { output "asg_name" {
value = "${aws_autoscaling_group.default.name}" value = "${aws_autoscaling_group.main.name}"
} }
output "autoscaling_policy_scale_up_arn" { output "autoscaling_policy_scale_up_arn" {
value = "${aws_autoscaling_policy.scale-up.arn}" value = "${aws_autoscaling_policy.scale_up.arn}"
} }
output "autoscaling_policy_scale_down_arn" { output "autoscaling_policy_scale_down_arn" {
value = "${aws_autoscaling_policy.scale-down.arn}" value = "${aws_autoscaling_policy.scale_down.arn}"
}
output "security_group" {
value = "${aws_security_group.main.id}"
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment