Package: ec2-spot-price
ec2-spot-price: Retrieve Amazon EC2 spot instance priceThis Python module provides simple functions and commands to retrieveAmazon EC2 spot instance priceby AWS API.InstallInstall bypip. Orpipxmay be convenient to use as a CLI application.pipinstallec2-spot-priceSetupYou need to setupAWS authentication credentialswhich have permissions to accessec2:DescribeSpotPriceHistoryandec2:DescribeRegionsAPIs.A simple way to do is tocreate a new IAM userand attachAmazonEC2ReadOnlyAccesspolicy directly to the user. Or you can use existent credentials which have permissions to accessec2:DescribeSpotPriceHistoryandec2:DescribeRegionsAPIs.Create a new IAM userCreate a new IAM user like the following.Go toIAM consoleClickUsersandAdd userUser name:myuser(whatever you want)Access type:Programmatic accessClickNext: PermissionsSet permissions:Attach existing policies directlyPolicy name:AmazonEC2ReadOnlyAccessClickNext: TagsClickNext: ReviewClickCreate userClickDownload .csvClickCloseNote: custom policyIf you don't want to attachAmazonEC2ReadOnlyAccesspolicy, you cancreate a new policythat only allows to accessec2:DescribeSpotPriceHistoryandec2:DescribeRegionsAPIs. Then attach this policy instead ofAmazonEC2ReadOnlyAccesspolicy.{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["ec2:DescribeSpotPriceHistory","ec2:DescribeRegions"],"Resource":"*"}]}Edit ~/.aws/credentialsYou can use "named profile" to have multiple credentials settings.[myprofile]  # whatever you want, or [default]aws_access_key_id=[copy from csv]aws_secret_access_key=[copy from csv]region=us-east-2# wherever you wantIf you use "named profile", you need to specifyAWS_PROFILEenvironment variable.exportAWS_PROFILE=myprofileUsageec2_spot_pricecommandYou can runec2_spot_price(orpython /path/to/ec2_spot_price.py) command to retrieve spot instance prices.-hoption shows help message.%ec2_spot_price-h
usage:ec2_spot_price[-h][-rREGION][-iINSTANCE][-oOS][-csv][-d][-V]retrieveAmazonEC2spotinstanceprice.

