-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2price.go
334 lines (292 loc) · 9.07 KB
/
ec2price.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"encoding/json"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/aws/aws-sdk-go/service/pricing/pricingiface"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"net/http"
"os"
"log"
"time"
"strings"
)
const priceRequestPeriod = 3600 //seconds
const usage = `
AWS EC2 OnDemand prices
Usage:
curl ec2.cicd.team/{region}/{ec2type}
curl ec2.cicd.team/{region}
curl ec2.cicd.team/all
Examples:
- get hourly onemand price for t3.nano instance in eu-west-1 AWS region
curl ec2.cicd.team/eu-west-1/t3.nano
- get hourly onemand prices for all instances in eu-west-1 AWS region
curl ec2.cicd.team/eu-west-1
- get hourly onemand prices for all instances in all AWS regions
curl ec2.cicd.team/all
`
const usage_html = `
<!DOCTYPE html>
<html>
<head>
<title>AWS EC2 OnDeman Prices</title>
</head>
<body>
<span style="font-family:courier new,courier,monospace; font-size:20px;">
<h4>AWS EC2 OnDemand prices</h4>
<p>Usage:</p>
<blockquote>
<p>
<strong>
curl ec2.cicd.team/<span style="color: #ff0000;">{AWSregion}</span>/<span style="color: #ff0000;">{EC2type}</span><br />
curl ec2.cicd.team/<span style="color: #ff0000;">{AWSregion}</span><br />
curl ec2.cicd.team/<span style="color: #ff0000;">all</span><br />
</strong>
</p>
</blockquote>
<p>Examples:</p>
<blockquote>
<p>
- get hourly onemand price for <span style="color: #ff0000;">t3.nano</span> instance in <span style="color: #ff0000;">eu-west-1</span> AWS region<br /><strong>
curl <a href="https://ec2.cicd.team/eu-west-1/t3.nano"><span style="color:#000000;">ec2.cicd.team/</span><span style="color: #ff0000;">eu-west-1</span><span style="color:#000000;">/</span><span style="color: #ff0000;">t3.nano</span></strong></a></p>
<p>
- get hourly onemand prices for all instances in <span style="color: #ff0000;">eu-west-1</span> AWS region<br /><strong>
curl <a href="https://ec2.cicd.team/eu-west-1"><span style="color:#000000;">ec2.cicd.team/</span><span style="color: #ff0000;">eu-west-1</span></strong></a></p>
<p>
- get hourly onemand prices for all instances in all AWS regions<br /><strong>
curl <a href="https://ec2.cicd.team/all"><span style="color:#000000;">ec2.cicd.team/</span><span style="color: #ff0000;">all</span></strong></a></p>
</blockquote>
</span>
<hr />
<p><code>Sources at <a href="https://github/cicdteam/ec2price">github/cicdteam/ec2price</a></code></p>
</body>
</html>
`
var priceData = map[string]map[string]string{}
var regions = map[string]string{
"Asia Pacific (Tokyo)": "ap-northeast-1",
"Asia Pacific (Seoul)": "ap-northeast-2",
"Asia Pacific (Mumbai)": "ap-south-1",
"Asia Pacific (Singapore)": "ap-southeast-1",
"Asia Pacific (Sydney)": "ap-southeast-2",
"Canada (Central)": "ca-central-1",
"EU (Frankfurt)": "eu-central-1",
"EU (Stockholm)": "eu-north-1",
"EU (Ireland)": "eu-west-1",
"EU (London)": "eu-west-2",
"EU (Paris)": "eu-west-3",
"South America (Sao Paulo)": "sa-east-1",
"US East (N. Virginia)": "us-east-1",
"US East (Ohio)": "us-east-2",
"US West (N. California)": "us-west-1",
"US West (Oregon)": "us-west-2",
"AWS GovCloud (US-East)": "us-gov-east-1",
"AWS GovCloud (US)": "us-gov-west-1",
}
func getPrices(svc pricingiface.PricingAPI) error {
var priceInput = &pricing.GetProductsInput{
Filters: []*pricing.Filter{
{
Field: aws.String("ServiceCode"),
Value: aws.String("AmazonEC2"),
Type: aws.String(pricing.FilterTypeTermMatch),
},
{
Field: aws.String("operatingSystem"),
Value: aws.String("Linux"),
Type: aws.String(pricing.FilterTypeTermMatch),
},
{
Field: aws.String("preInstalledSw"),
Value: aws.String("NA"),
Type: aws.String(pricing.FilterTypeTermMatch),
},
{
Field: aws.String("licenseModel"),
Value: aws.String("No License required"),
Type: aws.String(pricing.FilterTypeTermMatch),
},
{
Field: aws.String("capacitystatus"),
Value: aws.String("UnusedCapacityReservation"),
Type: aws.String(pricing.FilterTypeTermMatch),
},
{
Field: aws.String("tenancy"),
Value: aws.String("Shared"),
Type: aws.String(pricing.FilterTypeTermMatch),
},
},
FormatVersion: aws.String("aws_v1"),
MaxResults: aws.Int64(100),
ServiceCode: aws.String("AmazonEC2"),
}
priceDataNew := make(map[string]map[string]string)
err := svc.GetProductsPages(priceInput,
func(page *pricing.GetProductsOutput, lastPage bool) bool {
var reg, instanceType, price string
for _, item := range page.PriceList {
reg = regions[item["product"].(map[string]interface{})["attributes"].(map[string]interface{})["location"].(string)]
if reg == "" {
continue
}
instanceType = item["product"].(map[string]interface{})["attributes"].(map[string]interface{})["instanceType"].(string)
ondemand_map := item["terms"].(map[string]interface{})["OnDemand"].(map[string]interface{})
for _, dimen := range ondemand_map {
for _, pricedim := range dimen.(map[string]interface{})["priceDimensions"].(map[string]interface{}) {
price = pricedim.(map[string]interface{})["pricePerUnit"].(map[string]interface{})["USD"].(string)
}
}
if priceDataNew[reg] == nil {
priceDataNew[reg] = map[string]string{}
}
priceDataNew[reg][instanceType] = price
}
return true
})
if err == nil {
priceData = priceDataNew
}
return err
}
func getPricesLoop(svc pricingiface.PricingAPI) {
for {
time.Sleep(priceRequestPeriod * time.Second)
err := getPrices(svc)
if err != nil {
log.Println("ERROR:", err.Error())
}
}
}
func answerPrice(w http.ResponseWriter, r *http.Request) {
var region, ec2type string
region = mux.Vars(r)["region"]
ec2type = mux.Vars(r)["ec2type"]
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
if len(region) > 0 {
if len(priceData[region]) > 0 {
if len(ec2type) > 0 {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
if len(priceData[region][ec2type]) > 0 {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, priceData[region][ec2type])
} else {
w.WriteHeader(http.StatusNotFound)
}
} else {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(priceData[region])
}
} else {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
}
} else {
if len(priceData) > 0 {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(priceData)
} else {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
}
}
}
func usagePage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if strings.Index(r.Header.Get("User-Agent"), "curl") > -1 {
fmt.Fprintln(w, usage)
} else {
fmt.Fprintln(w, usage_html)
}
}
type Route struct {
Name string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route
var routes = Routes{
Route{
"allPrices",
"/all",
answerPrice,
},
Route{
"regionPrices",
"/{region}",
answerPrice,
},
Route{
"instancePrice",
"/{region}/{ec2type}",
answerPrice,
},
Route{
"index",
"/",
usagePage,
},
}
func NewRouter() http.Handler {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
router.
Methods("GET", "HEAD").
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
return handlers.CombinedLoggingHandler(os.Stdout, handlers.ProxyHeaders(router))
}
func awsInit() *session.Session {
awsconf := &aws.Config{
Region: aws.String("us-east-1"), // use us-east-1 as aws pricing api available only in two regions
CredentialsChainVerboseErrors: aws.Bool(false),
}
sess := session.Must(session.NewSession(awsconf))
return sess
}
func main() {
log.Println("Getting prices from AWS")
svc := pricing.New(awsInit())
err := getPrices(svc)
if err != nil {
log.Println("ERROR:", err.Error())
} else {
rsum := 0
isum := 0
for i := range priceData {
rsum++
isum = isum + len(priceData[i])
}
log.Println("got prices for", isum, "ec2 instance types in", rsum, "regions")
}
go getPricesLoop(svc)
log.Println("Serving http requests")
router := NewRouter()
srv := &http.Server{
Addr: ":8000",
ReadTimeout: 5 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: router,
}
log.Fatal(srv.ListenAndServe())
}