We have setup our infrastructure for a project using Terraform, including the code-deploy, ALB and auto-scaling groups. So far, we were doing in-place deployments. But now we're trying to switch to Blue/Green deployment. Since CodeDeploy Blue/Green deployment replaces the entire autoscaling group on successful deployments, the old state of Autoscaling group in the Terraform state file would become stale, and would not reflect the new Autoscaling group that was added by CodeDeploy service. Is there any known way to overcome this?
2 Answers
Depending on how you're triggering your Code Deploy deployment, you could run a Terraform import as a post-deployment hook in your deployment scripts to update the Terraform state to point at the new autoscaling group. You would need to fetch the name of the new ASG somehow via one of the many client libraries or the CLI
terraform import aws_autoscaling_group.some_asg_identifier name-of-your-replacement-asg
You can use
lifecycle {
ignore_changes = [autoscaling_groups]
}
in the aws_codedeploy_deployment_group
.
You also have to set the autoscaling_groups
to []
in the aws_codedeploy_deployment_group
since the newly created autoscaling group will be a different one (created by CodeDeploy) when the CodeDeploy deploys a new green environment.
So, the above code will ignore the changes that happens with the autoscaling group deletion/creation. This is because CodeDeploy takes control of the autoscaling group creation once the blue-green deployment is implemented.
terraform apply
to update the launch configuration, then Consul to increase (then decrease) the max size of the ASG. – Iso