gpt4 book ai didi

amazon-web-services - 启动配置更新 terraform 后实例未刷新

转载 作者:行者123 更新时间:2023-12-04 16:24:42 25 4
gpt4 key购买 nike

我正在尝试更新启动配置用户数据。但在申请后,启动配置正在创建和更新 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:
如果我做错了什么,请告诉我。

最佳答案

此问题在 AWS 文档 Changing the launch configuration for an Auto Scaling group 中得到了解答。 ,其中说:

After you change the launch configuration for an Auto Scaling group, any new instances are launched using the new configuration options, but existing instances are not affected. To update the existing instances, terminate them so that they are replaced by your Auto Scaling group, or allow automatic scaling to gradually replace older instances with newer instances based on your termination policies.


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"]
    }
    associated documentation状态:

    A refresh will always be triggered by a change in any of launch_configuration, launch_template, or mixed_instances_policy.


    最后要考虑的一个注意事项。根据您的用例,您可能更喜欢与 terraform apply 分开控制实例重启的节奏。 .在一些项目中,我们避免了这种情况,以便我们可以运行 terraform apply在 CI/CD 中,更少担心在不合适的时间更换运行我们的生产工作负载的机器。请注意 terraform当前可以触发实例刷新,但不会监控实例刷新的成功或失败。

    关于amazon-web-services - 启动配置更新 terraform 后实例未刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67446898/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com