AWS - Initiate Instance Refresh

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

Initiates an instance refresh for an Auto Scaling group and waits for it to complete.

Parameters

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

AWS Region

AWSBlueGreen.AWS.Region =

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

Account

AWSBlueGreen.AWS.Account =

ASG Name

AWSBlueGreen.AWS.ASG =

The name of the auto scaler group to refresh. See https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html for more details.

Script body

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

#!/bin/bash

ASG=${1:-'#{AWSBlueGreen.AWS.ASG | Trim}'}

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

if ! command -v "aws" &> /dev/null; then
  echoerror "You must have the AWS CLI installed for this step. Consider using a Container Image - https://octopus.com/docs/projects/steps/execution-containers-for-workers#how-to-use-execution-containers-for-workers"
  exit 1
fi

if ! command -v "jq" &> /dev/null; then
  echoerror "You must have jq installed for this step. Consider using a Container Image - https://octopus.com/docs/projects/steps/execution-containers-for-workers#how-to-use-execution-containers-for-workers"
  exit 1
fi

if [[ -z "${ASG}" ]]; then
  echoerror "Please provide the name of the Auto Scaling group as the first argument"
  exit 1
fi

for i in {1..30}; do
  EXISTINGREFRESHES=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name "${ASG}")
  NOTSUCCESSFUL=$(jq '.InstanceRefreshes[] | select(.Status == "Pending" or .Status == "InProgress" or .Status == "Cancelling" or .Status == "RollbackInProgress" or .Status == "Baking")' <<< "${EXISTINGREFRESHES}")
  if [[ -z "${NOTSUCCESSFUL}" ]];
  then
    break
  fi
    echo "Waiting for existing Auto Scaling group ${ASG} refresh to complete..."
    sleep 12
done

REFRESH=$(aws autoscaling start-instance-refresh --auto-scaling-group-name "${ASG}")

if [[ $? -ne 0 ]];
then
  echoerror "Failed to start instance refresh for Auto Scaling group ${ASG}"
  exit 1
fi

REFRESHTOKEN=$(jq -r '.InstanceRefreshId' <<< "${REFRESH}")

echo "Refreshing instances in Auto Scaling group ${ASG}..."

write_verbose "${REFRESH}"

# Wait for all instances to be healthy
for i in {1..30}; do
  REFRESHSTATUS=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name "${ASG}" --instance-refresh-ids "${REFRESHTOKEN}")
  STATUS=$(jq -r '.InstanceRefreshes[0].Status' <<< "${REFRESHSTATUS}")
  PERCENTCOMPLETE=$(jq -r '.InstanceRefreshes[0].PercentageComplete' <<< "${REFRESHSTATUS}")

  # Treat a null percentage as 0
  if [[ "${PERCENTCOMPLETE}" == "null" ]]
  then
    PERCENTCOMPLETE=0
  fi

  write_verbose "${REFRESHSTATUS}"

  if [[ "${STATUS}" == "Successful" ]]
  then
    echo "Instance refresh succeeded"
    break
  elif [[ "${STATUS}" == "Failed" ]];
  then
    echo "Instance refresh failed!"
    exit 1
  fi
  echo "Waiting for Auto Scaling group ${ASG} refresh to complete (${STATUS} ${PERCENTCOMPLETE}%)..."
  sleep 12
