Last active
July 14, 2023 15:15
-
-
Save Nicolas-Richard/43dd8557160007304e3ad85b8b60d25c to your computer and use it in GitHub Desktop.
This script updates the Amazon EKS node groups associated with a given EKS cluster to use the latest version of the launch template.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ---------------------------------------------- | |
| # This script updates the Amazon EKS node groups | |
| # associated with a given EKS cluster to use the | |
| # latest version of the launch template. | |
| # | |
| # Usage: | |
| # ./update_node_group_launch_template.sh <AWS_PROFILE> <AWS_REGION> <CLUSTER_NAME> | |
| # | |
| # Parameters: | |
| # AWS_PROFILE - The AWS profile to use | |
| # AWS_REGION - The AWS region of the cluster | |
| # CLUSTER_NAME - The name of the EKS cluster | |
| # ---------------------------------------------- | |
| # Ensure that exactly 3 arguments are provided | |
| if [ "$#" -ne 3 ]; then | |
| echo "Usage: ./update_nodegroups.sh <AWS_PROFILE> <AWS_REGION> <CLUSTER_NAME>" | |
| exit 1 | |
| fi | |
| # Assign command line arguments to variables | |
| AWS_PROFILE=$1 | |
| AWS_REGION=$2 | |
| CLUSTER_NAME=$3 | |
| # Export AWS_PROFILE and AWS_REGION as environment variables | |
| export AWS_PROFILE | |
| export AWS_REGION | |
| # List the node groups associated with the specified EKS cluster | |
| NODEGROUPS=$(aws eks list-nodegroups --cluster-name "${CLUSTER_NAME}" --query 'nodegroups' --output text) | |
| # Iterate through each node group in the cluster | |
| for NODE_GROUP_NAME in ${NODEGROUPS}; do | |
| echo "----------------------------------------------------------" | |
| echo "Processing Node Group: ${NODE_GROUP_NAME}" | |
| # Get the launch template ID and version associated with the node group | |
| LAUNCH_TEMPLATE_ID=$(aws eks describe-nodegroup --cluster-name "${CLUSTER_NAME}" --nodegroup-name "${NODE_GROUP_NAME}" --query 'nodegroup.launchTemplate.id' --output text) | |
| LAUNCH_TEMPLATE_VERSION=$(aws eks describe-nodegroup --cluster-name "${CLUSTER_NAME}" --nodegroup-name "${NODE_GROUP_NAME}" --query 'nodegroup.launchTemplate.version' --output text) | |
| # Get the latest version number of the launch template | |
| LATEST_VERSION=$(aws ec2 describe-launch-templates --launch-template-ids ${LAUNCH_TEMPLATE_ID} --query 'LaunchTemplates[0].LatestVersionNumber' --output text) | |
| # Output launch template details | |
| echo "Launch Template ID: ${LAUNCH_TEMPLATE_ID}" | |
| echo "Node Group's Launch Template Version: ${LAUNCH_TEMPLATE_VERSION}" | |
| echo "Latest Launch Template Version: ${LATEST_VERSION}" | |
| # Compare the version numbers and decide whether to update the node group | |
| if [ "$LAUNCH_TEMPLATE_VERSION" == "$LATEST_VERSION" ]; then | |
| echo "The node group ${NODE_GROUP_NAME} is already using the latest launch template version (${LATEST_VERSION})." | |
| else | |
| echo "The node group ${NODE_GROUP_NAME} is using version ${LAUNCH_TEMPLATE_VERSION}, but a newer version (${LATEST_VERSION}) is available." | |
| echo "Initiating update for the node group..." | |
| # Update the node group to use the latest launch template version | |
| aws eks update-nodegroup-version --cluster-name "${CLUSTER_NAME}" --nodegroup-name "${NODE_GROUP_NAME}" --launch-template "{\"id\": \"${LAUNCH_TEMPLATE_ID}\", \"version\": \"${LATEST_VERSION}\"}" | |
| # Monitor the status of the node group update | |
| while true; do | |
| STATUS=$(aws eks describe-nodegroup --cluster-name "${CLUSTER_NAME}" --nodegroup-name "${NODE_GROUP_NAME}" --query 'nodegroup.status' --output text) | |
| echo "Current status: ${STATUS}" | |
| # Break the loop if the update is complete or has failed | |
| if [ "$STATUS" = "ACTIVE" ]; then | |
| echo "Node group update completed successfully." | |
| break | |
| elif [ "$STATUS" = "CREATE_FAILED" ] || [ "$STATUS" = "DELETE_FAILED" ]; then | |
| echo "Node group update failed." | |
| exit 1 | |
| else | |
| # Wait for 30 seconds before checking the status again | |
| sleep 30 | |
| fi | |
| done | |
| fi | |
| done | |
| unset AWS_PROFILE | |
| unset AWS_REGION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment