Fetch and plot AWS spot pricing history
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
spot_reporter
.gitignore
LICENSE
README.md
setup.py

README.md

spot-price-reporter

Simple python package that will fetch and plot AWS spot prices for the given instance types. The plots cover the past 24 hours and the past 7 days. There is a built in option to send these to a specified slack channel

Setup

  1. Clone this repository then run python setup.py install. This is tested with Python 3, but may work with Python 2 (no guarantees).
  2. Create a slack API token from https://api.slack.com/docs/oauth-test-tokens
  3. Put the token in an environment variable SLACK_API_TOKEN. You can also configure the slack channel that is messaged by setting SLACK_CHANNEL. The default is #aws.

Usage

$ ipython -- spot_reporter/cli.py --help
Usage: cli.py [OPTIONS] INSTANCE_TYPES...

Options:
  -a, --action [email|slack]  Determine if/how to send aws pricing report
  --output-dir TEXT           What directory to output files to, by default
                              /tmp/spot-reporter
  --end-time TEXT             Last time to check spot price for
  --region TEXT               AWS region, by default uses the environment
  --skip-generation           Skip file generation
  --help                      Show this message and exit.

Example

Run a command like ipython -- spot_reporter/cli.py --action slack r3.8xlarge c3.8xlarge or aws_spot_price_history --action slack r3.8xlarge c3.8xlarge to fetch the reports for those instance types and upload the results to slack.

Airflow Example

Below is an example of an Apache Airflow DAG to run this every hour

from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta


default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2016, 8, 22),
    'retries': 1,
    'retry_delay': timedelta(minutes=1),
    }

dag = DAG('aws_spot_price_history', default_args=default_args, schedule_interval='@hourly')

run_all = BashOperator(
    task_id='run_all',
    bash_command='aws_spot_price_history --end-time {{ (execution_date + macros.timedelta(hours=1)).isoformat() }} --action slack --output-dir /tmp/spot-reporter/{{ (execution_date + macros.timedelta(hours=1)).isoformat() }} r3.8xlarge',
    dag=dag
)

Note that an hour is added to the timestamp due to how Airflow works. If a DAG is scheduled for 3PM this means that it will wait util 3PM + 1 hour (the schedule interval) to run. This is because airflow sees this as waiting until all the data for 3PM-4PM is in. Since we give an end date for AWS instead of a start date, we need to manually shift the time by one hour to get the most recent data