Giter VIP home page Giter VIP logo

aws-ec2-pricing's Introduction

aws-ec2-pricing

This python script reads the worksheet of a formatted Excel workbook based on the included Hosts.xlsx spreadsheet file. The script uses the CPU, RAM, Storage, and OS values along with command line parameters to assign instance types from the General Purpose family of AWS instances. After running the script once you may assign any instance type in the 'Instance Final' column and run the script again using the -i switch with 'all' parameter to read existing instance values as input. Pricing is provided as a unit and daily cost in USD via AWS Pricing API.

Setup

To use this script you will need an AWS Account, Python, and AWS CLI.

AWS Account:

  1. Setup an AWS account
  2. Create a new user with no console access and generate an access and secret key
  3. Create an IAM policy named 'PricingFullAccess' using the following JSON code
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "pricing:DescribeServices",
                "pricing:GetAttributeValues",
                "pricing:GetProducts"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
  1. Create a group named 'PricingOnly' and attach the PricingFullAccess policy
  2. Add the user from step 2 to the PricingOnly group to restrict access to pricing information only

Python for Windows:

  1. Download Python3 (https://www.python.org/downloads/)
  2. Install Python using the installer and check the box to add Python to PATH
    • Note: You can use Python2 as the script and modules are backwards compatible
  3. Use PIP: Python's Package Manager to install required modules
  • C:\>python -V Display Python version and confirms install
  • C:\>python -m pip list Lists installed Python modules
  • C:\>python -m pip install argparse Install the argparse module if not already installed by the installer
  • C:\>python -m pip install boto3 Install the AWS module
  • C:\>python -m pip install json Install the json module
  • C:\>python -m pip install openpyxl Install the Excel read/write module

AWS CLI for Windows:

  1. Download AWS CLI (https://s3.amazonaws.com/aws-cli/AWSCLI64.msi)
  2. Install AWS CLI using the installer
  3. C:\aws configure Setup AWS CLI with Access and Secret key for the AWS user
  • Paste in the Access and Secret keys
  • Set default region to us-east-1. This region provides access to the Pricing API endpoint
  • Set default output format to JSON

Python3 and AWS CLI for MacOS:

For MacOS use Homebrew to install Python3, PIP, and AWS CLI *Some of the below commands may be optional

  1. Install Homebrew using the following command:
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. brew list Lists installed packages
  3. brew install python3 Install Python3, you should now have python 2.X that ships with MacOS and python3 installed
  4. brew install awscli Install AWS CLI
  5. aws --version Verify that the AWS CLI is installed correctly
  6. Use pip3 to install the same modules listed above in Python for Windows steps.

If command is not found in step 5 you will need to add it to the PATH.

  1. $PATH Check the current PATH
  2. export PATH=/Users/USERNAME/Library/Python/3.X/bin/:$PATH Add Python3 bin folder to PATH
  3. $PATH Check PATH again
  4. vi .bash_profile Add to bash profile: export PATH=~/Library/Python/3.X/bin/:$PATH
  5. sudo vi /etc/path Add /usr/local/sbin after /usr/local/bin

Usage

Place the aws-ec2-pricing.py script and Hosts.xlsx file in the same directory. Use the -h switch to displays help info and available options.

Assign initial instance type and price

Example: C:\>python aws-ec2-pricing.py -f Hosts.xlsx -w Data -r us-west-2 -i m5 -v gp2

  • The script inspects the CPU and RAM values and selects an EC2 instance from the m5 family that will support the larger of the 2 variables. As the script iterates through the rows it will convert storage from MB to GB and inspect the OS and retrieve the pricing matching the instance type, OS, storage and region.

Read instance value and update pricing

Example: C:\>python aws-ec2-pricing.py -f Hosts.xlsx -w Data -r us-west-2 -i all -v gp2

  • Using the 'all' parameter with the -i switch provides the option to manually assign instance types after initial assignment using any available instance type. This skips the assignment and reads the value in the 'Instance Final' column and updates pricing accordingly.

Assumptions & Considerations

  1. The unit pricing for both EC2 and EBS are converted into daily units.
  2. Pricing assumes OnDemand, AWS Provided Licensing, No application bundles, Tenancy is shared. These values can be changed in the script.
  3. If the OS can't be matched against Windows, Red Hat, or SUSE (Which have licensing built into the EC2 pricing) the instance will assume Linux as the OS.
  4. EC2 instances will match RAM in GB -2 where VM's having 16GB RAM may be assigned EC2 instance with 15GB RAM. This method was choosen to avoid over-sizing instances.
  5. The script does not factor in EBS snapshots which are currently $0.05 per/GB-month for data stored in all regions. It's a good idea to account for atleast 1 full snapshot for each volume.
  6. This script can easily be modified to work with RVTools output or other formats as required.

aws-ec2-pricing's People

Contributors

mutineer612 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

aws-ec2-pricing's Issues

Error in OSX

Hi,
I am following your instructions and I have this error:

โžœ  aws-ec2-pricing-master ./aws-pricing-query.py -h
Traceback (most recent call last):
  File "./aws-pricing-query.py", line 15, in <module>
    pricing = boto3.client('pricing')
  File "/usr/local/lib/python3.7/site-packages/boto3/__init__.py", line 79, in client
    return _get_default_session().client(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/boto3/session.py", line 199, in client
    aws_session_token=aws_session_token, config=config)
  File "/usr/local/lib/python3.7/site-packages/botocore/session.py", line 761, in create_client
    client_config=config, api_version=api_version)
  File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 55, in create_client
    service_model = self._load_service_model(service_name, api_version)
  File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 80, in _load_service_model
    api_version=api_version)
  File "/usr/local/lib/python3.7/site-packages/botocore/loaders.py", line 119, in _wrapper
    data = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/botocore/loaders.py", line 338, in load_service_model
    type_name='str')
botocore.exceptions.ValidationError: Invalid value ('pricing') for param service_name of type str

Any idea?

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.