Мой скрипт Terraform выполняет следующие действия:
- Создает AMI при каждом запуске
- Создает новый шаблон запуска (из-за него с использованием только что созданного AMI)
- Создает группу автоматического масштабирования с использованием шаблона запуска
Я хочу, чтобы имя шаблона запуска оставалось неизменным, чтобы группа автоматического масштабирования могла просто обновиться для использования последней версии шаблона запуска.
К сожалению, Terraform пытается создать новый шаблон запуска (вместо простого обновления существующего, если он существует).Это приводит к ошибке:
aws_launch_template.launch_template: InvalidLaunchTemplateName.AlreadyExistsException:
Launch template name already in use.
Есть ли способ выполнить то, что я хочу (не прибегая к использованию префикса имени, а не имени в Launch-Template, поскольку это противоречит всей цели наличия версий)?
РЕДАКТИРОВАТЬ: Вот план
~ 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
}
}