How can I switch my load balancer to the active environment in a Blue/Green Environment setup in Octopus Deploy?

The exact answer will depend on the type of load balancer you are using. A number of Cloud Providers (e.g. Azure, GCP, AWS) provide ways to interact with the load balancer using their command-line tools.

I’ll outline the steps that you can use to switch to your active environment with an AWS Application Load Balancer, but the general concepts will more than likely be transferrable to other load balancer types.

Target Groups

In AWS, to route traffic to specific EC2 instances, you create target groups.

Here you can see that I have two target groups:

  • a octopus-samples-blue and
  • a octopus-samples-green group

Currently, the load balancer is forwarding traffic to instances in the octopus-samples-green target group.

Switching target groups with Runbooks

Operations Runbooks were introduced in Octopus in 2019.11. They are suited perfectly to this type of operations task, where you want to manage switching between the Blue and Green environments without giving users direct access to your infrastructure.

To match my Blue and Green deployment pattern, I have configured two environments in Octopus called Production - Blue and Production - Green:


To modify the Application Load Balancer to direct traffic to your active environment (defined by your target group) you can define an AWS CLI Script step in Octopus.

I create the AWS CLI script step and add the following PowerShell:

$listenerArn = $OctopusParameters["Project.AWS.ALB.listenerArn"]
$targetGroup = $OctopusParameters["Project.AWS.ALB.TargetArn"]

Write-Host "Modifying AWS ELB listener: $listenerArn to forward to targetGroup: $targetGroup"
aws elbv2 modify-listener --listener-arn $listenerArn --default-actions Type=forward,TargetGroupArn=$targetGroup

The script modifies the listener (specified by the variable Project.AWS.ALB.listenerArn) on the load balancer to switch to the target group specified in the other Octopus variable Project.AWS.ALB.TargetArn.

The variables mentioned in the script above are scoped to the appropriate Octopus Environment (described earlier) which reflects Blue and Green.

To further control who can run the Runbook, you could optionally add a Manual approval step to only allow users in a specific Octopus team to be able to approve the change of environment on the load balancer.

Sprinkle some Slack notifications in as well, and the entire runbook process ends up looking like this:

Next, I choose my Production - Blue environment and hit RUN:

The runbook executes in the Production - Blue environment:


And then check my AWS target group, and can see traffic is now routed to the octopus-samples-blue target group:

Having the step run as a runbook allows you to switch traffic to a different target group after you have tested your new deployment to production, but it could also be useful when you are doing an Adhoc operation for example server maintenance.

However, if you wanted to switch the load-balancer to one of the target groups when you are deploying your application, then there is nothing stopping you from adding this type of step in your deployment process too! :slightly_smiling_face:.

:white_check_mark: Octopus Samples instance

Check out our Samples instance for more examples of how to implement the Blue Green deployment pattern with Octopus!