We do enable blue/green deployments, but there isn't a "one-size-fits-all" method to perform them; instead, we have a three-step process to come up with design patterns for any application. What we will do here is create a state machine for the deployments, with Terraform being the tool to move between states.
First, we break the blue-green deployment down into desired states. Second, we determine what mechanisms are needed to make the changes between the states. Third, we put it all into a single flow using a combination of terraform functions, cloud components, and/or scripts as needed.
So let's walk through a simple application deployment: a 2-tier web application, with a single instance, being routed to via a Load Balancer. Note that the site's DNS name is pointing to the Load Balancer.
- Green application online, blue application down, traffic goes to green
- Green application online, blue application online, traffic goes to green
- Green application online, blue application online, traffic goes to blue
- Green application down, blue application online, traffic goes to blue
There are three 'sub-states' that can change: the state of the Green, the state of Blue, and the direction of traffic. So we need mechanisms to manage those three things. Additionally, we know that the actual code deployed is going to change, where the main difference is (typically) which git commit we're checking out.
We already know that the Blue and Green application state can be changed via Terraform because that's simply deploying and destroying instances. The easiest way to achieve this in Terraform code is to lay out a resource for the "blue" instance and "green" instance separately, and use variables to make the differentiation between the two instances. So we'll rely on Terraform to do that.
However, Terraform does not control traffic flow -- load balancers and DNS do. So instead of relying on Terraform to do it, we'll use Terraform to issue the correct commands to those components to do what we need them to do.
Note: The following code is untested, and there are a few required paramters left out (such as which ami_id to use) to highlight the important parts for this example