在使用中的错误已经启动模板名称

问题描述 投票:1回答:1

我Terraform脚本执行以下操作:

  1. 构建一个与AMI每次运行
  2. 构建一个新的发射模板(由于它使用刚创建的AMI来)
  3. 构建使用启动模板自动缩放组

我想推出模板的名称保持不变,以便自动缩放组只需更新使用最新版本的启动模板。

不幸的是,Terraform试图建立一个新的发射模板(而不是简单地更新现有如果存在)。这将导致错误:

aws_launch_template.launch_template: InvalidLaunchTemplateName.AlreadyExistsException: 
Launch template name already in use.

有没有办法来完成我想要的东西(而不是诉诸使用名称的前缀,而不是名称的启动,模板为违背了版本的全部目的)?

编辑:这是计划

  ~ module.continuous_pull_app.aws_autoscaling_group.pull_asg
      launch_template.0.id:                                  "lt-xxxxxxxxxxxxxxxxx" => "${var.launch_template_id}"
      launch_template.0.version:                             "1" => "${var.launch_template_version}"

  -/+ module.create_ami.aws_ami_from_instance.generic_ami (new resource required)
      id:                                                    "ami-xxxxxxxxxxxxxxxxx" => <computed> (forces new resource)
      architecture:                                          "x86_64" => <computed>
      ebs_block_device.#:                                    "1" => <computed>
      ena_support:                                           "true" => <computed>
      ephemeral_block_device.#:                              "0" => <computed>
      image_location:                                        "xxxxxxxxxxxx/generic-usage-ami-2019-02-07T212337Z-nonprod-2" => <computed>
      kernel_id:                                             "" => <computed>
      manage_ebs_snapshots:                                  "true" => <computed>
      name:                                                  "generic-usage-ami-2019-02-07T212337Z-nonprod-2" => "generic-usage-ami-2019-02-08T133220Z-nonprod-2" (forces new resource)
      ramdisk_id:                                            "" => <computed>
      root_device_name:                                      "/dev/xvda" => <computed>
      root_snapshot_id:                                      "snap-xxxxxxxxxxxxxxxxx" => <computed>
      snapshot_without_reboot:                               "false" => "false"
      source_instance_id:                                    "i-xxxxxxxxxxxxxxxxx" => "i-xxxxxxxxxxxxxxxxx"
      sriov_net_support:                                     "simple" => <computed>
      tags.%:                                                "2" => "2"
      tags.Name:                                             "Generic AMI for continuous process nonprod-2" => "Generic AMI for continuous process nonprod-2"
      tags.created-by:                                       "terraform" => "terraform"
      virtualization_type:                                   "hvm" => <computed>

  ~ module.create_ami.aws_instance.ec2
      tags.Name:                                             "generic-usage-ami-2019-02-07T212337Z-nonprod-2" => "generic-usage-ami-2019-02-08T133220Z-nonprod-2"

  + module.create_launch_template.module.pull.aws_launch_template.launch_template
      id:                                                    <computed>
      arn:                                                   <computed>
      block_device_mappings.#:                               "1"
      block_device_mappings.0.device_name:                   "/dev/xvda"
      block_device_mappings.0.ebs.#:                         "1"
      block_device_mappings.0.ebs.0.iops:                    <computed>
      block_device_mappings.0.ebs.0.volume_size:             "16"
      block_device_mappings.0.ebs.0.volume_type:             <computed>
      default_version:                                       <computed>
      iam_instance_profile.#:                                "1"
      iam_instance_profile.0.name:                           "role-foo"
      image_id:                                              "${var.generic_ami_id}"
      instance_market_options.#:                             "1"
      instance_market_options.0.market_type:                 "spot"
      instance_market_options.0.spot_options.#:              "1"
      instance_market_options.0.spot_options.0.valid_until:  <computed>
      instance_type:                                         "t2.micro"
      key_name:                                              "foo"
      latest_version:                                        "0"
      name:                                                  "PullTemplate-nonprod-2"
      user_data:                                             "..."
      vpc_security_group_ids.#:                              "1"
      vpc_security_group_ids.34235319:                       "sg-xxxxxxxx"

此次推出的模板资源:

resource "aws_launch_template" "launch_template" {
  //  If use_spot_pricing is true (which translates to 1), this resource is not created (i.e. count = 0).
  count                = "${1 - var.use_spot_pricing}"
//  name_prefix          = "${var.resource_name_prefix}${var.envSuffix}-"
  name                 = "${var.resource_name_prefix}${var.envSuffix}"
  image_id             = "${var.generic_ami_id}"
  instance_type        = "${var.instance_type}"
  key_name             = "${var.key_name}"
  vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
  user_data            = "${base64encode(data.template_file.lc_user_data.rendered)}"

  iam_instance_profile {
    name = "${var.iam_instance_profile}"
  }

  instance_market_options {
    market_type = "spot"

    spot_options {
      max_price = "${var.max_price}"
    }
  }

  block_device_mappings {
    device_name = "/dev/xvda"

    ebs {
      volume_size = "${var.volume_size}"
    }
  }

  lifecycle {
    create_before_destroy = true
  }
}

data "template_file" "lc_user_data" {
...
}

output "launch_template_id" {
  //  There should only ever be 1 instance
  value = "${ aws_launch_template.launch_template.0.id }"
}

output "launch_template_version" {
  //  There should only ever be 1 instance
  value = "${ aws_launch_template.launch_template.0.latest_version }"
}

该ASG资源:

resource "aws_autoscaling_group" "pull_asg" {

  name                = "Pull${var.envSuffix}"
  vpc_zone_identifier = ["${split(",", var.vpc_private_subnets)}"]

  launch_template = {
    id = "${var.launch_template_id}"
    version = "${var.launch_template_version}"
  }

  termination_policies      = ["OldestInstance"]
  max_size                  = "${var.pull_asg_max_size}"
  min_size                  = "${var.pull_asg_min_size}"
  desired_capacity          = "${var.pull_asg_desired_capacity}"
  health_check_grace_period = "${var.pull_asg_health_check_grace_period}"
  health_check_type         = "${var.health_check_type}"
  load_balancers            = ["${aws_elb.pull_app_elb.name}"]
  wait_for_elb_capacity     = "${var.pull_asg_wait_for_elb_capacity}"

  lifecycle {
    create_before_destroy = true
  }

  tag {
    key                 = "Name"
    value               = "${var.app_instance_name}"
    propagate_at_launch = true
  }

  tag {
    key                 = "spot-enabled"
    value               = "${var.spot_enabled}"
    propagate_at_launch = true
  }
}
terraform terraform-provider-aws
1个回答
0
投票

我也面临着launchconfiguration类似的问题。当你在创作中的launchconfiguration / launchtemplate提供的名称,如果有情况时,该LC / LT需要娱乐,terraform首先尝试破坏现有一个之前创建新的资源,并开始引发错误说资源存在。

解决这个问题,不提供发射模板名称参数,让这个被terraform并在ASG使用,而不是一个变量的引用ID管理的随机名称的最佳方式。

AWS_launch_template.launch template-Terra form-那么.ID

为以防万一,需要在发射模板更清晰。使用nameprifix或namesuffix,而不是名字。

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.