Skip to content

Commit

Permalink
Added step templates to support AMI Blue/Green deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Jan 10, 2025
1 parent a3102d5 commit 54f19cc
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 0 deletions.
80 changes: 80 additions & 0 deletions step-templates/aws-find-blue-green-asg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"Id": "6b72995e-500c-4b4b-9121-88f3a988ec71",
"Name": "AWS - Find Blue-Green ASG",
"Description": "Return the name of the online and offline blue and green Auto Scaling Groups",
"ActionType": "Octopus.AwsRunScript",
"Version": 1,
"CommunityActionTemplateId": null,
"Packages": [],
"GitDependencies": [],
"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.Script.ScriptBody": "#!/bin/bash\n\nINACTIVECOLOR=${1:-'#{AWSBlueGreen.InactiveColor}'}\nGREENASG=${2:-'#{AWSBlueGreen.AWS.GreenASG}'}\nBLUEASG=${3:-'#{AWSBlueGreen.AWS.BlueASG}'}\n\nechoerror() { echo \"$@\" 1>&2; }\n\nif [[ -z \"${INACTIVECOLOR}\" ]]\nthen\n echoerror \"Please provide the color of the inactive Auto Scaling group (Green or Blue) as the first argument\"\n exit 1\nfi\n\nif [[ -z \"${GREENASG}\" ]]\nthen\n echoerror \"Please provide the name of the Green Auto Scaling group as the second argument\"\n exit 1\nfi\n\nif [[ -z \"${BLUEASG}\" ]]\nthen\n echoerror \"Please provide the name of the Blue Auto Scaling group as the third argument\"\n exit 1\nfi\n\nif [[ \"${INACTIVECOLOR}\" == \"Green\" ]]\nthen\n set_octopusvariable \"ActiveGroup\" \"${BLUEASG}\"\n set_octopusvariable \"InactiveGroup\" \"${GREENASG}\"\n echo \"Active group is Blue (${BLUEASG}), inactive group is Green (${GREENASG})\"\nelse\n set_octopusvariable \"ActiveGroup\" \"${GREENASG}\"\n set_octopusvariable \"InactiveGroup\" \"${BLUEASG}\"\n echo \"Active group is Green (${GREENASG}), inactive group is Blue (${BLUEASG})\"\nfi",
"Octopus.Action.Aws.Region": "#{AWSBlueGreen.AWS.Region}"
},
"Parameters": [
{
"Id": "f8522014-f1ba-4e4a-a06d-59ebdba6f276",
"Name": "AWSBlueGreen.InactiveColor",
"Label": "Inactive Color",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "8753e6ed-0ae6-4a4c-ae5b-155139037633",
"Name": "AWSBlueGreen.AWS.GreenASG",
"Label": "Green ASG Name",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "bf9187f5-62e9-4ad9-b53f-459466b84994",
"Name": "AWSBlueGreen.AWS.BlueASG",
"Label": "Blue ASG Name",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "eb02bbf2-e05a-4469-9359-c77d77d87dd2",
"Name": "AWSBlueGreen.AWS.Region",
"Label": "Region",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "99a74afc-ee67-4295-8281-3bb1c6e83d06",
"Name": "AWSBlueGreen.AWS.Account",
"Label": "Account",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "AmazonWebServicesAccount"
}
}
],
"StepPackageId": "Octopus.AwsRunScript",
"$Meta": {
"ExportedAt": "2025-01-10T03:42:14.665Z",
"OctopusVersion": "2025.1.5319",
"Type": "ActionTemplate"
},
"LastModifiedBy": "mcasperson",
"Category": "aws"
}
90 changes: 90 additions & 0 deletions step-templates/aws-find-blue-green-target-group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"Id": "2f5f8b7b-5deb-45a9-966b-bf52c6e7976c",
"Name": "AWS - Find Blue-Green Target Group",
"Description": "Find the online and offline target groups for a blue-green deployment",
"ActionType": "Octopus.AwsRunScript",
"Version": 1,
"CommunityActionTemplateId": null,
"Packages": [],
"GitDependencies": [],
"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\nLISTENER=${1:-'#{AWSBlueGreen.AWS.ListenerARN | Trim}'}\nRULE=${2:-'#{AWSBlueGreen.AWS.RuleArn | Trim}'}\nGREENTARGETGROUP=${3:-'#{AWSBlueGreen.AWS.GreenTargetGroup | Trim}'}\nBLUETARGETGROUP=${4:-'#{AWSBlueGreen.AWS.BlueTargetGroup | 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\n# Validate the arguments\n\nif [[ -z \"${LISTENER}\" ]]; then\n echoerror \"Please provide the ARN of the listener as the first argument\"\n exit 1\nfi\n\nif [[ -z \"${RULE}\" ]]; then\n echoerror \"Please provide the ARN of the listener rule as the second argument\"\n exit 1\nfi\n\nif [[ -z \"${GREENTARGETGROUP}\" ]]; then\n echoerror \"Please provide the ARN of the green target group as the third argument\"\n exit 1\nfi\n\nif [[ -z \"${BLUETARGETGROUP}\" ]]; then\n echoerror \"Please provide the ARN of the blue target group as the fourth argument\"\n exit 1\nfi\n\n# Get the JSON representation of the listener rules\n\nRULES=$(aws elbv2 describe-rules \\\n --listener-arn \"${LISTENER}\" \\\n --output json)\n\nwrite_verbose \"${RULES}\"\n\n# Find the weight assigned to each of the target groups.\n\nGREENWEIGHT=$(jq -r \".Rules[] | select(.RuleArn == \\\"${RULE}\\\") | .Actions[] | select(.Type == \\\"forward\\\") | .ForwardConfig | .TargetGroups[] | select(.TargetGroupArn == \\\"${GREENTARGETGROUP}\\\") | .Weight\" <<< \"${RULES}\")\nBLUEWEIGHT=$(jq -r \".Rules[] | select(.RuleArn == \\\"${RULE}\\\") | .Actions[] | select(.Type == \\\"forward\\\") | .ForwardConfig | .TargetGroups[] | select(.TargetGroupArn == \\\"${BLUETARGETGROUP}\\\") | .Weight\" <<< \"${RULES}\")\n\n# Validation that we found the green and blue target groups.\n\nif [[ -z \"${GREENWEIGHT}\" ]]; then\n echoerror \"Failed to find the target group ${GREENTARGETGROUP} in the listener rule ${RULE}\"\n echoerror \"Double check that the target group exists and has been associated with the load balancer\"\n exit 1\nfi\n\nif [[ -z \"${BLUEWEIGHT}\" ]]; then\n echoerror \"Failed to find the target group ${BLUETARGETGROUP} in the listener rule ${RULE}\"\n echoerror \"Double check that the target group exists and has been associated with the load balancer\"\n exit 1\nfi\n\necho \"Green weight: ${GREENWEIGHT}\"\necho \"Blue weight: ${BLUEWEIGHT}\"\n\n# Set the output variables identifying which target group is active and which is inactive.\n# Note that we assume the target groups are either active or inactive (i.e. all traffic and no traffic).\n# Load balancers support more complex routing rules, but we assume a simple blue-green deployment.\n# If the green target group has traffic, it is considered active, and the blue target group is considered inactive.\n# If the green target group has no traffic, it is considered inactive, and the blue target group is considered active.\n\nif [ \"${GREENWEIGHT}\" != \"0\" ]; then\n echo \"Green target group is active, blue target group is inactive\"\n set_octopusvariable \"ActiveGroupArn\" \"${GREENTARGETGROUP}\"\n set_octopusvariable \"ActiveGroupColor\" \"Green\"\n set_octopusvariable \"InactiveGroupArn\" \"${BLUETARGETGROUP}\"\n set_octopusvariable \"InactiveGroupColor\" \"Blue\"\nelse\n echo \"Blue target group is active, green target group is inactive\"\n set_octopusvariable \"ActiveGroupArn\" \"${BLUETARGETGROUP}\"\n set_octopusvariable \"ActiveGroupColor\" \"Blue\"\n set_octopusvariable \"InactiveGroupArn\" \"${GREENTARGETGROUP}\"\n set_octopusvariable \"InactiveGroupColor\" \"Green\"\nfi"
},
"Parameters": [
{
"Id": "29cdfb7d-47fa-4c8a-837b-c58bb0d90c26",
"Name": "AWSBlueGreen.AWS.Region",
"Label": "Region",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "58a1ebcd-fd13-48e2-b8b9-fdfe4df8c35e",
"Name": "AWSBlueGreen.AWS.Account",
"Label": "Account",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "AmazonWebServicesAccount"
}
},
{
"Id": "80642a7b-ef3e-4db4-b969-d0148a1baa90",
"Name": "AWSBlueGreen.AWS.ListenerARN",
"Label": "Listener ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "2cf29ab4-61a1-4a80-942f-6f1dd035f634",
"Name": "AWSBlueGreen.AWS.BlueTargetGroup",
"Label": "Blue Target Group ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "b7d105c6-1640-48c4-9f01-ad9ece8d3588",
"Name": "AWSBlueGreen.AWS.GreenTargetGroup",
"Label": "Green Target Group ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "ff055e9f-f223-453a-9a1f-a0238e6cdfd6",
"Name": "AWSBlueGreen.AWS.RuleArn",
"Label": "Rule ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"StepPackageId": "Octopus.AwsRunScript",
"$Meta": {
"ExportedAt": "2025-01-10T03:41:11.780Z",
"OctopusVersion": "2025.1.5319",
"Type": "ActionTemplate"
},
"LastModifiedBy": "mcasperson",
"Category": "aws"
}
81 changes: 81 additions & 0 deletions step-templates/aws-set-blue-green-target-group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"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",
"ActionType": "Octopus.AwsRunScript",
"Version": 1,
"CommunityActionTemplateId": null,
"Packages": [],
"GitDependencies": [],
"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"
},
"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": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "2574f380-57b0-437a-8ca3-7a8af9dd1b8b",
"Name": "AWSBlueGreen.AWS.RuleArn",
"Label": "Rule ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "0483453c-564b-40f8-b80a-b28ba7a26504",
"Name": "AWSBlueGreen.AWS.OfflineTargetGroup",
"Label": "Offline Target Group ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "657a1c06-d0b8-4800-b473-d90fa2a63d9e",
"Name": "AWSBlueGreen.AWS.OnlineTargetGroup",
"Label": "Online Target Group ARN",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"StepPackageId": "Octopus.AwsRunScript",
"$Meta": {
"ExportedAt": "2025-01-10T03:44:19.550Z",
"OctopusVersion": "2025.1.5319",
"Type": "ActionTemplate"
},
"LastModifiedBy": "mcasperson",
"Category": "aws"
}
80 changes: 80 additions & 0 deletions step-templates/aws-update-launch-template-ami.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"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.",
"ActionType": "Octopus.AwsRunScript",
"Version": 1,
"CommunityActionTemplateId": null,
"Packages": [],
"GitDependencies": [],
"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"
},
"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": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "9885f4d6-b8a4-4445-973b-0b6b809e8bd0",
"Name": "AWSBlueGreen.AWS.ASG",
"Label": "ASG Name",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "b89df47e-3d7c-4d88-b3f6-a5b465448978",
"Name": "AWSBlueGreen.AWS.AMI",
"Label": "AMI",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "7f7a98b1-067d-47d0-94c8-1a8a3d285c1c",
"Name": "AWSBlueGreen.AWS.LaunchTemplateDescription",
"Label": "Launch Template Version Description",
"HelpText": null,
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"StepPackageId": "Octopus.AwsRunScript",
"$Meta": {
"ExportedAt": "2025-01-10T03:43:20.697Z",
"OctopusVersion": "2025.1.5319",
"Type": "ActionTemplate"
},
"LastModifiedBy": "mcasperson",
"Category": "aws"
}

0 comments on commit 54f19cc

Please sign in to comment.