I am working on an autoscaling setup using Terraform and Jenkins. If there is an existing ASG and I run the Jenkins pipeline, Terraform will create a new ASG, wait for instances from new ASG to get healthy and then destroy the older ASG.
I am using a combination of arguments to achieve this -
Firstly lifecycle so that new asg is created before the older one is destroyed
lifecycle { create_before_destroy = true }
then I am deriving ASG name from launch configuration name to force a new resource every time i run the pipeline (My terraform script creates both asg and launch configuration)
resource "aws_autoscaling_group""bar" {
name = "$(aws_launc_configuration.lc.name}"
.
.
}
and lastly using wait_for_capacity
option so that terraform destroys older asg only if instances from new asg are healthy.
In case if the instances from new ASG do not report healthy, Jenkins pipeline fails without any rollback or destroying the new ASG. This leaves me with two ASG, one with healthy instances and another one with unhealthy.
I was trying to use -
terraform destroy -input=false -force -var-file=tfvars_file -target aws_autoscaling_group.my_asg_name -target aws_launch_configuration.my_lc_name
But it doesn't delete anything and shows destroyed resources: 0 as the message. Any thoughts?
Thanks in advance.