AWS - Update Launch Template AMI

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

Update the AMI used by a launch template, create a new launch template version, and set the new version as the default.

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.

ASG Name

AWSBlueGreen.AWS.ASG =

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

AMI

AWSBlueGreen.AWS.AMI =

The AMI image to configure in the launch template. See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html for more details.

Launch Template Version Description

AWSBlueGreen.AWS.LaunchTemplateDescription =

The description of the new launch template version.

Script body

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

#!/bin/bash

ASG=${1:-'#{AWSBlueGreen.AWS.ASG | Trim}'}
AMI=${2:-'#{AWSBlueGreen.AWS.AMI | Trim}'}
VERSIONDESCRIPTION=${3:-'#{AWSBlueGreen.AWS.LaunchTemplateDescription | 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

if [[ -z "${AMI}" ]]; then
  echoerror "Please provide the ID of the new AMI as the second argument"
  exit 1
fi

if [[ -z "${VERSIONDESCRIPTION}" ]]; then
  echoerror "Please provide a description for the new launch template version as the third argument"
  exit 1
fi

LAUNCHTEMPLATE=$(aws autoscaling describe-auto-scaling-groups \
  --auto-scaling-group-names "${ASG}" \
  --query 'AutoScalingGroups[0].LaunchTemplate.LaunchTemplateId' \
  --output text)

echo "Modifying launch template ${LAUNCHTEMPLATE} for Auto Scaling group ${ASG}..."

NEWVERSION=$(aws ec2 create-launch-template-version \
    --launch-template-id "${LAUNCHTEMPLATE}" \
    --version-description "${VERSIONDESCRIPTION}" \
    --source-version 1 \
    --launch-template-data "ImageId=${AMI}")

NEWVERSIONNUMBER=$(jq -r '.LaunchTemplateVersion.VersionNumber' <<< "${NEWVERSION}")

echo "Set AMI for launch template ${LAUNCHTEMPLATE} to ${AMI}, generating new version ${NEWVERSIONNUMBER}..."

write_verbose "${NEWVERSION}"

MODIFYTEMPLATE=$(aws ec2 modify-launch-template \
    --launch-template-id "${LAUNCHTEMPLATE}" \
    --default-version "${NEWVERSIONNUMBER}")

echo "Set default version for launch template ${LAUNCHTEMPLATE} to ${NEWVERSIONNUMBER}..."

write_verbose "${MODIFYTEMPLATE}"

UPDATELAUNCHTEMPLATEVERSION=$(aws autoscaling update-auto-scaling-group \
  --auto-scaling-group-name "${ASG}" \
  --launch-template "LaunchTemplateId=${LAUNCHTEMPLATE},Version=${NEWVERSIONNUMBER}")

echo "Updated the ASG launch template version to ${NEWVERSIONNUMBER}..."

write_verbose "${UPDATELAUNCHTEMPLATEVERSION}"


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": "143400df-19a9-42f5-a6c0-68145489482a",
  "Name": "AWS - Update Launch Template AMI",
  "Description": "Update the AMI used by a launch template, create a new launch template version, and set the new version as the default.",
  "Version": 1,
  "ExportedAt": "2025-01-10T03:43:20.697Z",
  "ActionType": "Octopus.AwsRunScript",
  "Author": "mcasperson",
  "Packages": [],
  "Parameters": [
    {
      "Id": "11e34c09-311a-49b4-82b7-c33212a07c01",
      "Name": "AWSBlueGreen.AWS.Account",
      "Label": "Account",
      "HelpText": null,
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AmazonWebServicesAccount"
      }
    },
    {
      "Id": "6a42aaa1-e5f5-4c03-bba7-068fa75eb53f",
      "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": "9885f4d6-b8a4-4445-973b-0b6b809e8bd0",
      "Name": "AWSBlueGreen.AWS.ASG",
      "Label": "ASG Name",
      "HelpText": "The name of the auto scaler group to update. See https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html for more details.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "b89df47e-3d7c-4d88-b3f6-a5b465448978",
      "Name": "AWSBlueGreen.AWS.AMI",
      "Label": "AMI",
      "HelpText": "The AMI image to configure in the launch template. See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html for more details.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "7f7a98b1-067d-47d0-94c8-1a8a3d285c1c",
      "Name": "AWSBlueGreen.AWS.LaunchTemplateDescription",
      "Label": "Launch Template Version Description",
      "HelpText": "The description of the new launch template version.",
      "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\nASG=${1:-'#{AWSBlueGreen.AWS.ASG | Trim}'}\nAMI=${2:-'#{AWSBlueGreen.AWS.AMI | Trim}'}\nVERSIONDESCRIPTION=${3:-'#{AWSBlueGreen.AWS.LaunchTemplateDescription | 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\nif [[ -z \"${AMI}\" ]]; then\n  echoerror \"Please provide the ID of the new AMI as the second argument\"\n  exit 1\nfi\n\nif [[ -z \"${VERSIONDESCRIPTION}\" ]]; then\n  echoerror \"Please provide a description for the new launch template version as the third argument\"\n  exit 1\nfi\n\nLAUNCHTEMPLATE=$(aws autoscaling describe-auto-scaling-groups \\\n  --auto-scaling-group-names \"${ASG}\" \\\n  --query 'AutoScalingGroups[0].LaunchTemplate.LaunchTemplateId' \\\n  --output text)\n\necho \"Modifying launch template ${LAUNCHTEMPLATE} for Auto Scaling group ${ASG}...\"\n\nNEWVERSION=$(aws ec2 create-launch-template-version \\\n    --launch-template-id \"${LAUNCHTEMPLATE}\" \\\n    --version-description \"${VERSIONDESCRIPTION}\" \\\n    --source-version 1 \\\n    --launch-template-data \"ImageId=${AMI}\")\n\nNEWVERSIONNUMBER=$(jq -r '.LaunchTemplateVersion.VersionNumber' <<< \"${NEWVERSION}\")\n\necho \"Set AMI for launch template ${LAUNCHTEMPLATE} to ${AMI}, generating new version ${NEWVERSIONNUMBER}...\"\n\nwrite_verbose \"${NEWVERSION}\"\n\nMODIFYTEMPLATE=$(aws ec2 modify-launch-template \\\n    --launch-template-id \"${LAUNCHTEMPLATE}\" \\\n    --default-version \"${NEWVERSIONNUMBER}\")\n\necho \"Set default version for launch template ${LAUNCHTEMPLATE} to ${NEWVERSIONNUMBER}...\"\n\nwrite_verbose \"${MODIFYTEMPLATE}\"\n\nUPDATELAUNCHTEMPLATEVERSION=$(aws autoscaling update-auto-scaling-group \\\n  --auto-scaling-group-name \"${ASG}\" \\\n  --launch-template \"LaunchTemplateId=${LAUNCHTEMPLATE},Version=${NEWVERSIONNUMBER}\")\n\necho \"Updated the ASG launch template version to ${NEWVERSIONNUMBER}...\"\n\nwrite_verbose \"${UPDATELAUNCHTEMPLATEVERSION}\"\n\n"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-update-launch-template-ami.json",
  "Website": "/step-templates/143400df-19a9-42f5-a6c0-68145489482a",
  "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