Skip to content

Instantly share code, notes, and snippets.

@maksim-paskal
Created February 23, 2021 14:24
Show Gist options
  • Save maksim-paskal/3b891ff61e4eeaa0390aa323f55ae234 to your computer and use it in GitHub Desktop.
Save maksim-paskal/3b891ff61e4eeaa0390aa323f55ae234 to your computer and use it in GitHub Desktop.
Simple bash script to change AMI in AWS EKS cluster

Simple bash script to change AMI in AWS EKS cluster

Sometime you need some not standart AMI in kubernetes cluster, for example you want to preload some docker images in kubernetes node

  1. Fork https://github.com/awslabs/amazon-eks-ami

  2. Make some changes in https://github.com/awslabs/amazon-eks-ami/blob/v20210208/scripts/install-worker.sh - for example

sudo systemctl start docker

# this will preload and extract mysql image on kubernetes node
# as the result, when node start - it's will speed up pod start in cluster ~ 40s
sudo docker pull mysql:8.0.23
  1. Update AMI (with cached docker layers) in EKS cluster, without recreating ASG
#!/bin/sh

version=^eksctl-
ami=<SOME-AMI>

echo "WARNING!! this will create new launch-template-version in eks cluster"
sleep 30s

set -ex

aws autoscaling describe-auto-scaling-groups --max-items 100 | jq -r '.AutoScalingGroups[] | "\(.AutoScalingGroupName) \(.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateName)"' | grep -i $version | while read line
do
  AutoScalingGroupName=`echo $line | awk '{print $1}'`
  LaunchTemplateName=`echo $line | awk '{print $2}'`
  template=`aws ec2 create-launch-template-version --launch-template-name "$LaunchTemplateName" --source-version 1 --launch-template-data "{\"ImageId\":\"$ami\"}"`
  VersionNumber=`echo $template | jq -r '.LaunchTemplateVersion.VersionNumber'`
  aws autoscaling update-auto-scaling-group \
  --auto-scaling-group-name $AutoScalingGroupName \
  --mixed-instances-policy "{\"LaunchTemplate\":{\"LaunchTemplateSpecification\":{\"LaunchTemplateName\":\"$LaunchTemplateName\",\"Version\":\"$VersionNumber\"}}}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment