Created
March 14, 2019 23:14
-
-
Save eskp/73822186b08fce8c6fe731c7315c1d46 to your computer and use it in GitHub Desktop.
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
resource "aws_ecs_cluster" "demo-ecs-cluster" { | |
name = "${var.ecs_cluster}" | |
} | |
# TODO convert to aws_launch_template https://www.terraform.io/docs/providers/aws/r/launch_template.html | |
resource "aws_launch_configuration" "ecs-launch-configuration" { | |
# Notice create_before_destroy lifecycle setting and name_prefix. As we can’t create a new resource with | |
# the same name as the old one, we don’t hard-code the name and only specify the prefix. Terraform adds | |
# a random postfix to it, so the new configuration doesn’t clash with the old one before it is destroyed. | |
name_prefix = "demo-cluster-lc" | |
# image_id = "${data.aws_ami.latest_ecs.id}" | |
image_id = "ami-4cc5072e" | |
instance_type = "${var.instance_type}" | |
iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile.id}" | |
root_block_device { | |
volume_type = "standard" | |
volume_size = 16 | |
delete_on_termination = true | |
} | |
lifecycle { | |
create_before_destroy = true | |
} | |
security_groups = ["${module.security_group.this_security_group_id}"] | |
associate_public_ip_address = "true" | |
key_name = "${var.key_name}" | |
user_data = <<EOF | |
#!/bin/bash | |
echo ECS_CLUSTER=${var.ecs_cluster} >> /etc/ecs/ecs.config | |
EOF | |
} | |
resource "aws_autoscaling_group" "ecs-autoscaling-group" { | |
# We reference the launch configuration name in the name of the Auto Scaling group. | |
# Updating the name of an ASG requires its replacement, and the new Auto Scaling | |
# group would spin up its instances using the new launch configuration. | |
name = "${aws_launch_configuration.ecs-launch-configuration.name}-asg" | |
max_size = "${var.max_instance_size}" | |
min_size = "${var.min_instance_size}" | |
desired_capacity = "${var.desired_capacity}" | |
vpc_zone_identifier = ["${aws_subnet.public-1.id}", "${aws_subnet.public-2.id}"] | |
launch_configuration = "${aws_launch_configuration.ecs-launch-configuration.name}" | |
health_check_type = "ELB" | |
# Required to redeploy without an outage. | |
lifecycle { | |
create_before_destroy = true | |
} | |
} | |
resource "aws_autoscaling_policy" "demo-cluster" { | |
name = "demo-ecs-auto-scaling" | |
policy_type = "TargetTrackingScaling" | |
estimated_instance_warmup = "90" | |
# adjustment_type = "ChangeInCapacity" | |
autoscaling_group_name = "${aws_autoscaling_group.ecs-autoscaling-group.name}" | |
target_tracking_configuration { | |
predefined_metric_specification { | |
predefined_metric_type = "ASGAverageCPUUtilization" | |
} | |
target_value = 40.0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment