Skip to content

Instantly share code, notes, and snippets.

@davivcgarcia
Last active April 7, 2022 20:44
Show Gist options
  • Save davivcgarcia/c7fff38bd3428c1029d5b1933cd5c58a to your computer and use it in GitHub Desktop.
Save davivcgarcia/c7fff38bd3428c1029d5b1933cd5c58a to your computer and use it in GitHub Desktop.
AWS CloudShell/Cloud9 Bootstrapper
#! /bin/bash
#
# About: Unified shell script to simplify AWS CloudShell/Cloud9 tooling bootstraping
# Author: Davi Garcia (davivcgarcia)
#
# Usage: $ source <(curl -sL https://bit.ly/aws-cloud-bootstrapper)
#
#
# Creates directory structure in the persistent storage mount
#
mkdir -p $HOME/{.local/bin,.config}
#
# Updates system packages (only for Cloud9)
#
if [[ "$AWS_EXECUTION_ENV" != "CloudShell" ]]
then
sudo yum -y update &> /dev/null
fi
#
# Installs required packages
#
sudo yum install -y bash-completion openssl vim git jq yq gettext moreutils perl-Digest-SHA &> /dev/null
#
# Adds workaround for "bash-completion" if on AWS CloudShell
#
if [[ "$AWS_EXECUTION_ENV" == "CloudShell" ]]
then
if ! grep -q "YUM Workaround" $HOME/.bashrc
then
echo -e "\n# YUM Workaround" >> $HOME/.bashrc
echo "sudo yum install -y bash-completion &> /dev/null" >> $HOME/.bashrc
fi
fi
#
# Loads Bash Completion helpers
#
. /etc/profile.d/bash_completion.sh
#
# Installs AWS CLI v2 (aws) if not on AWS CloudShell
#
if [[ "$AWS_EXECUTION_ENV" != "CloudShell" ]]
then
curl --silent --location "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
unzip -o /tmp/awscliv2.zip &> /dev/null
sudo ./aws/install --update &> /dev/null
rm -rf ./aws
fi
#
# Installs Amazon EKS CLI (eksctl)
#
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
mv /tmp/eksctl $HOME/.local/bin
chmod +x $HOME/.local/bin/eksctl
$HOME/.local/bin/eksctl completion bash >> $HOME/.config/bash_completion
. <($HOME/.local/bin/eksctl completion bash)
#
# Installs Kubernetes CLI (kubectl)
#
curl --silent --location "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o "/tmp/kubectl"
mv /tmp/kubectl $HOME/.local/bin
chmod +x $HOME/.local/bin/kubectl
$HOME/.local/bin/kubectl completion bash >> $HOME/.config/bash_completion
. <($HOME/.local/bin/kubectl completion bash)
#
# Installs Helm v3 CLI (helm)
#
curl -fsSL -o /tmp/get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
chmod 700 /tmp/get_helm.sh
HELM_INSTALL_DIR=$HOME/.local/bin/ /tmp/get_helm.sh &> /dev/null
$HOME/.local/bin/helm completion bash >> $HOME/.config/bash_completion
. <($HOME/.local/bin/helm completion bash)
rm -rf /tmp/get_helm.sh
#
# Install Krew plugin manager (kubectl-krew)
#
curl -fsSL https://github.com/kubernetes-sigs/krew/releases/latest/download/krew-linux_amd64.tar.gz | tar xzv -C /tmp
/tmp/krew-linux_amd64 install krew &> /dev/null
if ! grep -q "Kubernetes Krew Binary Path" $HOME/.bashrc
then
echo -e "\n# Kubernetes Krew Binary Path" >> $HOME/.bashrc
echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> $HOME/.bashrc
fi
#
# Install FluxCD v2 CLI (flux)
#
curl -sLo /tmp/flux-install.sh https://fluxcd.io/install.sh
chmod 700 /tmp/flux-install.sh
/tmp/flux-install.sh $HOME/.local/bin/ &> /dev/null
$HOME/.local/bin/flux completion bash >> $HOME/.config/bash_completion
. <($HOME/.local/bin/flux completion bash)
rm -rf /tmp/flux-install.sh
#
# Install AWS Copilot CLI (copilot)
#
curl -sLo /tmp/copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux
mv /tmp/copilot $HOME/.local/bin
chmod +x $HOME/.local/bin/copilot
$HOME/.local/bin/copilot completion bash >> $HOME/.config/bash_completion
. <($HOME/.local/bin/copilot completion bash)
#
# Define standard AWS envinronment variables (only if not on AWS CloudShell)
#
if ! grep -q "AWS Common Variables" $HOME/.bashrc && [ "$AWS_EXECUTION_ENV" != "CloudShell" ]
then
export AWS_ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
export AWS_REGION=$(curl -sl http://169.254.169.254/latest/meta-data/placement/availability-zone | head -c -1)
export AWS_AZS=$(aws ec2 describe-availability-zones --query 'AvailabilityZones[].ZoneName' --output text --region $AWS_REGION)
echo -e "\n# AWS Common Variables" >> $HOME/.bashrc
echo "export AWS_ACCOUNT=${ACCOUNT_ID}" >> $HOME/.bashrc
echo "export AWS_REGION=${AWS_REGION}" >> $HOME/.bashrc
echo "export AWS_AZS=(${AWS_AZS[@]})" >> $HOME/.bashrc
fi
#
# Configure AWS CLI
#
aws configure set default.region ${AWS_REGION}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment