-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2
executable file
·64 lines (55 loc) · 1.78 KB
/
ec2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
set -eu
BUCKET=ec2-cache.binaris.com
DEFAULT_FZF_QUERY=${1:-${realm:-$USER}}
# Requirements:
# - fzf
# - aws cli
function get_cache() {
# Set you bucket name here
aws s3 cp --quiet s3://$BUCKET/instances.fzf >(cat)
}
function choose() {
cache="$(get_cache)"
chosen=$(echo -e "$cache\n%refresh ec2 cache" | fzf -q "$DEFAULT_FZF_QUERY")
}
while true; do
choose
if [[ -z "$chosen" ]]; then
exit 1
elif [[ "$chosen" == "%refresh ec2 cache" ]]; then
echo "Refreshing EC2 fuzzy cache..."
aws lambda invoke --region ${region:-eu-central-1} --function-name ec2-fzf-${region:-eu-central-1}-discover_instances /dev/null >/dev/null
else
break
fi
done
function lookup() {
chosen=$1
field=$2
echo $chosen | grep -o -E "$field:\\S+" | cut -d ':' -f2
}
export key=$(lookup "$chosen" key)
export pub_ip=$(lookup "$chosen" pub_ip)
export priv_ip=$(lookup "$chosen" priv_ip)
export realm=$(lookup "$chosen" realm)
if echo $chosen | grep -E "default_user:" >/dev/null; then
export user=$(lookup "$chosen" default_user)
else
export user=ubuntu
fi
BASTION="kz8s-${realm}-bastion"
if echo $chosen | grep -E "ssh:bastion" >/dev/null; then
export bastion=$(echo "$cache" | fzf -q "$BASTION" -e -1 --prompt="Select your bastion: ")
export bastion_pub_ip=$(lookup "$bastion" pub_ip)
echo "Using bastion ($bastion_pub_ip) to connect..."
if echo $bastion | grep -E "default_user:" >/dev/null; then
export DEFAULT_USER=$(lookup "$bastion" default_user)
fi
export ip=$priv_ip
export proxy_command="ssh -i ~/.ssh/$key.pem -W %h:%p $DEFAULT_USER@$bastion_pub_ip"
exec ssh -i ~/.ssh/$key.pem -o ProxyCommand="$proxy_command" $user@$ip
else
export ip=$pub_ip
exec ssh -i ~/.ssh/$key.pem $user@$ip
fi