Skip to content

Instantly share code, notes, and snippets.

@civitaspo
Created March 27, 2019 02:33
Show Gist options
  • Save civitaspo/7cb7c3c763b5b5b71ace1e930560e741 to your computer and use it in GitHub Desktop.
Save civitaspo/7cb7c3c763b5b5b71ace1e930560e741 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ -n "$1" ]; then
cat <<EOS
[NAME] $(basename $0) -- A Tool To Start Session With AWS Sessios Manager
[SYNOPSIS]
$(basename $0)
[NOTE]
- 'session-manager-plugin' has to be installed (See. https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html).
EOS
exit 1
fi
set -eu
INSTANCE_ID="$(
aws ec2 describe-instances \
| jq -cr '.Reservations[].Instances[]
| select(.State.Name == "running")
| .InstanceId + " (" +
( [.Tags[] | .Key + ":" + .Value]
| sort
| join(", ")) + ")"' \
| peco \
| cut -d' ' -f1
)"
if [ -z "$INSTANCE_ID" ]; then
echo "$(date +'%Y-%m-%d %H:%M:%S %z') [ERROR] Unable to fetch any instance."
exit 1
fi
exec aws ssm start-session --target $INSTANCE_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment