使用Terraform在AWS中创建自动缩放组。
在Terraform的aws_autoscaling_group
资源中使用mixed_instances_policy
。
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}"
}
启动模板中的instance_type
是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
}
}
}
这将创建1个按需实例和1个现货实例(由于所需容量为2)。但是它将创建1个按需实例,但无法创建竞价型实例。
“自动缩放”组的“活动历史记录”尝试创建实例,但出现以下错误:
Launching a new EC2 instance. Status Reason: Invalid fleet configuration.
Overrides t2.nano, us-west-2c, LINUX. Launching EC2 instance failed.