-
-
Save rymndhng/ed144ac3ea149b32be581257ae3ee6f9 to your computer and use it in GitHub Desktop.
ssm using pipes
This file contains hidden or 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
#!/bin/sh -x | |
# | |
# CLI Tool for accessing instances via ssm without having to pass in params | |
# | |
# Dependencies: awscli, zsh (Included by default on Catalina) | |
# | |
HELP=$(cat <<-EOF | |
Usage: | |
ssm i-073404e8ece4624b5 [OPTIONAL] --profile staging | |
EOF | |
) | |
INSTANCE_ID=$1 | |
if [ -z "$INSTANCE_ID" ]; then | |
echo "$HELP" | |
exit 0 | |
fi | |
PROFILE=${3:-default} | |
prompt_for_mfa_if_needed () { | |
# Make sure MFA is gathered if needed: | |
aws --region us-west-2 ec2 describe-instances --instance-id ${INSTANCE_ID} --profile ${PROFILE} &> /dev/null | |
} | |
find_region_of_instance() { | |
REGIONS=( | |
'us-west-1' | |
'us-west-2' | |
'us-east-1' | |
'eu-central-1' | |
'eu-west-1' | |
'eu-west-3' | |
'ap-southeast-1' | |
'ap-southeast-2' | |
) | |
for region in "${REGIONS[@]}"; do | |
(aws --region "${region}" ec2 describe-instances --instance-id ${INSTANCE_ID} --profile ${PROFILE} &> /dev/null) && echo "${region}" & | |
done | head -n 1 | |
} | |
prompt_for_mfa_if_needed | |
region=$(find_region_of_instance) | |
aws ssm start-session --target ${INSTANCE_ID} --region ${region} --profile ${PROFILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment