5

我正在使用 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是可选的。我已经尝试过设置它,但它只是进入了一个所谓的可选设置的兔子洞,其中一些我根本不想设置(比如覆盖)。

实现我所追求的正确方法是什么?无论哪种方式,基于上述内容,文档至少部分错误......

4

1 回答 1

3

该文档似乎是错误的,因为它被设置为Required实际代码中的一个字段

        "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 "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"
      }
    }
  }
}

这些覆盖实际上是可选的,如代码所示:

                                "override": {
                                    Type:     schema.TypeList,
                                    Optional: true,
                                    Elem: &schema.Resource{
                                        Schema: map[string]*schema.Schema{
                                            "instance_type": {
                                                Type:     schema.TypeString,
                                                Optional: true,
                                            },
                                        },
                                    },
                                },
于 2019-01-28T11:24:21.617 回答