0

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.

1 Answers1

1

The Invalid fleet configuration. Overrides t2.nano error message means that the t2.nano instance type is not a supported spot instance type in that region.

Take a look at the spot instance advisor for supported instance types.

Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
  • Removed `t2.nano` type and the error changed to `Could not launch Spot Instances. MaxSpotInstanceCountExceeded - Max spot instance count exceeded.` Marking solved as your solution resolves the issue of the post. – Parth Jaggi Jan 22 '19 at 12:09
  • You probably hit a `soft` limit. You can file a support case to raise the limit for the account. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-limits.html – Henrik Pingel Jan 22 '19 at 12:15