Skip to content

Commit

Permalink
feat: added regions option
Browse files Browse the repository at this point in the history
  • Loading branch information
hoonoh committed Oct 15, 2019
1 parent 59d4e7b commit ba08b43
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ CLI utility to list current global AWS EC2 Spot Instance prices. Requires valid

## Options

### --regions | -r

AWS regions to search. Accepts multiple string values.
Defaults to all available AWS regions which does not require opt-in.

### --instanceTypes | -i

Type of EC2 instance to filter. Accepts multiple string values.
Expand Down
13 changes: 13 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import * as yargs from 'yargs';

import { allInstances, instanceFamilies, instanceSizes } from './ec2-types';
import { awsCredentialsCheck, getGlobalSpotPrices, ProductDescription } from './lib';
import { defaultRegions, Region } from './regions';

const { argv } = yargs
.scriptName('spot-price')
.command(
'$0',
'get current AWS spot instance prices',
{
regions: {
alias: 'r',
describe: 'AWS regions.',
type: 'array',
choices: defaultRegions,
string: true,
},
instanceTypes: {
alias: 'i',
describe: 'EC2 type',
Expand Down Expand Up @@ -67,6 +75,7 @@ const { argv } = yargs

async args => {
const {
regions,
instanceTypes,
families,
sizes,
Expand All @@ -76,10 +85,12 @@ const { argv } = yargs
accessKeyId,
secretAccessKey,
} = args;

if ((!families && sizes) || (families && !sizes)) {
console.log('`families` or `sizes` attribute missing.');
return;
}

if (
(accessKeyId !== undefined && secretAccessKey === undefined) ||
(accessKeyId === undefined && secretAccessKey !== undefined)
Expand All @@ -98,6 +109,7 @@ const { argv } = yargs
try {
console.log('Querying current spot prices with options:');
console.group();
if (regions) console.log('regions:', regions);
if (instanceTypes) console.log('instanceTypes:', instanceTypes);
if (families) console.log('families:', families);
if (sizes) console.log('sizes:', sizes);
Expand All @@ -107,6 +119,7 @@ const { argv } = yargs
console.groupEnd();

getGlobalSpotPrices({
regions: regions as Region[],
instanceTypes,
families,
sizes,
Expand Down
9 changes: 6 additions & 3 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { EC2, STS } from 'aws-sdk';
import { table } from 'table';

import { Region, regionNames, regions } from './regions';
import { defaultRegions, Region, regionNames } from './regions';

// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html
// https://aws.amazon.com/ec2/instance-types/

export enum ProductDescription {
Expand Down Expand Up @@ -91,6 +90,7 @@ const getEc2SpotPrice = async (options: {

export const getGlobalSpotPrices = async (
options: {
regions?: Region[];
families?: string[];
sizes?: string[];
priceMax?: number;
Expand All @@ -103,8 +103,11 @@ export const getGlobalSpotPrices = async (
} = {},
) => {
const { families, sizes, priceMax, limit, quiet, accessKeyId, secretAccessKey } = options;
let { productDescriptions, instanceTypes } = options;
let { regions, productDescriptions, instanceTypes } = options;
let rtn: EC2.SpotPrice[] = [];

if (regions === undefined) regions = defaultRegions;

if (productDescriptions && productDescriptions.indexOf(ProductDescription.windows) >= 0) {
productDescriptions = [ProductDescription.Windows, ProductDescription['Windows (Amazon VPC)']];
} else if (productDescriptions && productDescriptions.indexOf(ProductDescription.linux) >= 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Region =
| 'me-south-1'
| 'sa-east-1';

export const regions: Region[] = [
export const defaultRegions: Region[] = [
'us-east-1',
'us-east-2',
'us-west-1',
Expand Down

0 comments on commit ba08b43

Please sign in to comment.