'terraform apply throws an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 2
I have this piece of terraform code which is supposed to do Auto scaling on ELB. I am getting this error when doing a terraform apply. I had googled around but cannot find any good clues to fix this. Terraform plan is working fine though. Tried to swap the health check type between ELB and EC2 but it did not help.
Appreciate if anyone can advise how to fix this? Thanks.
│ Error: "myasg-2022040803194187280000000a": Waiting up to 10m0s: Need at least 2 healthy instances in ASG, have 0. Most recent activity: {
│ ActivityId: "be66001c-e1b2-6826-2682-7ee755717b9a",
│ AutoScalingGroupARN: "arn:aws:autoscaling:ap-southeast-1:500295128231:autoScalingGroup:8fa8db5c-17f2-490a-8331-a892b5f41323:autoScalingGroupName/myasg-2022040803194187280000000a",
│ AutoScalingGroupName: "myasg-2022040803194187280000000a",
│ Cause: "At 2022-04-08T03:29:38Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 2.",
│ Description: "Launching a new EC2 instance. Status Reason: The requested configuration is currently not supported. Please check the documentation for supported configurations. Launching EC2 instance failed.",
│ Details: "{\"Subnet ID\":\"subnet-0823ae89d15fcc88e\",\"Availability Zone\":\"ap-southeast-1a\"}",
│ EndTime: 2022-04-08 03:29:39 +0000 UTC,
│ Progress: 100,
│ StartTime: 2022-04-08 03:29:39.994 +0000 UTC,
│ StatusCode: "Failed",
│ StatusMessage: "The requested configuration is currently not supported. Please check the documentation for supported configurations. Launching EC2 instance failed."
│ }
│
│ with aws_autoscaling_group.my_asg,
│ on c13-03-autoscaling-resource.tf line 2, in resource "aws_autoscaling_group" "my_asg":
│ 2: resource "aws_autoscaling_group" "my_asg" {
│
╵
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
#version = "2.78.0"
version = "3.0.0"
# VPC Basic Details
name = "${local.name}-${var.vpc_name}"
cidr = var.vpc_cidr_block
azs = var.vpc_availability_zones
public_subnets = var.vpc_public_subnets
private_subnets = var.vpc_private_subnets
# Database Subnets
database_subnets = var.vpc_database_subnets
create_database_subnet_group = var.vpc_create_database_subnet_group
create_database_subnet_route_table = var.vpc_create_database_subnet_route_table
# create_database_internet_gateway_route = true
# create_database_nat_gateway_route = true
# VPC DNS Parameters
enable_dns_hostnames = true
enable_dns_support = true
tags = local.common_tags
vpc_tags = local.common_tags
# Additional Tags to Subnets
public_subnet_tags = {
Type = "Public Subnets"
}
private_subnet_tags = {
Type = "Private Subnets"
}
database_subnet_tags = {
Type = "Private Database Subnets"
}
}
# Terraform AWS Classic Load Balancer (ELB-CLB)
module "elb" {
source = "terraform-aws-modules/elb/aws"
version = "2.5.0"
name = "${local.name}-myelb"
subnets = [
module.vpc.public_subnets[0],
module.vpc.public_subnets[1]
]
# security_groups = [module.loadbalancer_sg.this_security_group_id]
security_groups = [module.loadbalancer_sg.security_group_id]
#internal = false
listener = [
{
instance_port = 80
instance_protocol = "HTTP"
lb_port = 80
lb_protocol = "HTTP"
},
{
instance_port = 80
instance_protocol = "HTTP"
lb_port = 81
lb_protocol = "HTTP"
},
]
health_check = {
target = "HTTP:80/"
interval = 30
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
}
# ELB attachments
number_of_instances = var.private_instance_count
instances = [
module.ec2_private.id[0],
module.ec2_private.id[1]
]
tags = local.common_tags
}
# Terraform AWS Classic Load Balancer (ELB-CLB) Outputs
output "this_elb_id" {
description = "The name of the ELB"
value = module.elb.this_elb_id
}
output "this_elb_name" {
description = "The name of the ELB"
value = module.elb.this_elb_name
}
output "this_elb_dns_name" {
description = "The DNS name of the ELB"
value = module.elb.this_elb_dns_name
}
output "this_elb_instances" {
description = "The list of instances in the ELB (if may be outdated, because instances are attached using elb_attachment resource)"
value = module.elb.this_elb_instances
}
output "this_elb_source_security_group_id" {
description = "The ID of the security group that you can use as part of your inbound rules for your load balancer's back-end application instances"
value = module.elb.this_elb_source_security_group_id
}
output "this_elb_zone_id" {
description = "The canonical hosted zone ID of the ELB (to be used in a Route 53 Alias record)"
value = module.elb.this_elb_zone_id
}
output "target_group_arns" {
description = "The ARN of the ELB"
value = module.elb.this_elb_arn
}
# VPC Input Variables
# VPC Name
variable "vpc_name" {
description = "VPC Name"
type = string
default = "symbiosis_vpc"
}
# VPC CIDR Block
variable "vpc_cidr_block" {
description = "VPC CIDR Block"
type = string
default = "10.0.0.0/16"
}
# VPC Availability Zones
variable "vpc_availability_zones" {
description = "VPC Availability Zones"
type = list(string)
default = ["ap-southeast-1a", "ap-southeast-1b"]
}
# VPC Public Subnets
variable "vpc_public_subnets" {
description = "VPC Public Subnets"
type = list(string)
default = ["10.0.101.0/24", "10.0.102.0/24"]
}
# VPC Private Subnets
variable "vpc_private_subnets" {
description = "VPC Private Subnets"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
# VPC Database Subnets
variable "vpc_database_subnets" {
description = "VPC Database Subnets"
type = list(string)
default = ["10.0.121.0/24", "10.0.122.0/24"]
}
# VPC Create Database Subnet Group (True / False)
variable "vpc_create_database_subnet_group" {
description = "VPC Create Database Subnet Group"
type = bool
default = true
}
# Autoscaling Group Resource
resource "aws_autoscaling_group" "my_asg" {
name_prefix = "myasg-"
desired_capacity = 2
max_size = 4
min_size = 2
vpc_zone_identifier = module.vpc.private_subnets
load_balancers = [ module.elb.this_elb_id ]
health_check_type = "ELB”
#health_check_grace_period = 300 # default is 300 seconds
launch_template {
id = aws_launch_template.my_launch_template.id
version = aws_launch_template.my_launch_template.latest_version
}
# Instance Refresh
instance_refresh {
strategy = "Rolling"
preferences {
# instance_warmup = 300 # Default behavior is to use the Auto Scaling Groups health check grace period value
min_healthy_percentage = 50
}
triggers = [ "desired_capacity" ] # You can add any argument from ASG here, if those has changes, ASG Instance Refresh will trigger
}
tag {
key = "Owners"
value = "Web-Team"
propagate_at_launch = true
}
}
# Launch Template Resource
resource "aws_launch_template" "my_launch_template" {
name = "my-launch-template"
description = "My Launch template"
image_id = data.aws_ami.amzlinux2.id
instance_type = var.instance_type
vpc_security_group_ids = [ module.private_sg.security_group_id ]
key_name = var.instance_keypair
user_data = filebase64("${path.module}/app1-install.sh")
ebs_optimized = true
#default_version = 1
update_default_version = true
block_device_mappings {
device_name = "/dev/sda1"
ebs {
#volume_size = 10
volume_size = 20 # LT Update Testing - Version 2 of LT
delete_on_termination = true
volume_type = "gp2" # default is gp2
}
}
monitoring {
enabled = true
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "myasg"
}
}
}
# Autoscaling Notifications
## AWS Bug for SNS Topic: https://stackoverflow.com/questions/62694223/cloudwatch-alarm-pending-confirmation
## Due to that create SNS Topic with unique name
## SNS - Topic
resource "aws_sns_topic" "myasg_sns_topic" {
name = "myasg-sns-topic-${random_pet.this.id}"
}
## SNS - Subscription
resource "aws_sns_topic_subscription" "myasg_sns_topic_subscription" {
topic_arn = aws_sns_topic.myasg_sns_topic.arn
protocol = "email"
endpoint = "[email protected]"
}
## Create Autoscaling Notification Resource
resource "aws_autoscaling_notification" "myasg_notifications" {
group_names = [aws_autoscaling_group.my_asg.id]
notifications = [
"autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_TERMINATE",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
]
topic_arn = aws_sns_topic.myasg_sns_topic.arn
}
# Launch Template Outputs
## launch_template_id
output "launch_template_id" {
description = "Launch Template ID"
value = aws_launch_template.my_launch_template.id
}
## launch_template_latest_version
output "launch_template_latest_version" {
description = "Launch Template Latest Version"
value = aws_launch_template.my_launch_template.latest_version
}
# Autoscaling Outputs
## autoscaling_group_id
output "autoscaling_group_id" {
description = "Autoscaling Group ID"
value = aws_autoscaling_group.my_asg.id
}
## autoscaling_group_name
output "autoscaling_group_name" {
description = "Autoscaling Group Name"
value = aws_autoscaling_group.my_asg.name
}
## autoscaling_group_arn
output "autoscaling_group_arn" {
description = "Autoscaling Group ARN"
value = aws_autoscaling_group.my_asg.arn
}
###### Target Tracking Scaling Policies ######
# TTS - Scaling Policy-1: Based on CPU Utilization
# Define Autoscaling Policies and Associate them to Autoscaling Group
resource "aws_autoscaling_policy" "avg_cpu_policy_greater_than_xx" {
name = "avg-cpu-policy-greater-than-xx"
policy_type = "TargetTrackingScaling" # Important Note: The policy type, either "SimpleScaling", "StepScaling" or "TargetTrackingScaling". If this value isn't provided, AWS will default to "SimpleScaling."
autoscaling_group_name = aws_autoscaling_group.my_asg.id
estimated_instance_warmup = 180 # defaults to ASG default cooldown 300 seconds if not set
# CPU Utilization is above 50
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 50.0
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
