#!/usr/bin/perl -w ###################################################################### # AWS Pricing helper, (C) 2015-2020 Nosey Nick Waterman, # https://noseynick.org/artemis/nebula/ # All wrong righted, all rights reserved. Licensed under thev GNU # Affero General Public License v3.0 https://www.gnu.org/licenses/agpl.txt # with Commons Clause https://commonsclause.com/ v1.0 ###################################################################### use strict; use LWP::Simple; use JSON::PP; use Data::Dumper; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # fetch the top-level EC2 pricing page: # my $URL = 'https://aws.amazon.com/ec2/pricing/on-demand/'; # my $page = get($URL) or die "Can't fetch $URL: $!\n"; # extract the pricing model URLs: # $page =~ m( data-manifest-url="(.*?)" data-service-url="(.*?/ondemand/linux/.*?)" ) # or die "Can't find /ondemand/linux/ line on $URL\n"; my $manifest = 'https://a0.p.awsstatic.com/pricing/1.0/ec2/manifest.json'; my $service = 'https://a0.p.awsstatic.com/pricing/1.0/ec2/region/{{region}}/ondemand/linux/index.json'; print "# $manifest\n"; $manifest = get($manifest) or die "Can't fetch manifest $manifest: $!\n"; $manifest = decode_json($manifest) or die "Can't parse JSON in manifest\n"; $manifest = $manifest->{ec2} or die "manifest does not contain ec2?\n"; print "# Each $service ...\n"; for my $region (@$manifest) { # construct region-specific URL: my $URL = $service; $URL =~ s|\{\{region\}\}|$region|; print "# $URL\n"; # go fetch the JSON: my $JSON = get($URL) or die "Can't fetch data $URL: $!\n"; $JSON = decode_json($JSON) or die "Can't parse data from $URL!\n"; my $ver = $JSON->{metadata}; $ver = $ver->{"source:version"} if $ver; if ($ver =~ /^(20\d\d)(\d\d)(\d\d)/) { $ver = "$1-$2-$3" } else { $ver = '' } print "# Last updated $ver\n" if $ver; $JSON = $JSON->{prices} or die "data does not contain prices?\n"; for my $size (@$JSON) { my $price = $size->{price} // {}; $price = $price->{USD} // '???'; print "$price USD/hr:"; for my $col (qw(vcpu ecu memory storage region physicalProcessor instanceFamily instanceType)) { my $val = $size->{attributes}->{"aws:ec2:$col"} // $size->{attributes}->{"aws:$col"} // '???'; $val =~ s/ //g; print " $col=$val"; } print "\n"; } }