【问题标题】:Terraform - ASGs with Mixed Instance PoliciesTerraform - 具有混合实例策略的 ASG
【发布时间】:2019-06-21 08:35:06
【问题描述】:

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

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

【问题讨论】:

  • 你能解决这个问题吗?在我的情况下,没有错误,但没有一个实例是现场实例,而且都是按需提供的。

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


【解决方案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,
                                            },
                                        },
                                    },
                                },

【讨论】:

  • 覆盖似乎不是可选的,如果我从示例中删除它们,我会收到此错误:aws_autoscaling_group.example: Error creating AutoScaling Group: ValidationError: The number of LaunchTemplateOverrides must be between 2 and 20.
猜你喜欢
  • 2022-08-20
  • 2021-02-15
  • 2018-11-13
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2013-02-13
相关资源
最近更新 更多