-
Notifications
You must be signed in to change notification settings - Fork 44
/
get-metadata
executable file
·102 lines (95 loc) · 3.59 KB
/
get-metadata
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Taken from https://gist.github.com/xuwang/c42a77e9f833d263d040. Replaced some grep with jq.
info=${1^^}
meta_data_url=http://instance-data/latest/meta-data/
roleProfile=$(curl -s http://instance-data/latest/meta-data/iam/info | jq -r '.InstanceProfileArn' | awk -F'/' '{print $NF}')
export AWS_DEFAULT_REGION=$(curl -s http://instance-data/latest/dynamic/instance-identity/document | jq --raw-output .region)
# auth values
get_sts_value() {
echo -n $(curl -s http://instance-data/latest/meta-data/iam/security-credentials/$roleProfile/ \
| grep "$1" \
| awk -F":" '{print $2}' \
| sed 's/^[ ^t]*//;s/"//g;s/,//g')
}
# exit 1 with an error message
abort(){
echo $1
exit 1
}
# get ASG name and instance public IPs
asg_public_ips(){
instance_id=$(curl -s http://instance-data/latest/meta-data/instance-id)
asg_name=$(aws autoscaling describe-auto-scaling-groups \
| jq -r ".[] | map(select(.Instances[].InstanceId | contains(\"$instance_id\"))) | .[].AutoScalingGroupName")
if [[ "$asg_name" ]]; then
asg_ips=$(aws ec2 describe-instances --instance-ids $(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$asg_name" \
| jq '.AutoScalingGroups[0].Instances[] | select(.LifecycleState == "InService") | .InstanceId' \
| xargs) \
| jq -r ".Reservations[].Instances[].PublicIpAddress")
result=$(echo $asg_name: $asg_ips)
else
abort "$instance_id doesn't belong to autoscaling group."
fi
}
asg_private_ips(){
instance_id=$(curl -s http://instance-data/latest/meta-data/instance-id)
asg_name=$(aws autoscaling describe-auto-scaling-groups \
| jq -r ".[] | map(select(.Instances[].InstanceId | contains(\"$instance_id\"))) | .[].AutoScalingGroupName")
if [[ "$asg_name" ]]; then
asg_ips=$(aws ec2 describe-instances --instance-ids $(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$asg_name" \
| jq '.AutoScalingGroups[0].Instances[] | select(.LifecycleState == "InService") | .InstanceId' \
| xargs) \
| jq -r ".Reservations[].Instances[].PrivateIpAddress")
result=$(echo $asg_name: $asg_ips)
else
abort "$instance_id doesn't belong to autoscaling group."
fi
}
case $info in
ASGIPS|ASGIPSPUB)
asg_public_ips
;;
ASGIPSPRV)
asg_private_ips
;;
ACCOUNT)
result=$(curl -s http://instance-data/latest/dynamic/instance-identity/document | jq .accountId | sed 's/"//g')
;;
HOSTNAME)
result=$(curl -s http://instance-data/latest/meta-data/public-hostname)
;;
ID|INSTANCEID)
result=$(curl -s http://instance-data/latest/meta-data/instance-id)
;;
PRIVATEIP)
result=$(curl -s http://instance-data/latest/meta-data/local-ipv4)
;;
PUBLICIP)
result=$(curl -s http://instance-data/latest/meta-data/public-ipv4)
;;
ROLE)
result=$roleProfile
;;
STSCRED)
result=$(curl -s http://instance-data/latest/meta-data/iam/security-credentials/$roleProfile)
;;
STSTOKEN)
result=$(get_sts_value "Token")
;;
STSKEY)
result=$(get_sts_value "AccessKeyId")
;;
S3SECRET)
result=$(get_sts_value "SecretAccessKey")
;;
ZONE)
result=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone/ | sed -e 's/.$//')
;;
*)
echo "Usage: $(basename $0) <argument>"
grep -Eo '([A-Z.]+\))' $0 | sed 's/)//'
;;
esac
if [ ! -z "$result" ]; then
echo "$result"
fi