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.
To use this script you will need an AWS Account, Python, and AWS CLI.
- Setup an AWS account
- Create a new user with no console access and generate an access and secret key
- 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": [
"*"
]
}
]
}
- Create a group named 'PricingOnly' and attach the PricingFullAccess policy
- Add the user from step 2 to the PricingOnly group to restrict access to pricing information only
- Download Python3 (https://www.python.org/downloads/)
- 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
- Use PIP: Python's Package Manager to install required modules
C:\>python -V
Display Python version and confirms installC:\>python -m pip list
Lists installed Python modulesC:\>python -m pip install argparse
Install the argparse module if not already installed by the installerC:\>python -m pip install boto3
Install the AWS moduleC:\>python -m pip install openpyxl
Install the Excel read/write module
- Download AWS CLI (https://s3.amazonaws.com/aws-cli/AWSCLI64.msi)
- Install AWS CLI using the installer
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
For MacOS use Homebrew to install Python3, PIP, and AWS CLI *Some of the below commands may be optional
- Install Homebrew using the following command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew list
Lists installed packagesbrew install python3
Installs Python3, you should now have python 2.X that ships with MacOS and python3 installedpip3 install awscli --upgrade --user
Use PIP to install AWS CLIaws --version
Verify that the AWS CLI is installed correctly
If command is not found in step 5 you will need to add it to the PATH.
$PATH
Check the current PATHexport PATH=/Users/USERNAME/Library/Python/3.6/bin/:$PATH
Add Python3 bin folder to PATH$PATH
Check PATH againvi .bash_profile
Add to bash profile:export PATH=~/Library/Python/3.6/bin/:$PATH
sudo vi /etc/path
Add /usr/local/sbin after /usr/local/bin
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.
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.
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.
- The unit pricing for both EC2 and EBS are converted into daily units.
- Pricing assumes OnDemand, AWS Provided Licensing, No application bundles, Tenancy is shared. These values can be changed in the script.
- 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.
- 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.
- 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.
- This script can easily be modified to work with RVTools output or other formats as required.