resource "aws_launch_template" "webserver" {
name_prefix = "${var.environment_name}-web-"
image_id = data.aws_ami.web.id
instance_type = var.web_instance_priority_1
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "webserver-scaling-group" {
name = "asg-${aws_launch_template.webserver.id}-${aws_launch_template.webserver.latest_version}"
vpc_zone_identifier = sort(data.aws_subnet_ids.private_subnets.ids)
desired_capacity = 2
min_size = 2
max_size = 4
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.webserver.id
version = "$Latest"
}
override {
instance_type = var.web_instance_priority_1
}
override {
instance_type = var.web_instance_priority_2
}
override {
instance_type = var.web_instance_priority_3
}
override {
instance_type = var.web_instance_priority_4
}
}
instances_distribution {
on_demand_base_capacity = 2
on_demand_percentage_above_base_capacity = 0
}
}
target_group_arns = [aws_alb_target_group.web.arn]
health_check_type = "ELB"
wait_for_elb_capacity = 2
wait_for_capacity_timeout = "15m"
lifecycle {
create_before_destroy = true
}
}
When the launch template gets updated due to an AMI change, the auto scaling group should get destroyed and recreated successfully.
Receive the error below:
Error: Error creating AutoScaling Group: ValidationError: Max bound, 0, must be greater than or equal to OnDemandBaseCapacity, 2.
status code: 400, request id: e560b0d0-aa2d-11e9-af61-6f9caecbaf19
on main.tf line 482, in resource "aws_autoscaling_group" "webserver-scaling-group":
482: resource "aws_autoscaling_group" "webserver-scaling-group" {
terraform apply
When we used launch configurations, the ASG would get destroyed and recreated perfectly. We switched to launch templates in order to begin using the mixed instance policy, and that is when we started encountering this. Interestingly, if you set the on_demand_base_capacity to zero, it works fine.
I'm getting this when I have a initial_lifecycle_hook in my ASG. Works fine without it.
While not directly related to this I just switched to a mix instance policy and now I get this exact error on scheduled scaling actions when I attempt to set min, max and desired to 0. If I get keep my ASG max value above or equal to the OnDemandBaseCapacity value it has no issues. So it's probably not a provider specific thing but an AWS API thing. Could probably just add some error checking to the provider for this particular scenario I guess?
Executing scheduled action ScaleDown. Status Reason: Max bound, 0, must be greater than or equal to OnDemandBaseCapacity, 1.
I think there's a similar post on the forum with a minimum repro: https://discuss.hashicorp.com/t/cannot-create-asg-with-mixed-instances-and-lifecycle-hooks/13400
Most helpful comment
I'm getting this when I have a
initial_lifecycle_hookin my ASG. Works fine without it.