我正在尝试在ASG后面添加实例。这就是我想到的。
main.tf
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";,CCD_ 2确实显示了这一点;
# 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_resource,aws_launch_template.m-web-asg[aws_launch_template
上的triggers
会对其进行更改。null_resource
将使用本地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
}
}
这样可以确保启动模板中发生的任何更改都反映到现有实例中。