Forked from cablespaghetti/gist:54de0ae93449e4698f0206a0e85514be
Created
May 26, 2020 18:24
-
-
Save rulai-jianfang/54d96790fd08e29fb8da16cd7f0cd398 to your computer and use it in GitHub Desktop.
Example terraform for ASG with spot instances
This file contains hidden or 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
# | |
# 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