Created
December 10, 2022 23:39
-
-
Save niski84/c9002584e96e3b8fe4e926a92f537f7b to your computer and use it in GitHub Desktop.
Fetch AWS prices for EC2 instances. Nick skich
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
type Product struct { | |
ProductFamily string `json:"productFamily"` | |
Attributes struct { | |
InstanceType string `json:"instanceType"` | |
Location string `json:"location"` | |
Memory float64 `json:"memory"` | |
Storage float64 `json:"storage"` | |
Tenancy string `json:"tenancy"` | |
} `json:"attributes"` | |
} | |
func main() { | |
// Get the list of all products from the Price List Query API | |
resp, err := http.Get("https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json") | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
// Read the response body | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
// Parse the JSON data | |
var data map[string][]Product | |
err = json.Unmarshal(body, &data) | |
if err != nil { | |
panic(err) | |
} | |
// Filter the products by Amazon EC2 | |
var ec2Products []Product | |
for _, products := range data { | |
for _, product := range products { | |
if product.ProductFamily == "Compute Instance" { | |
ec2Products = append(ec2Products, product) | |
} | |
} | |
} | |
// Print the Amazon EC2 products | |
fmt.Println("Amazon EC2 products:") | |
for _, product := range ec2Products { | |
fmt.Printf("- %s %s %s %.0fGB %.0fGB %s\n", product.Attributes.InstanceType, product.Attributes.Location, product.Attributes.Tenancy, product.Attributes.Memory, product.Attributes.Storage, product.ProductFamily) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment