I have been trying use terraform for a while . any suggestions/tips on how I can handle blue green deployments . I see how terraform enables it with auto scale groups on aws . But I’d like run some Cucumber acceptance tests on the new Asg before routing traffic to it . Is there a way in terraform to retain the old asg for a period , if In case I need to do a rollback . Can this be accomplished with terraform ... thanks in advance .

2 Answers 2


It all depends on how you are deploying your code.

(First, IMHO, I don't think that Terraform is the right hammer for this operation. Terraform responsibility is to build the necessary infrastructure for your application, not to be the tool used for deploying it. But, as always, "the right/best one" will depend on lots of factors and, depending on the case/scale, it may be the "good enough" solution)

My suggestion would be, in summary:

  1. Deploy your application in the "unused" environment
  2. Test it
  3. Change the domain name to point to it

Assuming you have 2 AutoScaling groups already configured. One called asg-blue and the other called asg-green. And that the current version resides on asg-blue and in terraform you have an aws_route53_record pointing to it. You would need to follow these steps, each one with a terraform apply before the next:

  • Deploy the newer version of your application on asg-green
    • I.e. update everything that is needed to deploy your application version. It may be the AMI you point on your Launch Configuration or any other way that you do exclusively using Terraform.
  • Test that everything is running as expected
    • Here you could use the domain-name provided for each ASG/ELB or one already configured on terraform
  • Update the domain name to pont to asg-green ASG/ELB on the aws_route53_record
  • Reduce the number of machines needed on asg-blue to don't use too many resources
    • Depending on your case you can configure for 0 machines

This way you can have your B/G deployment, but for every deployment you'd need at least two manual interventions in your terraform (the deployment of the new version and updating the domain name).

But with this strategy you should take a lot of care to not deploy on the current ASG being used.

Of course if your terraform/aws fu is good, instead of reusing ASGs, you could delete and create them as needed.

Hope that this help in having a "big picture" of the entire process. But I'd suggest that you research and start using tools more focused on deployment, like AWS CodeDeploy.

  • you might consider also to use terraform workspaces, but still is changing infrastructure not deploy code. Here is the boundry about deployment and infrastructure changes to be defined properly. strictly should not be changed the infrastructure to support new code releases. In some circustances yes.
    – Raffaello
    Commented Jan 4, 2019 at 16:17

I had a similar question and this answer was quite helpful. It sketches pattern on how you can do blue-green deployments in Terraform.

But having said that there is no clearly best solution as far as I know. I think the most common approach for your use case would be to have a dedicated "staging" environment that uses a setup that is very close to the production one (sharing the same Terraform modules, but with smaller auto scaling groups). You can use it for the final tests, maybe even route some fraction of the production traffic over it.

If there are no errors in staging, you simplify roll it out in production and hope that you found most critical bugs before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.