Last active
August 22, 2017 13:31
-
-
Save s-maj/0bf81fef71274d31605d4caf43631a2c to your computer and use it in GitHub Desktop.
1430.tf
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
#Prep | |
provider "aws" { | |
region = "eu-central-1" | |
} | |
resource "random_id" "alarm" { | |
byte_length = 8 | |
} | |
#IAM | |
resource "aws_iam_role" "ecs_autoscale" { | |
name = "${format("%s-%s", "role", random_id.alarm.hex)}" | |
assume_role_policy = <<EOF | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": "application-autoscaling.amazonaws.com" | |
}, | |
"Action": "sts:AssumeRole" | |
} | |
] | |
} | |
EOF | |
} | |
resource "aws_iam_role_policy_attachment" "ecs_autoscale_policy_attach" { | |
role = "${aws_iam_role.ecs_autoscale.id}" | |
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceAutoscaleRole" | |
} | |
#ECS | |
resource "aws_ecs_cluster" "ecs" { | |
name = "${format("%s-%s", "ecs", random_id.alarm.hex)}" | |
} | |
resource "aws_ecs_task_definition" "task" { | |
family = "${format("%s-%s", "task", random_id.alarm.hex)}" | |
container_definitions = <<EOF | |
[{ | |
"name": "task", | |
"image": "hello-world", | |
"cpu": 10, | |
"memory": 512, | |
"essential": true, | |
"portMappings": [{ | |
"containerPort": 80, | |
"hostPort": 80 | |
}] | |
}] | |
EOF | |
} | |
resource "aws_ecs_service" "service1" { | |
name = "${format("%s-%s", "service1", random_id.alarm.hex)}" | |
cluster = "${aws_ecs_cluster.ecs.id}" | |
task_definition = "${aws_ecs_task_definition.task.arn}" | |
} | |
resource "aws_ecs_service" "service2" { | |
name = "${format("%s-%s", "service2", random_id.alarm.hex)}" | |
cluster = "${aws_ecs_cluster.ecs.id}" | |
task_definition = "${aws_ecs_task_definition.task.arn}" | |
} | |
resource "aws_ecs_service" "service3" { | |
name = "${format("%s-%s", "service3", random_id.alarm.hex)}" | |
cluster = "${aws_ecs_cluster.ecs.id}" | |
task_definition = "${aws_ecs_task_definition.task.arn}" | |
} | |
# AppScaling1 | |
resource "aws_appautoscaling_target" "ecs_target1" { | |
role_arn = "${aws_iam_role.ecs_autoscale.arn}" | |
scalable_dimension = "ecs:service:DesiredCount" | |
max_capacity = "2" | |
min_capacity = "0" | |
resource_id = "service/${aws_ecs_cluster.ecs.name}/${aws_ecs_service.service1.name}" | |
service_namespace = "ecs" | |
depends_on = ["aws_ecs_service.service1"] | |
} | |
resource "aws_appautoscaling_policy" "ecs_policy1" { | |
name = "scale-down-${count.index}" | |
adjustment_type = "ChangeInCapacity" | |
cooldown = 60 | |
metric_aggregation_type = "Maximum" | |
resource_id = "service/${aws_ecs_cluster.ecs.name}/${aws_ecs_service.service1.name}" | |
scalable_dimension = "ecs:service:DesiredCount" | |
service_namespace = "ecs" | |
count = 50 | |
step_adjustment { | |
metric_interval_upper_bound = 0 | |
scaling_adjustment = -1 | |
} | |
depends_on = ["aws_appautoscaling_target.ecs_target1", "aws_ecs_service.service1"] | |
} | |
#Alarm | |
resource "aws_cloudwatch_metric_alarm" "scale_out1" { | |
alarm_name = "${format("alarm-%s", count.index)}" | |
alarm_actions = ["${element(aws_appautoscaling_policy.ecs_policy1.*.arn, count.index)}"] | |
metric_name = "CPUUtilization" | |
namespace = "AWS/ECS" | |
evaluation_periods = "1" | |
period = "300" | |
threshold = "20" | |
statistic = "Maximum" | |
comparison_operator = "GreaterThanOrEqualToThreshold" | |
actions_enabled = true | |
count = 50 | |
dimensions = { | |
"ClusterName" = "${aws_ecs_cluster.ecs.name}" | |
"ServiceName" = "${aws_ecs_service.service1.name}" | |
} | |
depends_on = ["aws_ecs_service.service1"] | |
} | |
# AppScaling2 | |
resource "aws_appautoscaling_target" "ecs_target2" { | |
role_arn = "${aws_iam_role.ecs_autoscale.arn}" | |
scalable_dimension = "ecs:service:DesiredCount" | |
max_capacity = "2" | |
min_capacity = "1" | |
resource_id = "service/${aws_ecs_cluster.ecs.name}/${aws_ecs_service.service2.name}" | |
service_namespace = "ecs" | |
depends_on = ["aws_ecs_service.service2"] | |
} | |
resource "aws_appautoscaling_policy" "ecs_policy2" { | |
name = "scale-down-${count.index}" | |
adjustment_type = "ChangeInCapacity" | |
cooldown = 60 | |
metric_aggregation_type = "Maximum" | |
resource_id = "service/${aws_ecs_cluster.ecs.name}/${aws_ecs_service.service2.name}" | |
scalable_dimension = "ecs:service:DesiredCount" | |
service_namespace = "ecs" | |
count = 50 | |
step_adjustment { | |
metric_interval_upper_bound = 0 | |
scaling_adjustment = -1 | |
} | |
depends_on = ["aws_appautoscaling_target.ecs_target2", "aws_ecs_service.service2"] | |
} | |
#Alarm | |
resource "aws_cloudwatch_metric_alarm" "scale_out2" { | |
alarm_name = "${format("alarm-%s", count.index)}" | |
alarm_actions = ["${element(aws_appautoscaling_policy.ecs_policy2.*.arn, count.index)}"] | |
metric_name = "CPUUtilization" | |
namespace = "AWS/ECS" | |
evaluation_periods = "1" | |
period = "300" | |
threshold = "20" | |
statistic = "Maximum" | |
comparison_operator = "GreaterThanOrEqualToThreshold" | |
actions_enabled = true | |
count = 50 | |
dimensions = { | |
"ClusterName" = "${aws_ecs_cluster.ecs.name}" | |
"ServiceName" = "${aws_ecs_service.service2.name}" | |
} | |
depends_on = ["aws_ecs_service.service2"] | |
} | |
# Appscaling 3 | |
resource "aws_appautoscaling_target" "ecs_target3" { | |
role_arn = "${aws_iam_role.ecs_autoscale.arn}" | |
scalable_dimension = "ecs:service:DesiredCount" | |
max_capacity = "2" | |
min_capacity = "1" | |
resource_id = "service/${aws_ecs_cluster.ecs.name}/${aws_ecs_service.service3.name}" | |
service_namespace = "ecs" | |
depends_on = ["aws_ecs_service.service3"] | |
} | |
resource "aws_appautoscaling_policy" "ecs_policy3" { | |
name = "scale-down-${count.index}" | |
adjustment_type = "ChangeInCapacity" | |
cooldown = 60 | |
metric_aggregation_type = "Maximum" | |
resource_id = "service/${aws_ecs_cluster.ecs.name}/${aws_ecs_service.service3.name}" | |
scalable_dimension = "ecs:service:DesiredCount" | |
service_namespace = "ecs" | |
count = 50 | |
step_adjustment { | |
metric_interval_upper_bound = 0 | |
scaling_adjustment = -1 | |
} | |
depends_on = ["aws_appautoscaling_target.ecs_target3", "aws_ecs_service.service3"] | |
} | |
#Alarm | |
resource "aws_cloudwatch_metric_alarm" "scale_out3" { | |
alarm_name = "${format("alarm-%s", count.index)}" | |
alarm_actions = ["${element(aws_appautoscaling_policy.ecs_policy3.*.arn, count.index)}"] | |
metric_name = "CPUUtilization" | |
namespace = "AWS/ECS" | |
evaluation_periods = "1" | |
period = "300" | |
threshold = "20" | |
statistic = "Maximum" | |
comparison_operator = "GreaterThanOrEqualToThreshold" | |
actions_enabled = true | |
count = 50 | |
dimensions = { | |
"ClusterName" = "${aws_ecs_cluster.ecs.name}" | |
"ServiceName" = "${aws_ecs_service.service3.name}" | |
} | |
depends_on = ["aws_ecs_service.service3"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment