Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jtbonhomme/86801e198cee5c7a661a9890d5bb3586 to your computer and use it in GitHub Desktop.
Save jtbonhomme/86801e198cee5c7a661a9890d5bb3586 to your computer and use it in GitHub Desktop.
Terraform for Autoscaling Group with spot instances
#
# Configuration for Autoscaling group.
#
resource "aws_launch_template" "eks-cluster-worker-nodes" {
iam_instance_profile = { name = "${aws_iam_instance_profile.eks-cluster-worker-nodes.name}" }
image_id = "${data.aws_ami.eks-worker.id}"
name = "${var.cluster-name}-eks-cluster-worker-nodes"
vpc_security_group_ids = ["${aws_security_group.eks-cluster-worker-nodes.id}"]
key_name = "${var.ssh-key-name}"
instance_type = "${local.host-types[0]}"
user_data = "${base64encode(element(data.template_file.userdata.*.rendered, count.index))}"
monitoring = { enabled = "${var.enable-spot == "true" ? false : true}" } # Enable if enable-spot isn't true
ebs_optimized = "${lookup(local.ebs_optimized, local.host-types[0], false)}"
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = 50
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "eks-cluster-worker-nodes-spot" {
count = "${var.enable-spot == "true" ? 1 : 0}"
max_size = "${var.max-host-count}"
min_size = "${var.min-host-count}"
name = "${var.cluster-name}"
vpc_zone_identifier = "${local.subnet-ids}"
mixed_instances_policy {
instances_distribution {
on_demand_percentage_above_base_capacity = 0
spot_instance_pools = 2
}
launch_template {
launch_template_specification {
launch_template_id = "${aws_launch_template.eks-cluster-worker-nodes.id}"
version = "$$Latest"
}
override {
instance_type = "${local.host-types[0]}"
}
override {
instance_type = "${local.host-types[1]}"
}
}
}
tag {
key = "Name"
value = "${var.cluster-name}"
propagate_at_launch = true
}
tag {
key = "Environment"
value = "${var.cluster-name}"
propagate_at_launch = true
}
tag {
key = "kubernetes.io/cluster/${var.cluster-name}"
value = "owned"
propagate_at_launch = true
}
tag {
key = "k8s.io/cluster-autoscaler/enabled"
value = "true"
propagate_at_launch = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment