Code Monkey home page Code Monkey logo

spotlib's Introduction


spotlib


Summary

Python 3 library for retrieving historical spot prices from Amazon EC2 Service.

Internally, spotlib utilises a paged generator architecture to achieve maximum performance with non-blocking state transition.

Although spotlib was designed for maximum flexibility as a library, spotlib also includes a cli helper application, spotcli, a cli binary which can be used directly for retrieving spot price data simple use cases.

Version: 0.2.10


Contents

--

back to the top


Dependencies

spotlib requires:

back to the top


Installation

spotlib may be installed on Linux via pip, python package installer in one of two methods:

To install spotlib for a single user:

$  pip3 install spotlib --user

To install spotlib for all users (Linux):

$  sudo -H pip3 install spotlib

back to the top


Use


Use / Spotlib Library

spotlib can be used most flexibly as an importable library:

  1. Initial Setup: Default endpoints for data retreival is a period of 1 day

    >>> from spotlib import SpotPrices, DurationEndpoints
    >>> sp = SpotPrices()
    # Display datetime endpoints
    >>> sp.start
    
    datetime.datetime(2019, 9, 17, 0, 0)
    
    >>> sp.end
    
    datetime.datetime(2019, 9, 18, 0, 0)
    
  2. Set custom endpoints (start & end date times, 10 days back from today):

    >>> start, end = sp.set_endpoints(duration=10)
  3. Retrieve spot price data for the custom time period for a particular region:

    >>> prices = sp.generate_pricedata(regions=['eu-west-1'])
  4. Examine price data returned:

    >>> from libtools.js import export_iterobject
    >>> export_iterobject(prices)

--

back to the top


Use / Spotcli Helper Application / Options

To display the help menu for spotcli, the included command line helper application:

    $ spotcli --help

--

back to the top


Use / Spotcli Helper Application / Data Retrieval (1 region)

To run a test of the spotlib library, retrieve spot price data and writeout to disk:

$ spotcli --duration-days 3 --region eu-west-1

--

back to the top


Use / Spotcli Helper Application / Data Retrieval (multi-region)

To run a test of the spotlib library, retrieve spot price data and writeout to disk:

$ spotcli --duration-days 1 --region eu-west-1 eu-west-2 us-east-1

--

back to the top


IAM Permissions

Either an Identity and Access Management user or role must be used to retrieve spot price data from AWS. The following is the minimum permissions required to retrieve data:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeSpotPriceHistory",
                "ec2:DescribeRegions"
            ],
            "Resource": "*"
        }
    ]
}

Alternatively, if more permissive (but still read-only) permissions can be tolerated, the AWS Managed Policy 'AmazonEC2ReadOnlyAccess' can be used via the following ARN:

  • arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess

The permissions above for IAM are a subset of the AmazonEC2ReadOnlyAccess policy. Either should work without problems.

--

back to the top


Author & Copyright

All works contained herein copyrighted via below author unless work is explicitly noted by an alternate author.

  • Copyright Blake Huber, All Rights Reserved.

back to the top


License

  • Software contained in this repo is licensed under the license agreement. You may display the license and copyright information by issuing the following command:
$ spotcli --version

back to the top


Disclaimer

Code is provided "as is". No liability is assumed by either the code's originating author nor this repo's owner for their use at AWS or any other facility. Furthermore, running function code at AWS may incur monetary charges; in some cases, charges may be substantial. Charges are the sole responsibility of the account holder executing code obtained from this library.

Additional terms may be found in the complete license agreement.

back to the top


spotlib's People

Contributors

fstab50 avatar

Watchers

 avatar

spotlib's Issues

Cannot use credentials in memory; only awscli credentials

When access key and secret key are exported to the shell environment (as env vars), boto3 will use them first before looking for credentials submitted via the awscli and --profile <value>.

spotlib currently fails authentication to AWS if credentials are environment variables. A pre-run test module must be developed that performs an authentication test to find out if the environment contains valid credentials. If yes, use them, if no keys in the environment, spotlib must then utilise awscli credentials.

The following code could be utilised as a pre-run test and branch on the results, performing the run.

try:
    client = boto3.client('sts')
    client.get_caller_identity()
except Exception:
    print('try --profile')

Credential hierarchy for spotlib matches boto3:

  • use in memory credentials if found & valid
  • else use awscli credentials

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.