Skip to content

Instantly share code, notes, and snippets.

@peter-mcconnell
Last active October 6, 2021 11:39
Show Gist options
  • Save peter-mcconnell/3721044a0a238b50e7d893a6b2f81c2c to your computer and use it in GitHub Desktop.
Save peter-mcconnell/3721044a0a238b50e7d893a6b2f81c2c to your computer and use it in GitHub Desktop.
ec2_ssm_connect.sh
#!/usr/bin/env bash
###############################################################################
## connect to an instance via SSM
###############################################################################
if [ ! -f ~/.aws/credentials ]; then
echo "this script scrapes profiles set up in ~/.aws/credentials"
echo "please set that file up"
exit 1
fi
read -p "instance: " instance
if ! command -v fzf > /dev/null 2>&1; then
echo ""
echo "note: you may want to install fzf if you want to select from a list"
echo ""
read -p "profile [default]: " profile
read -p "region [us-east-1]: " region
else
profile="$(cat ~/.aws/credentials | grep -o -e '\[.*' | tr -d '[' | tr -d ']' | \
fzf --prompt="profile: ")"
region="$(echo "us-east-1
eu-west-1
us-west-1" | \
fzf --prompt="region: ")"
fi
if [[ "$profile" == "" ]]; then
profile="default"
fi
if [[ "$region" == "" ]]; then
region="us-east-1"
fi
aws ssm start-session \
--target="$instance" \
--region="$region" \
--profile="$profile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment