6
votes

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
Do you need to use CodeDeploy for your deployments? I usually just use terraform apply to update the launch configuration, then Consul to increase (then decrease) the max size of the ASG.Iso
Yes, we are using CodeDeploy for deployments, with the Blue/Green configuration for deployment group. I am aware of the other blue/green method through terraform when we only require to switch between AMIs. We make deployments through Jenkins using CodeDeploy, and during this time, it launches new fleet of instances in ASG and deploys new code into it. Once the instances are healthy, it terminates the old ASG.John Kentucky

2 Answers

0
votes

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
0
votes

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.