Skip to content

Instantly share code, notes, and snippets.

@nukosuke
Last active August 30, 2024 20:18
Show Gist options
  • Save nukosuke/b4507f41f934e8897870934a46814de3 to your computer and use it in GitHub Desktop.
Save nukosuke/b4507f41f934e8897870934a46814de3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# 使いかた
# source ec2ssh.bash
# ec2ssh
#
# インストール方法
# echo "[ -f ~/ec2ssh.bash ] && source ~/ec2ssh.bash" >> ~/.bashrc
#
# 依存
# aws-cli - https://github.com/aws/aws-cli
# fzf - https://github.com/junegunn/fzf?tab=readme-ov-file#using-git
INSTANCE_FILE="$HOME/ec2_instances.txt"
function _fetch_ec2_instance_list() {
aws ec2 describe-instances \
--output text \
--query "Reservations[].Instances[].{InstanceId:InstanceId,KeyName:KeyName} | sort_by(@, &KeyName)" \
> $INSTANCE_FILE
}
function _ec2ssh() {
_fetch_ec2_instance_list
local instance_id=$(cat $INSTANCE_FILE \
| fzf -1 --layout reverse \
| cut -f1)
aws ssm start-session --target $instance_id
}
alias ec2ssh=_ec2ssh
@nukosuke
Copy link
Author

nukosuke commented Aug 29, 2024

インストール方法

$ curl -o ~/ec2ssh.bash https://gist.githubusercontent.com/nukosuke/b4507f41f934e8897870934a46814de3/raw/1bf9073347e57b81837934fd4ad5e12c55f9fc89/ec2ssh.bash
$ echo "[ -f ~/ec2ssh.bash ] && source ~/ec2ssh.bash" >> ~/.bashrc
$ source ~/.bashrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment