-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprice_check
executable file
·118 lines (101 loc) · 2.81 KB
/
price_check
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# This translates region strings
# e.g. us-west-2
# into the regions that the GraphQL API expects
read -r -d '' region_lookup_map << EOF
us-gov-west-1,AWS GovCloud (US)
us-gov-east-1,AWS GovCloud (US)
us-east-2,US East (Ohio)
us-east-1,US East (N. Virginia)
us-west-1,US West (N. California)
us-west-2,US West (Oregon)
af-south-1,Africa (Cape Town)
ap-east-1,Asia Pacific (Hong Kong)
ap-south-2,Asia Pacific (Hyderabad)
ap-southeast-3,Asia Pacific (Jakarta)
ap-southeast-4,Asia Pacific (Melbourne)
ap-south-1,Asia Pacific (Mumbai)
ap-northeast-3,Asia Pacific (Osaka)
ap-northeast-2,Asia Pacific (Seoul)
ap-southeast-1,Asia Pacific (Singapore)
ap-southeast-2,Asia Pacific (Sydney)
ap-northeast-1,Asia Pacific (Tokyo)
ca-central-1,Canada (Central)
ca-west-1,Canada West (Calgary)
eu-central-1,Europe (Frankfurt)
eu-west-1,Europe (Ireland)
eu-west-2,Europe (London)
eu-south-1,Europe (Milan)
eu-west-3,Europe (Paris)
eu-south-2,Europe (Spain)
eu-north-1,Europe (Stockholm)
eu-central-2,Europe (Zurich)
il-central-1,Israel (Tel Aviv)
me-south-1,Middle East (Bahrain)
me-central-1,Middle East (UAE)
sa-east-1,South America (São Paulo)
cn-north-1,China (Beijing)
cn-northwest-1,China (Ningxia)
EOF
usage="$(basename "$0") [-h|--help] [-r|--region] <instance_type>
Examples:
price_check t2.medium
price_check -r us-west-1 r4.large"
POSITIONAL=()
# If no arguments given, print help
if [ $# -eq 0 ]; then
echo "$usage"
exit 0
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
echo "$usage"
exit 0
;;
-r|--region)
REGION="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ "$REGION" = "" ]; then
REGION="us-east-1"
fi
if ! echo "$region_lookup_map" | grep -q "$REGION"; then
echo "Could not find region: $REGION"
exit 1
fi
graphql_region=$(echo "$region_lookup_map" | grep "$REGION" | cut -d',' -f2)
instance_type=$1
header="Content-Type: application/json"
read -r -d '' req_body << EOF
{
"query": "{ AmazonEC2( \
Location:\"${graphql_region}\", \
TermType:\"OnDemand\", \
InstanceType:\"${instance_type}\", \
OS:\"Linux\", \
Tenancy:\"Shared\", \
CapacityStatus:\"Used\", \
PreInstalledSW:\"NA\") \
{PricePerUnit Unit Currency}}", \
"variables":"","operationName":""
}
EOF
price=$(curl -sS -H "${header}" -X POST -d "${req_body}" \
https://fvaexi95f8.execute-api.us-east-1.amazonaws.com/Dev/graphql/ \
| cut -d':' -f 4 | cut -d',' -f 1 | cut -d'"' -f2)
if [ "$price" = "" ]; then
echo "Could not find instance type: $instance_type"
exit 1
fi
echo "$price"