启动配置更新terraform后实例未刷新
我正在尝试更新启动配置用户数据。但是在申请后,启动配置正在创建和更新 ASG。但是正在运行的实例仍然带有旧的用户数据。为什么这样 ?
下面是启动配置和 ASG 块。
resource "aws_launch_configuration" "BackEndWebLaunchConfig" {
name_prefix = "${var.component_name}-BackEndWebLaunchConfig"
user_data = file("user_data/${terraform.workspace}/vision-be-user-data.sh")
image_id = var.ASLCWEBAPPSAMI
instance_type = var.ASGWebAppsInstanceType
key_name = var.ssh_key_name
security_groups = [module.vpc.sgssh, aws_security_group.vision_backend_EC2SG.id]
root_block_device {
volume_size = var.EC2_EBS_SIZE
volume_type = "standard"
encrypted = true
}
#iam_instance_profile = var.EC2_instance_profile
associate_public_ip_address = false
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "vision_asg" {
name = "${var.component_name}-BackEnd-ASG-TF"
max_size = var.ASGWEBAPPSMaxSize
min_size = var.ASGWEBAPPSMinSize
health_check_grace_period = 300
force_delete = true
health_check_type = "ELB"
desired_capacity = var.ASGWEBAPPSDesiredSize
launch_configuration = aws_launch_configuration.BackEndWebLaunchConfig.name
target_group_arns = [module.loadbalancer.visionalb_ext_tg_arn]
vpc_zone_identifier = [module.vpc.PrivateSubnet0, module.vpc.PrivateSubnet1]
termination_policies = ["OldestInstance"]
lifecycle {
create_before_destroy = true
}
tags = [
{
key = "Name"
value = "${var.component_name}-BackEndWebASG-TF"
propagate_at_launch = true
},
{
key = "component"
value = var.component_name
propagate_at_launch = true
},
{
key = "tier"
value = "web"
propagate_at_launch = true
}
]
depends_on = [
aws_sns_topic.BackEndSNSTopic, aws_launch_configuration.BackEndWebLaunchConfig
]
}
应用后,新的启动配置正在创建,但 ec2 机器没有刷新。
aws_autoscaling_group.vision_asg: Refreshing state... [id=BackEnd-ASG-TF]
aws_autoscaling_policy.BEWebScaleUpPolicy: Refreshing state... [id=BEWebScaleUpPolicy]
aws_autoscaling_notification.vision_asg_notification: Refreshing state... [id=arn:aws:sns:us-east-1:193676128801:BackEndApplication-TF]
aws_autoscaling_policy.BEWebScaleDownPolicy: Refreshing state... [id=BEWebScaleDownPolicy]
aws_cloudwatch_metric_alarm.BEScaleDownNotifyAlarm: Refreshing state... [id=BEScaleDownNotifyAlarm]
aws_cloudwatch_metric_alarm.ScaleUPNotifyAlarm: Refreshing state... [id=ScaleUPNotifyAlarm]
aws_launch_configuration.BackEndWebLaunchConfig: Creating...
aws_launch_configuration.BackEndWebLaunchConfig: Creation complete after 8s [id=BackEndWebLaunchConfig20210508105416185400000001]
aws_autoscaling_group.vision_asg: Modifying... [id=BackEnd-ASG-TF]
aws_autoscaling_group.vision_asg: Modifications complete after 4s [id=BackEnd-ASG-TF]
aws_launch_configuration.BackEndWebLaunchConfig (2530c36e): Destroying... [id=BackEndWebLaunchConfig20210508103324724600000001]
aws_launch_configuration.BackEndWebLaunchConfig: Destruction complete after 2s
Apply complete! Resources: 1 added, 1 changed, 1 destroyed.
Outputs:
如果我做错了什么,请告诉我。
回答
有关更改 Auto Scaling 组的启动配置的 AWS 文档中回答了此问题,其中说:
更改 Auto Scaling 组的启动配置后,将使用新配置选项启动任何新实例,但现有实例不受影响。要更新现有实例,请终止它们以便它们被您的 Auto Scaling 组替换,或者允许自动扩展根据您的终止策略逐渐用新实例替换旧实例。
要在 期间执行实例刷新terraform apply,您可以执行以下操作:
- 将您的 Terraform AWS Provider 升级到至少 3.22.0。
instance_refresh向您的aws_autoscaling_group资源添加一个块。这是一个示例,从他们的文档中提供:
instance_refresh {
strategy = "Rolling"
preferences {
// You probably want more than 50% healthy depending on how much headroom you have
min_healthy_percentage = 50
}
// Depending the triggers you wish to configure, you may not want to include this
triggers = ["tag"]
}
在相关文件中指出:
刷新将始终由launch_configuration、launch_template 或mixed_instances_policy 中的任何更改触发。
要考虑的最后一个注意事项。根据您的用例,您可能更喜欢单独控制实例重启的节奏terraform apply。在某些项目中,我们避免了这种情况,这样我们就可以terraform apply在 CI/CD 中运行,而不必担心在不合适的时间更换运行我们的生产工作负载的机器。请注意,terraform当前可以触发实例刷新,但不会监控实例刷新的成功或失败。