optionalarguments:-h,--helpshowthishelpmessageandexit-rREGION,--regionREGIONfilterbyregions.if""isspecified,retrievealloftheregions.(default:"us-east-1,us-east-2,us-west-1,us-west-2")-iINSTANCE,--instanceINSTANCEfilterbyinstancetypese.g."g3.4xlarge,p2.xlarge".(default:retrievealloftheinstancetypes)-oOS,--osOSfilterbyOStypes.(default:"Linux/UNIX")-csv,--csvoutputbyCSVformat.(default:False)-d,--debugshowdebuginformation.-V,--versionshowversion.You can specify region names by-r, instance types by-iand OS types by-o. For example, the following command shows table ofus-east-1,us-east-2regions,c5.xlarge,c5d.xlargeinstances andLinux/UNIXOS (default value).%ec2_spot_price-r"us-east-1,us-east-2"-i"c5.xlarge,c5d.xlarge"───────────────────────────────────────────────────────────────────────PriceZoneInstanceOSTimestamp───────────────────────────────────────────────────────────────────────0.038000us-east-2ac5.xlargeLinux/UNIX2021-02-2403:59:140.038000us-east-2ac5d.xlargeLinux/UNIX2021-02-2418:23:400.038000us-east-2bc5.xlargeLinux/UNIX2021-02-2403:59:140.038000us-east-2bc5d.xlargeLinux/UNIX2021-02-2420:49:320.038000us-east-2cc5.xlargeLinux/UNIX2021-02-2403:59:140.038000us-east-2cc5d.xlargeLinux/UNIX2021-02-2408:06:210.066500us-east-1cc5.xlargeLinux/UNIX2021-02-2415:56:410.066800us-east-1fc5d.xlargeLinux/UNIX2021-02-2418:38:060.069600us-east-1ac5d.xlargeLinux/UNIX2021-02-2417:55:160.069700us-east-1dc5.xlargeLinux/UNIX2021-02-2419:28:400.069900us-east-1bc5.xlargeLinux/UNIX2021-02-2421:10:110.072100us-east-1ac5.xlargeLinux/UNIX2021-02-2416:55:410.079600us-east-1cc5d.xlargeLinux/UNIX2021-02-2421:01:420.079600us-east-1dc5d.xlargeLinux/UNIX2021-02-2414:31:550.083700us-east-1fc5.xlargeLinux/UNIX2021-02-2414:48:410.090300us-east-1bc5d.xlargeLinux/UNIX2021-02-2403:58:17───────────────────────────────────────────────────────────────────────In this case, you should usec5.xlargeatus-east-2region.Another example to retrieve all of the spot prices in all regions with verbose option.%ec2_spot_price-r""-i""-o""-csv>spot_prices.csv
%wc-lspot_prices.csv49822spot_prices.csv
%headspot_prices.csv
SpotPrice,AvailabilityZone,InstanceType,ProductDescription,Timestamp0.000800,ap-south-1a,t4g.nano,Linux/UNIX,2021-02-2417:41:520.000800,ap-south-1a,t4g.nano,SUSELinux,2021-02-2417:41:520.000800,ap-south-1b,t4g.nano,Linux/UNIX,2021-02-2417:41:520.000800,ap-south-1b,t4g.nano,SUSELinux,2021-02-2417:41:520.000800,ap-south-1c,t4g.nano,Linux/UNIX,2021-02-2417:41:520.000800,ap-south-1c,t4g.nano,SUSELinux,2021-02-2417:41:520.000900,ap-south-1a,t3a.nano,Linux/UNIX,2021-02-2417:06:180.000900,ap-south-1a,t3a.nano,SUSELinux,2021-02-2417:06:180.000900,ap-south-1b,t3a.nano,Linux/UNIX,2021-02-2417:06:18Then openspot_prices.csvwith spread sheet application like Excel.ec2_spot_pricemoduleThere are three functions.Functionspot_pricesretrieves spot prices as list.Functionprint_csvprints spot prices with CSV format.Functionprint_tableprints spot prices with table format.%python>>>importec2_spot_priceasec2sp>>>df=ec2sp.spot_prices(['us-east-1','us-east-2'],['c5.xlarge','c5d.xlarge'],['Linux/UNIX'])>>>len(df)16>>>ec2sp.print_table(df)───────────────────────────────────────────────────────────────────────PriceZoneInstanceOSTimestamp───────────────────────────────────────────────────────────────────────0.038000us-east-2ac5.xlargeLinux/UNIX2021-02-2403:59:140.038000us-east-2ac5d.xlargeLinux/UNIX2021-02-2418:23:400.038000us-east-2bc5.xlargeLinux/UNIX2021-02-2403:59:140.038000us-east-2bc5d.xlargeLinux/UNIX2021-02-2420:49:320.038000us-east-2cc5.xlargeLinux/UNIX2021-02-2403:59:140.038000us-east-2cc5d.xlargeLinux/UNIX2021-02-2408:06:210.066500us-east-1cc5.xlargeLinux/UNIX2021-02-2415:56:410.066800us-east-1fc5d.xlargeLinux/UNIX2021-02-2418:38:060.069600us-east-1ac5d.xlargeLinux/UNIX2021-02-2417:55:160.069700us-east-1dc5.xlargeLinux/UNIX2021-02-2419:28:400.069900us-east-1bc5.xlargeLinux/UNIX2021-02-2421:10:110.072100us-east-1ac5.xlargeLinux/UNIX2021-02-2416:55:410.079600us-east-1cc5d.xlargeLinux/UNIX2021-02-2421:01:420.079600us-east-1dc5d.xlargeLinux/UNIX2021-02-2414:31:550.082500us-east-1fc5.xlargeLinux/UNIX2021-02-2421:35:410.090300us-east-1bc5d.xlargeLinux/UNIX2021-02-2403:58:17───────────────────────────────────────────────────────────────────────>>>ec2sp.print_csv(df)SpotPrice,AvailabilityZone,InstanceType,ProductDescription,Timestamp0.038000,us-east-2a,c5.xlarge,Linux/UNIX,2021-02-2403:59:140.038000,us-east-2a,c5d.xlarge,Linux/UNIX,2021-02-2418:23:400.038000,us-east-2b,c5.xlarge,Linux/UNIX,2021-02-2403:59:140.038000,us-east-2b,c5d.xlarge,Linux/UNIX,2021-02-2420:49:320.038000,us-east-2c,c5.xlarge,Linux/UNIX,2021-02-2403:59:140.038000,us-east-2c,c5d.xlarge,Linux/UNIX,2021-02-2408:06:210.066500,us-east-1c,c5.xlarge,Linux/UNIX,2021-02-2415:56:410.066800,us-east-1f,c5d.xlarge,Linux/UNIX,2021-02-2418:38:060.069600,us-east-1a,c5d.xlarge,Linux/UNIX,2021-02-2417:55:160.069700,us-east-1d,c5.xlarge,Linux/UNIX,2021-02-2419:28:400.069900,us-east-1b,c5.xlarge,Linux/UNIX,2021-02-2421:10:110.072100,us-east-1a,c5.xlarge,Linux/UNIX,2021-02-2416:55:410.079600,us-east-1c,c5d.xlarge,Linux/UNIX,2021-02-2421:01:420.079600,us-east-1d,c5d.xlarge,Linux/UNIX,2021-02-2414:31:550.082500,us-east-1f,c5.xlarge,Linux/UNIX,2021-02-2421:35:410.090300,us-east-1b,c5d.xlarge,Linux/UNIX,2021-02-2403:58:17Another example to retrieve all of the spot prices in all regions.
You can usepd.DataFramemethods to manipulate them.>>>importec2_spot_priceasec2sp>>>df=ec2sp.spot_prices([],[],[])>>>len(df)49817>>>df=df.query('ProductDescription == "Linux/UNIX"')>>>df=df.drop(['Timestamp','ProductDescription'],axis=1)>>>df=df.sort_values(by=['SpotPrice','AvailabilityZone','InstanceType'])>>>df=df.query('InstanceType.str.match("c5.?\.xlarge")')>>>len(df)187>>>df.head(20)SpotPriceAvailabilityZoneInstanceType420610.038000us-east-2ac5.xlarge401210.038000us-east-2ac5d.xlarge406500.038000us-east-2ac5n.xlarge420600.038000us-east-2bc5.xlarge396300.038000us-east-2bc5d.xlarge406490.038000us-east-2bc5n.xlarge420590.038000us-east-2cc5.xlarge417120.038000us-east-2cc5d.xlarge406480.038000us-east-2cc5n.xlarge397160.042400us-east-2cc5a.xlarge395920.044100us-east-2bc5a.xlarge31050.052800ap-south-1cc5a.xlarge31040.053200ap-south-1bc5a.xlarge25050.053500ap-south-1ac5a.xlarge28310.054000ap-south-1ac5n.xlarge34300.054000ap-south-1bc5d.xlarge37680.054100ap-south-1ac5d.xlarge17650.054100ap-south-1bc5n.xlarge17270.054100ap-south-1cc5d.xlarge29810.054100ap-south-1cc5n.xlargeLinkshttps://aws.amazon.com/ec2/spot/pricing/https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.htmlhttps://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances-history.htmlhttps://docs.aws.amazon.com/cli/latest/reference/ec2/describe-spot-price-history.htmlhttps://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSpotPriceHistory.htmlhttps://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRegions.htmlhttps://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configurationhttps://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html?highlight=describe%20spot#EC2.Client.describe_spot_price_historyAuthorSusumu OTA