Last active
August 7, 2024 10:38
-
-
Save huevos-y-bacon/e6516d4db7e69a375db612a9c8475866 to your computer and use it in GitHub Desktop.
AWS SSM - Session Manager connection by tag:key=Name or instance-id. ssmsm is a straight cli session, while ssmsmwin does port forwarding to use RDP via localhost.
This file contains 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
#!/usr/bin/env bash | |
# shellcheck disable=SC2086 | |
ssm_sm(){ | |
[[ -n $DEBUG ]] && set -x | |
ARG1="$1" | |
get_name(){ | |
INSTANCENAME=$(aws ec2 describe-tags \ | |
--filters \ | |
Name=resource-id,Values=${INSTANCEID} \ | |
Name=key,Values=Name \ | |
--query 'Tags[].Value' \ | |
--output text) | |
} | |
get_id(){ | |
INSTANCENAME=${ARG1} | |
INSTANCEID=$(aws ec2 describe-instances \ | |
--filters \ | |
Name=tag-value,Values=${INSTANCENAME} \ | |
Name=instance-state-name,Values=running \ | |
--query 'Reservations[].Instances[].InstanceId' \ | |
--out text) | |
} | |
usage(){ echo "no instance id or name specified in \$1"; exit 1; } | |
[[ -z ${ARG1} ]] && usage | |
if [[ ${ARG1} = i-* ]];then | |
INSTANCEID=${ARG1} | |
get_name | |
else | |
get_id | |
fi | |
if [[ -z ${INSTANCEID} ]];then | |
exit 1; | |
else | |
echo "Target: Instance ${INSTANCEID} (${INSTANCENAME})"; | |
fi | |
aws ssm start-session \ | |
--target ${INSTANCEID} | |
# shellcheck disable=SC2181 | |
while [[ $? -ne 0 ]]; do | |
sleep 1; clear; | |
echo -n "Trying..."; | |
aws ssm start-session \ | |
--target ${INSTANCEID} 2> /dev/null; done | |
echo "Target: Instance ${INSTANCEID} (${INSTANCENAME})" | |
} | |
ssm_sm $* |
This file contains 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
#!/usr/bin/env bash | |
# shellcheck disable=SC2086 | |
ssm_sm_win(){ | |
[[ -n $DEBUG ]] && set -x | |
ARG1="$1" | |
get_name(){ | |
INSTANCENAME=$(aws ec2 describe-tags \ | |
--filters \ | |
Name=resource-id,Values=${INSTANCEID} \ | |
Name=key,Values=Name \ | |
--query 'Tags[].Value' \ | |
--output text) | |
} | |
get_id(){ | |
INSTANCENAME=${ARG1} | |
INSTANCEID=$(aws ec2 describe-instances \ | |
--filters \ | |
Name=tag-value,Values=${INSTANCENAME} \ | |
Name=instance-state-name,Values=running \ | |
--query 'Reservations[].Instances[].InstanceId' \ | |
--out text) | |
} | |
usage(){ echo "no instance id or name specified in \$1"; exit 1; } | |
[[ -z ${ARG1} ]] && usage | |
if [[ ${ARG1} = i-* ]];then | |
INSTANCEID=${ARG1} | |
get_name | |
else | |
get_id | |
fi | |
[[ -z ${INSTANCEID} ]] && exit 1 || echo "Target: Instance ${INSTANCEID} (${INSTANCENAME})" | |
aws ssm start-session \ | |
--target ${INSTANCEID} \ | |
--document-name AWS-StartPortForwardingSession \ | |
--parameters '{"portNumber":["3389"],"localPortNumber":["3389"]}' | |
echo "Target: Instance ${INSTANCEID} (${INSTANCENAME})" | |
} | |
ssm_sm_win $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment