Skip to content

Instantly share code, notes, and snippets.

@danilogco
Last active March 29, 2026 18:04
Show Gist options
  • Select an option

  • Save danilogco/022b8fa64dbb3b1c923e433d2c60ede1 to your computer and use it in GitHub Desktop.

Select an option

Save danilogco/022b8fa64dbb3b1c923e433d2c60ede1 to your computer and use it in GitHub Desktop.
Check and update all AWS clients / aws cli, cdk, kubectl, sam
#!/usr/bin/env bash
set -Eeuo pipefail
readonly TMP_DIR="$(mktemp -d)"
readonly ARCH="$(uname -m)"
readonly TARGET="${1:-all}"
AWS_ARCH=""
K8S_ARCH=""
SAM_ARCH=""
cleanup() {
rm -rf "$TMP_DIR"
}
on_error() {
local exit_code=$?
echo
echo "Erro na linha ${1:-unknown}. Código de saída: $exit_code" >&2
exit "$exit_code"
}
log_section() {
echo
echo "########################################"
echo "# $1"
echo "########################################"
}
usage() {
cat <<'EOF'
Uso:
aws_update [componente]
Componentes disponíveis:
all Atualiza todos os componentes (padrão)
aws Atualiza AWS CLI
kubectl Atualiza kubectl
sam Atualiza SAM CLI
cdk Atualiza AWS CDK
help Exibe esta ajuda
Exemplos:
aws_update
aws_update aws
aws_update kubectl
aws_update sam
aws_update cdk
aws_update help
EOF
}
validate_target() {
case "$TARGET" in
all|aws|kubectl|sam|cdk|help|--help|-h)
;;
*)
echo "Parâmetro inválido: $TARGET" >&2
echo
usage
exit 1
;;
esac
}
should_update() {
local component="$1"
[[ "$TARGET" == "all" || "$TARGET" == "$component" ]]
}
require_commands() {
local cmd
for cmd in "$@"; do
command -v "$cmd" >/dev/null 2>&1 || {
echo "$cmd not found. Install it before continuing." >&2
exit 1
}
done
}
set_architectures() {
case "$ARCH" in
x86_64|amd64)
AWS_ARCH="x86_64"
K8S_ARCH="amd64"
SAM_ARCH="x86_64"
;;
aarch64|arm64)
AWS_ARCH="aarch64"
K8S_ARCH="arm64"
SAM_ARCH="arm64"
;;
*)
echo "Unsupported architecture: $ARCH" >&2
exit 1
;;
esac
}
get_version_or_none() {
local cmd="$1"
local extractor="$2"
if command -v "$cmd" >/dev/null 2>&1; then
eval "$extractor" 2>/dev/null || echo "unknown"
else
echo "none"
fi
}
install_aws() {
echo "Updating AWS CLI..."
require_commands curl unzip sudo
local zip_file="$TMP_DIR/awscliv2.zip"
curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-${AWS_ARCH}.zip" -o "$zip_file"
unzip -q "$zip_file" -d "$TMP_DIR"
sudo "$TMP_DIR/aws/install" \
--bin-dir /usr/local/bin \
--install-dir /usr/local/aws-cli \
--update
echo "AWS CLI updated: $(aws --version 2>&1)"
}
install_kubectl() {
local stable_version="$1"
echo "Updating kubectl..."
require_commands curl sudo
local bin_file="$TMP_DIR/kubectl"
curl -fsSL "https://dl.k8s.io/release/${stable_version}/bin/linux/${K8S_ARCH}/kubectl" -o "$bin_file"
chmod +x "$bin_file"
sudo install -m 0755 "$bin_file" /usr/local/bin/kubectl
echo "kubectl updated: $(kubectl version --client 2>/dev/null | head -n 1)"
}
install_sam() {
echo "Updating SAM CLI..."
require_commands curl unzip sudo
local zip_file="$TMP_DIR/aws-sam-cli.zip"
local install_dir="$TMP_DIR/sam-install"
curl -fsSL \
"https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-${SAM_ARCH}.zip" \
-o "$zip_file"
unzip -q "$zip_file" -d "$install_dir"
sudo "$install_dir/install" --update
echo "SAM CLI updated: $(sam --version)"
}
install_cdk() {
echo "Updating AWS CDK..."
require_commands npm node
npm install -g aws-cdk
echo "AWS CDK updated: $(cdk --version)"
}
check_and_update() {
local name="$1"
local current="$2"
local latest="$3"
local installer="$4"
log_section "$name"
if [[ "$current" == "none" ]]; then
echo "$name not found."
else
echo "Installed version: $current"
fi
echo "Latest version: $latest"
if [[ "$latest" == "unknown" ]]; then
echo "Could not determine the latest version for $name. Skipping."
return
fi
if [[ "$current" == "$latest" ]]; then
echo "$name is already up to date."
else
"$installer"
fi
}
trap cleanup EXIT
trap 'on_error $LINENO' ERR
validate_target
if [[ "$TARGET" == "help" || "$TARGET" == "--help" || "$TARGET" == "-h" ]]; then
usage
exit 0
fi
require_commands awk sed jq curl
set_architectures
if should_update aws; then
AWS_CURRENT="$(
get_version_or_none aws \
"aws --version 2>&1 | awk -F/ '{print \$2}' | awk '{print \$1}'"
)"
AWS_LATEST="$(
curl -fsSL https://api.github.com/repos/aws/aws-cli/tags \
| jq -r '.[0].name' \
| sed 's/^v//'
)"
check_and_update "AWS CLI" "$AWS_CURRENT" "$AWS_LATEST" install_aws
fi
if should_update kubectl; then
KUBECTL_CURRENT="$(
get_version_or_none kubectl \
"kubectl version --client -o json | jq -r '.clientVersion.gitVersion' | sed 's/^v//'"
)"
KUBECTL_LATEST="$(
curl -fsSL https://dl.k8s.io/release/stable.txt | sed 's/^v//'
)"
check_and_update "Kubectl CLI" "$KUBECTL_CURRENT" "$KUBECTL_LATEST" "install_kubectl v${KUBECTL_LATEST}"
fi
if should_update sam; then
SAM_CURRENT="$(
get_version_or_none sam \
"sam --version | awk '{print \$4}'"
)"
SAM_LATEST="$(
curl -fsSL https://api.github.com/repos/aws/aws-sam-cli/releases/latest \
| jq -r '.tag_name' \
| sed 's/^v//'
)"
check_and_update "SAM CLI" "$SAM_CURRENT" "$SAM_LATEST" install_sam
fi
if should_update cdk; then
CDK_CURRENT="$(
get_version_or_none cdk \
"cdk --version | awk '{print \$1}'"
)"
CDK_LATEST="$(npm view aws-cdk version 2>/dev/null || echo "unknown")"
check_and_update "AWS CDK" "$CDK_CURRENT" "$CDK_LATEST" install_cdk
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment