Terraform-具有混合实例策略的ASG

时间:2019-01-28 10:51:31

标签: amazon-web-services terraform autoscaling terraform-provider-aws infrastructure-as-a-code

我正在将AWS与Terraform结合使用以提高基础性能,但特别是在使用混合实例策略来简化ASG时遇到了问题。我正在尝试启动一个ASG,在这种情况下,一个实例总是按需提供的,其余的点(都是相同类型的),从表面上看,这看起来很简单,但我已经尝试了一段时间,并不断遇到各种错误。这是我目前正在使用的:

 resource "aws_launch_template" "lt" {
   name_prefix            = "danny_test-"
   vpc_security_group_ids = [
     "${module.sec_groups.security_group_id_common}",
     "${module.sec_groups.security_group_id_ssh}",
     "${module.sec_groups.security_group_id_web}"
   ]
   image_id               = "${module.core_ami.core_ami_id}"
   instance_type          = "t2.small"
   key_name               = "${var.ssh_privkey_name}"
   user_data              = "${base64encode(data.template_file.common_user_data.rendered)}"

   iam_instance_profile {
     name = "${module.infradev_remote_state.iam_profile_updater_id}"
   }

   lifecycle {
     create_before_destroy = true
   }
}

resource "aws_autoscaling_group" "asg" {
  name                      = "danny_test"
  vpc_zone_identifier       = ["${split(",", module.environment.private_subnet_ids)}"]
  #launch_configuration      = "${aws_launch_configuration.lc.name}"
  min_size                  = "0"
  max_size                  = "1"
  desired_capacity          = "1"
  health_check_grace_period = "600"
  health_check_type         = "EC2"

  launch_template {
    id = "${aws_launch_template.lt.id}"
  }

  mixed_instances_policy {
    instances_distribution {
      on_demand_base_capacity = "1"
    }
  }
}

但是我得到这个错误:

Error: aws_autoscaling_group.asg: "mixed_instances_policy.0.launch_template": required field is not set

根据文档 launch_template 是可选的。我曾尝试设置此设置,但它只是陷入了一些可选设置的困境中,其中一些我根本不想设置(例如覆盖)。

实现我追求的目标的正确方法是什么?无论哪种方式,基于上述情况,文档似乎至少部分是错误的...

1 个答案:

答案 0 :(得分:1)

该文档似乎是错误的,因为它被设置为Required field in the actual code

        "mixed_instances_policy": {
            Type:     schema.TypeList,
            Optional: true,
            MaxItems: 1,
            Elem: &schema.Resource{
                Schema: map[string]*schema.Schema{
                    # ...
                    "launch_template": {
                        Type:     schema.TypeList,
                        Required: true,
                        MinItems: 1,
                        MaxItems: 1,

aws_autoscaling_group resource docs显示了一个有用的示例,其中包括您提到的可选替代:

resource "aws_launch_template" "example" {
  name_prefix   = "example"
  image_id      = "${data.aws_ami.example.id}"
  instance_type = "c5.large"
}

resource "aws_autoscaling_group" "example" {
  availability_zones = ["us-east-1a"]
  desired_capacity   = 1
  max_size           = 1
  min_size           = 1

  mixed_instances_policy {
    launch_template {
      launch_template_specification {
        launch_template_id = "${aws_launch_template.example.id}"
      }

      override {
        instance_type = "c4.large"
      }

      override {
        instance_type = "c3.large"
      }
    }
  }
}

这些替代实际上是可选的,如the code所示:

                                "override": {
                                    Type:     schema.TypeList,
                                    Optional: true,
                                    Elem: &schema.Resource{
                                        Schema: map[string]*schema.Schema{
                                            "instance_type": {
                                                Type:     schema.TypeString,
                                                Optional: true,
                                            },
                                        },
                                    },
                                },