繁体   English   中英

如果我更改 AMI ID,Terraform 不会创建新的 ec2 实例

[英]Terraform does not create a new ec2 instance if I change the AMI ID

我正在尝试在 ASG 后面添加实例。 这就是我想出的。

主程序

data "aws_availability_zones" "available" {
  state = "available"
}


resource "aws_launch_template" "m-web-asg" {
  name = "m-web-asg"

  capacity_reservation_specification {
    capacity_reservation_preference = "open"
  }
  image_id = var.web_image_id
  instance_initiated_shutdown_behavior = "terminate"
  instance_type = "t2.micro"
  key_name = "keyname"

  monitoring {
    enabled = true
  }

  network_interfaces {
    security_groups             = var.m_web_server_security_group_ids
    associate_public_ip_address = true
  }

  # vpc_security_group_ids = var.m_web_server_security_group_ids

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "test"
    }
  }

  user_data = filebase64("external-files/instance_provisioner.sh")
}


resource "aws_autoscaling_group" "m-web-asg" {
  name                 = "m-web-asg"
  min_size             = 1
  max_size             = 3
  desired_capacity     = 1
  launch_template {
    name      = aws_launch_template.m-web-asg.name
  }
  vpc_zone_identifier  = var.m_subnet_ids
  tag {
    key                 = "env"
    value               = "testing"
    propagate_at_launch = true
  }
}

resource "aws_autoscaling_attachment" "m-web-asg" {
  autoscaling_group_name = aws_autoscaling_group.m-web-asg.id
  lb_target_group_arn   = var.target_group_arn
}

resource "aws_autoscaling_policy" "scale_down" {
  name                   = "m-web-asg-scale-down"
  autoscaling_group_name = aws_autoscaling_group.m-web-asg.name
  adjustment_type        = "ChangeInCapacity"
  scaling_adjustment     = -1
  cooldown               = 120
}

resource "aws_cloudwatch_metric_alarm" "scale_down" {
  alarm_description   = "Monitors CPU utilization for m-web-asg ASG"
  alarm_actions       = [aws_autoscaling_policy.scale_down.arn]
  alarm_name          = "m-web-asg-scale-down"
  comparison_operator = "LessThanOrEqualToThreshold"
  namespace           = "AWS/EC2"
  metric_name         = "CPUUtilization"
  threshold           = "30"
  evaluation_periods  = "2"
  period              = "120"
  statistic           = "Average"

  dimensions = {
    AutoScalingGroupName = aws_autoscaling_group.m-web-asg.name
  }
}

resource "aws_autoscaling_policy" "scale_up" {
  name                   = "m-web-asg-scale-up"
  autoscaling_group_name = aws_autoscaling_group.m-web-asg.name
  adjustment_type        = "ChangeInCapacity"
  scaling_adjustment     = 1
  cooldown               = 120
}

resource "aws_cloudwatch_metric_alarm" "scale_up" {
  alarm_description   = "Monitors CPU utilization for m-web-asg ASG"
  alarm_actions       = [aws_autoscaling_policy.scale_up.arn]
  alarm_name          = "m-web-asg-scale-up"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  namespace           = "AWS/EC2"
  metric_name         = "CPUUtilization"
  threshold           = "75"
  evaluation_periods  = "2"
  period              = "120"
  statistic           = "Average"

  dimensions = {
    AutoScalingGroupName = aws_autoscaling_group.m-web-asg.name
  }
}


terraform {
  backend "s3" {
    bucket = "terraform-m-backend"
    key = "terraform"
    region = "us-west-2"
    dynamodb_table = "terraform-m-lock"
  }  
}

变量.tf

variable m_vpc_id {
  type        = string
  default     = "vpc-xxxx"
  description = "This is the VPC ID for web servers of m AWS"
}

variable m_subnet_ids {
  type        = list(string)
  default     = ["subnet-xxxx"]
  description = "This is the Subnet ID for web servers of m AWS"
}

variable m_web_server_security_group_ids {
  type        = list(string)
  default     = ["sg-xxxx", "sg-yyyy"]
  description = "This is the Security Groups ID for web servers of m AWS"
}

variable target_group_arn {
  type        = string
  default     = "arn:aws:elasticloadbalancing:us-west-2:xxx:targetgroup/testing/xxxx"
  description = "This is the target_group_arn for web servers of m AWS"
}

variable web_image_id {
  description = "This is the image id that we will use to bring up web server"
  type        = string
  default     = "ami-xxxx"
}

当我最初执行terraform init/plan/apply时,一切正常。

现在,如果我将变量 web_image_id 从“ami-xxxx”更改为“ami-yyyy”, terraform plan确实显示了这一点;

# aws_launch_template.m-web-asg will be updated in-place

然后在应用时注意更改。 旧的 ec2 实例保持原样运行。

我原以为 AMI 中的更改会终止现有实例并使用新 AMI 创建一个新实例。

我错过了什么?

当您更改AMI时,您的代码只会更新aws_launch_template.m-web-asg[aws_launch_template.m-web-asg 不会影响您的 ASG 中当前正在运行的实例 将会发生的事情是,当 ASG 扩展时,新实例将使用新 AMI 运行,而旧实例仍将运行旧AMI

您可以做的是设置一个null_resourcetriggers您的aws_launch_template.m-web-asg[aws_launch_template更改。 null_resource将使用local-exec通过 AWS CLI 执行ASG 刷新

感谢@Marcin,我阅读了 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 = 80
      instance_warmup        = 10

    }
  }

这可确保启动模板中发生的任何更改都会反映到现有实例中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM