AWS - Set Blue-Green Target Group

Octopus.AwsRunScript exported 2025-01-10 by mcasperson belongs to ‘AWS’ category.

Sets 100% of traffic to the online target group, and 0% to the offline target group

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Account

AWSBlueGreen.AWS.Account =

Region

AWSBlueGreen.AWS.Region =

The AWS region. See https://aws.amazon.com/about-aws/global-infrastructure/regions_az/ for more information.

Rule ARN

AWSBlueGreen.AWS.RuleArn =

The ARN of the listener rule to update. See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-update-rules.html for more information.

Offline Target Group ARN

AWSBlueGreen.AWS.OfflineTargetGroup =

The ARN of the target group that should receive 0% of the traffic. See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html for more information.

Online Target Group ARN

AWSBlueGreen.AWS.OnlineTargetGroup =

The ARN of the target group that should receive 100% of the traffic. See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html for more information.

Script body

Steps based on this template will execute the following Bash script.

#!/bin/bash

RULE=${1:-'#{AWSBlueGreen.AWS.RuleArn | Trim}'}
OFFLINEGROUP=${2:-'#{AWSBlueGreen.AWS.OfflineTargetGroup | Trim}'}
ONLINEGROUP=${3:-'#{AWSBlueGreen.AWS.OnlineTargetGroup | Trim}'}

echoerror() { echo "$@" 1>&2; }

if ! command -v "aws" &> /dev/null; then
  echoerror "You must have the AWS CLI installed for this step."
  exit 1
fi

if [[ -z "${RULE}" ]]; then
  echoerror "Please provide the ARN of the listener rule as the first argument"
  exit 1
fi

if [[ -z "${OFFLINEGROUP}" ]]; then
  echoerror "Please provide the ARN of the offline target group as the second argument"
  exit 1
fi

if [[ -z "${ONLINEGROUP}" ]]; then
  echoerror "Please provide the ARN of the online target group as the third argument"
  exit 1
fi

# https://stackoverflow.com/questions/61074411/modify-aws-alb-traffic-distribution-using-aws-cli
MODIFYRULE=$(aws elbv2 modify-rule \
  --rule-arn "${RULE}" \
  --actions \
    "[{
        \"Type\": \"forward\",
        \"Order\": 1,
        \"ForwardConfig\": {
          \"TargetGroups\": [
              {\"TargetGroupArn\": \"${OFFLINEGROUP}\", \"Weight\": 0 },
              {\"TargetGroupArn\": \"${ONLINEGROUP}\", \"Weight\": 100 }
          ]
        }
     }]")

echo "Updated listener rules for ${RULE} to set weight to 0 for ${OFFLINEGROUP} and 100 for ${ONLINEGROUP}."

write_verbose "${MODIFYRULE}"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "4b5f56c1-61f9-4d85-88f8-14dbe8cf8122",
  "Name": "AWS - Set Blue-Green Target Group",
  "Description": "Sets 100% of traffic to the online target group, and 0% to the offline target group",
  "Version": 1,
  "ExportedAt": "2025-01-10T03:44:19.550Z",
  "ActionType": "Octopus.AwsRunScript",
  "Author": "mcasperson",
  "Packages": [],
  "Parameters": [
    {
      "Id": "0835a276-6fae-45e0-863d-021e1ccd4937",
      "Name": "AWSBlueGreen.AWS.Account",
      "Label": "Account",
      "HelpText": null,
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AmazonWebServicesAccount"
      }
    },
    {
      "Id": "0dd99dd4-6676-44e8-b356-c0846f9f40e2",
      "Name": "AWSBlueGreen.AWS.Region",
      "Label": "Region",
      "HelpText": "The AWS region. See https://aws.amazon.com/about-aws/global-infrastructure/regions_az/ for more information.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "2574f380-57b0-437a-8ca3-7a8af9dd1b8b",
      "Name": "AWSBlueGreen.AWS.RuleArn",
      "Label": "Rule ARN",
      "HelpText": "The ARN of the listener rule to update. See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-update-rules.html for more information.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "0483453c-564b-40f8-b80a-b28ba7a26504",
      "Name": "AWSBlueGreen.AWS.OfflineTargetGroup",
      "Label": "Offline Target Group ARN",
      "HelpText": "The ARN of the target group that should receive 0% of the traffic. See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html for more information.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "657a1c06-d0b8-4800-b473-d90fa2a63d9e",
      "Name": "AWSBlueGreen.AWS.OnlineTargetGroup",
      "Label": "Online Target Group ARN",
      "HelpText": "The ARN of the target group that should receive 100% of the traffic. See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html for more information.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "OctopusUseBundledTooling": "False",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "Bash",
    "Octopus.Action.Aws.AssumeRole": "False",
    "Octopus.Action.AwsAccount.UseInstanceRole": "False",
    "Octopus.Action.AwsAccount.Variable": "#{AWSBlueGreen.AWS.Account}",
    "Octopus.Action.Aws.Region": "#{AWSBlueGreen.AWS.Region}",
    "Octopus.Action.Script.ScriptBody": "#!/bin/bash\n\nRULE=${1:-'#{AWSBlueGreen.AWS.RuleArn | Trim}'}\nOFFLINEGROUP=${2:-'#{AWSBlueGreen.AWS.OfflineTargetGroup | Trim}'}\nONLINEGROUP=${3:-'#{AWSBlueGreen.AWS.OnlineTargetGroup | Trim}'}\n\nechoerror() { echo \"$@\" 1>&2; }\n\nif ! command -v \"aws\" &> /dev/null; then\n  echoerror \"You must have the AWS CLI installed for this step.\"\n  exit 1\nfi\n\nif [[ -z \"${RULE}\" ]]; then\n  echoerror \"Please provide the ARN of the listener rule as the first argument\"\n  exit 1\nfi\n\nif [[ -z \"${OFFLINEGROUP}\" ]]; then\n  echoerror \"Please provide the ARN of the offline target group as the second argument\"\n  exit 1\nfi\n\nif [[ -z \"${ONLINEGROUP}\" ]]; then\n  echoerror \"Please provide the ARN of the online target group as the third argument\"\n  exit 1\nfi\n\n# https://stackoverflow.com/questions/61074411/modify-aws-alb-traffic-distribution-using-aws-cli\nMODIFYRULE=$(aws elbv2 modify-rule \\\n  --rule-arn \"${RULE}\" \\\n  --actions \\\n    \"[{\n        \\\"Type\\\": \\\"forward\\\",\n        \\\"Order\\\": 1,\n        \\\"ForwardConfig\\\": {\n          \\\"TargetGroups\\\": [\n              {\\\"TargetGroupArn\\\": \\\"${OFFLINEGROUP}\\\", \\\"Weight\\\": 0 },\n              {\\\"TargetGroupArn\\\": \\\"${ONLINEGROUP}\\\", \\\"Weight\\\": 100 }\n          ]\n        }\n     }]\")\n\necho \"Updated listener rules for ${RULE} to set weight to 0 for ${OFFLINEGROUP} and 100 for ${ONLINEGROUP}.\"\n\nwrite_verbose \"${MODIFYRULE}\"",
    "Octopus.Action.RunOnServer": "true"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-set-blue-green-target-group.json",
  "Website": "/step-templates/4b5f56c1-61f9-4d85-88f8-14dbe8cf8122",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////9o0R/eLD/Nu0/erS95Qg+bhr95sv/vHh+r96/vjw+bFc/NSl+KI++82W+saI+KpNeDqM1wAAA41JREFUeNrsnG2XazAURiuo0Cr//9feliIvR3DvXJFZe3+a6XpW5+xWEpyY2w0AAAAAAAAAAAAAAAAAAADgf1J0bda/9N70q83a3enzUHWVjbR1sW0xp6sd6fPI72VmUt3zA+kymD6N5vnIBMrHsxHTjsUXOX0e+iVaTNU5Q0A/Q+k+4oAp+ixMbw6A4rGVVjGHR92ulNXWuTAlBNJN/FFyr5yy3qN9rawmF9IxR4hqX4U1WMplmGtruVBDuiuswbKkzaGhX+cfXsqbZlXXv0dsYR13nw9fLenGXD7f6U5Ony4yTpzyZLNMUcpMr0xNzfwdRRMR1/LP2cqMctNqKx1LZFydm2U022ueEtLL6HbHfmSRYRn4HDXaXyzU4XRkkZWK/+JlRBBBBBFEEEEEEUQQQQQRRBBB5B9uYJc7SyuLw+nI7R2ptKWJcywd18Utza0rnM4iN66M6qzS5E93Lf1zLaviUL/ISs/Nt6W00DEyuRgiP2Yxvrd15z/Y26ncG76jy1Ta5jEy/L0p/VMWy33woVm8UYN1Y9fqKrzfZ5iedtaV34+kNxHak2Wg2SSkY7djx/bQWkNP6nkE0lH3Lyx7D1aak1Z1erWJ+U130Vz0Sude7mZqv995nW7mZxJd27Sg5XQppuMdWY3xl1XXOge8MasWjZfund0KbvrkE9fK7OPNne+2U9YEWX3nemtSbvLv6LJ7gZ9X45yBl9ZxrZ9d3vjT8rz62tOsny7jXkpYPX9jQmvF8yF55TdaslGviZy1vAmfoTobsZztGNEv7qZZSr/6HRc/0yzlb3HiKhURRBBBBBFEEEEEEUQQQQQRRBD5XSLav38tllbVzeH02Ww/UWA+6XgsHdXFKc2vK5Quoz/duVRnlrb26crpizzXOVU3l2Zb5Pfe+d1OX8ViqW7qH9gt51K44bukr2XxrW54vMaoy7mxa/cgvPRVKcQG7uOCD58HLQLt3r17Iy6AqjYeDG7TUenWW+p9Ot/IOF/lwuHV1nk6o8M469PWXhtr+0BeX/x7Ue40W3xacfb2gXFxUZcX8TYB3Kyfp+GThsjKti2zgZuMiLshxW3gpiQyrn/DXhR/i1NqIte5pkUEEUQQQQQRRBBBBBFEEEEEEUR+g4jQUZBEqjqFO9mOiyeShoXvYoukZOG4GCLpWZgu83/vTNRidhlE0rYAAAAAAAAAAAAAAAAAAACAZPkjwAAMDi+bsnPP/wAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, January 10, 2025