11

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?

John Kentucky
  • 876
  • 6
  • 10
  • 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 Jun 08 '18 at 09:31
  • 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 Jun 08 '18 at 09:59

2 Answers2

1

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.

user198530
  • 331
  • 1
  • 3
  • 10
  • As the OP mentions, the original ASG is also created by terraform and when removed, terraform keeps creating a new one every time. So this does not solve the issue mentioned here. – anishtain4 Jun 26 '23 at 18:08
0

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