Using Terraform to create Autoscaling Group in AWS.
Using mixed_instances_policy
in aws_autoscaling_group
resource of Terraform.
resource "aws_launch_template" "go_app" {
image_id = "${data.aws_ami.go_app.id}"
instance_type = "${var.launch_config["instance_type"]}"
vpc_security_group_ids = ["${aws_security_group.go_app.id}"]
key_name = "${var.key_name}"
}
The instance_type
in launch template is t2.small
.
resource "aws_autoscaling_group" "go_app" {
name = "${aws_launch_template.go_app.name}-asg"
vpc_zone_identifier = ["${aws_subnet.public.*.id}"]
min_size = 1
desired_capacity = 2
max_size = 4
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = "${aws_launch_template.go_app.id}"
version = "$$Latest"
}
override {
instance_type = "t2.micro"
}
override {
instance_type = "t2.nano"
}
}
instances_distribution {
on_demand_base_capacity = 1
on_demand_percentage_above_base_capacity = 0
spot_allocation_strategy = "lowest-price"
spot_instance_pools = 2
}
}
}
This should create 1 on-demand instance and 1 spot instance (as desired capacity is 2). But it creates 1 on-demand instance and fails to create the spot instance.
Activity History of the Autoscaling group mentions that the AutoScaling group tries to create the instance but fails due to the following error:
Launching a new EC2 instance. Status Reason: Invalid fleet configuration.
Overrides t2.nano, us-west-2c, LINUX. Launching EC2 instance failed.