-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2-ip
executable file
·43 lines (34 loc) · 1021 Bytes
/
ec2-ip
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
#!/bin/bash
set -eu
# TODO: deduplicate code between ec2 and ec2-ip, allow running a command
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")
}
until
choose
[[ "$chosen" != "%refresh ec2 cache" ]];
do
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
done
if [[ -z "$chosen" ]]; then exit 1; fi
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)
echo $pub_ip