done

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": "150c46d1-f33f-493b-a8c6-f5bd22f540f3",
  "Name": "AWS - Initiate Instance Refresh",
  "Description": "Initiates an instance refresh for an Auto Scaling group and waits for it to complete.",
  "Version": 1,
  "ExportedAt": "2025-01-10T04:12:22.681Z",
  "ActionType": "Octopus.AwsRunScript",
  "Author": "mcasperson",
  "Packages": [],
  "Parameters": [
    {
      "Id": "2fe001f5-39ee-40d9-b104-24817759ac6f",
      "Name": "AWSBlueGreen.AWS.Region",
      "Label": "AWS 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": "f3630bb5-ab07-46b4-b764-19f1c3b2ec5f",
      "Name": "AWSBlueGreen.AWS.Account",
      "Label": "Account",
      "HelpText": null,
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AmazonWebServicesAccount"
      }
    },
    {
      "Id": "bdfdedee-fdeb-4292-96fd-e41c64b1e523",
      "Name": "AWSBlueGreen.AWS.ASG",
      "Label": "ASG Name",
      "HelpText": "The name of the auto scaler group to refresh. See https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html for more details.",
      "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.RunOnServer": "true",
    "Octopus.Action.AwsAccount.Variable": "#{AWSBlueGreen.AWS.Account}",
    "Octopus.Action.Aws.Region": "#{AWSBlueGreen.AWS.Region}",
    "Octopus.Action.Script.ScriptBody": "#!/bin/bash\n\nASG=${1:-'#{AWSBlueGreen.AWS.ASG | 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. Consider using a Container Image - https://octopus.com/docs/projects/steps/execution-containers-for-workers#how-to-use-execution-containers-for-workers\"\n  exit 1\nfi\n\nif ! command -v \"jq\" &> /dev/null; then\n  echoerror \"You must have jq installed for this step. Consider using a Container Image - https://octopus.com/docs/projects/steps/execution-containers-for-workers#how-to-use-execution-containers-for-workers\"\n  exit 1\nfi\n\nif [[ -z \"${ASG}\" ]]; then\n  echoerror \"Please provide the name of the Auto Scaling group as the first argument\"\n  exit 1\nfi\n\nfor i in {1..30}; do\n  EXISTINGREFRESHES=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name \"${ASG}\")\n  NOTSUCCESSFUL=$(jq '.InstanceRefreshes[] | select(.Status == \"Pending\" or .Status == \"InProgress\" or .Status == \"Cancelling\" or .Status == \"RollbackInProgress\" or .Status == \"Baking\")' <<< \"${EXISTINGREFRESHES}\")\n  if [[ -z \"${NOTSUCCESSFUL}\" ]];\n  then\n    break\n  fi\n    echo \"Waiting for existing Auto Scaling group ${ASG} refresh to complete...\"\n    sleep 12\ndone\n\nREFRESH=$(aws autoscaling start-instance-refresh --auto-scaling-group-name \"${ASG}\")\n\nif [[ $? -ne 0 ]];\nthen\n  echoerror \"Failed to start instance refresh for Auto Scaling group ${ASG}\"\n  exit 1\nfi\n\nREFRESHTOKEN=$(jq -r '.InstanceRefreshId' <<< \"${REFRESH}\")\n\necho \"Refreshing instances in Auto Scaling group ${ASG}...\"\n\nwrite_verbose \"${REFRESH}\"\n\n# Wait for all instances to be healthy\nfor i in {1..30}; do\n  REFRESHSTATUS=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name \"${ASG}\" --instance-refresh-ids \"${REFRESHTOKEN}\")\n  STATUS=$(jq -r '.InstanceRefreshes[0].Status' <<< \"${REFRESHSTATUS}\")\n  PERCENTCOMPLETE=$(jq -r '.InstanceRefreshes[0].PercentageComplete' <<< \"${REFRESHSTATUS}\")\n\n  # Treat a null percentage as 0\n  if [[ \"${PERCENTCOMPLETE}\" == \"null\" ]]\n  then\n    PERCENTCOMPLETE=0\n  fi\n\n  write_verbose \"${REFRESHSTATUS}\"\n\n  if [[ \"${STATUS}\" == \"Successful\" ]]\n  then\n    echo \"Instance refresh succeeded\"\n    break\n  elif [[ \"${STATUS}\" == \"Failed\" ]];\n  then\n    echo \"Instance refresh failed!\"\n    exit 1\n  fi\n  echo \"Waiting for Auto Scaling group ${ASG} refresh to complete (${STATUS} ${PERCENTCOMPLETE}%)...\"\n  sleep 12\ndone"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-initiate-instance-refresh.json",
  "Website": "/step-templates/150c46d1-f33f-493b-a8c6-f5bd22f540f3",
  "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pfe+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