Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add module #26

Merged
merged 8 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .azure-pipelines-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ steps:
condition: succeededOrFailed()

# Check types
- script: yarn lint:types
- script: yarn types
displayName: "Check types"
condition: succeededOrFailed()

Expand Down
15 changes: 15 additions & 0 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ trigger:
tags:
include:
- latest
paths:
exclude:
- .github/*
- bin/*
- docs/*
- "*.md"
- .codeclimate.yml
- .eslintrc.js
- .gitignore
- .npmignore
- .prettierignore
- aws-spot-price.code-workspace
- commitlint.config.js
- webpack.config.js
- yarn.lock

jobs:
- job: Release
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ec2-gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: yarn lint

- name: Check types
run: yarn lint:types
run: yarn types

- name: Run tests and update snapshots
run: yarn test -u
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
dist
junit.xml
node_modules
types
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ docs/
junit.xml
node_modules/
patches/
scripts/
sonar-project.properties
src/
stats.json
test/
tsconfig*.json
util/
webpack.config.js
yarn.lock
118 changes: 97 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,163 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=hoonoh_aws-spot-price&metric=coverage)](https://sonarcloud.io/component_measures?id=hoonoh_aws-spot-price&metric=coverage&view=list)
[![Greenkeeper badge](https://badges.greenkeeper.io/hoonoh/aws-spot-price.svg)](https://greenkeeper.io/)

CLI utility to list current global AWS EC2 Spot Instance prices.
Lists current global AWS EC2 Spot Instance prices.

## Example
Supports CLI and module usage.

## CLI

### Example

![Example](https://raw.githubusercontent.com/hoonoh/aws-spot-price/master/docs/preview.svg?sanitize=true)

## Installation
### Installation

### npm
#### npm

`npm -g i aws-spot-price`

### yarn
#### yarn

`yarn global add aws-spot-price`

### run with npx
#### run with npx

`npx aws-spot-price`

## Usage
### Usage

`aws-spot-run [options]`

If no options are applied, it will fetch all recent pricing data from default regions and show top 20 cheapest instances.

### Credentials
#### Credentials

This CLI utility uses AWS-SDK and requires AWS Access & Secret keys. If environment variables pair `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` or `~/.aws/credentials` is available it will use it. Otherwise, you will need to supply credentials through CLI options [`--accessKeyId`](#accessKeyId) and [`--secretAccessKey`](#secretAccessKey).

### Options
#### Options

#### --ui
##### --ui

Start with UI mode.

#### --region | -r
##### --region | -r

AWS region to fetch data from. Accepts multiple string values.
Defaults to all available AWS region which does not require opt-in.

#### --family
##### --family

EC2 instance families to filter. Will be translated to `--familyType` and `--size` values.
Accepts multiple string values.
Choose from: `general`, `compute`, `memory`, `storage`, `acceleratedComputing`

#### --instanceType | -i
##### --instanceType | -i

Type of EC2 instance to filter. Accepts multiple string values.
Enter valid EC2 instance type name. e.g. `-i t3.nano t3a.nano`

#### --familyType | -f
##### --familyType | -f

EC2 Family type (`c4`, `c5`, etc..). Accepts multiple string values.

#### --size | -s
##### --size | -s

EC2 size (`large`, `xlarge`, etc..). Accepts multiple string values.

#### --priceMax | -p
##### --priceMax | -p

Maximum price.

#### --productDescription | -d
##### --productDescription | -d

Instance product description to filter. Accepts multiple string values.
You can use `linux` or `windows` (all in lowercase) as wildcard.

#### --limit | -l
##### --limit | -l

Limits list of price information items to be returned.

#### --json | -j
##### --json | -j

Outputs in JSON format. This option will silence any progress output.

#### <a name="accessKeyId"></a>--accessKeyId
##### <a name="accessKeyId"></a>--accessKeyId

Specific AWS Access Key ID. Requires `--secretAccessKey` option to be used together.

#### <a name="secretAccessKey"></a>--secretAccessKey
##### <a name="secretAccessKey"></a>--secretAccessKey

Specific AWS Secret Access Key. Requires `--accessKeyId` option to be used together.

## Module

### Installation

#### npm

`npm i aws-spot-price`

#### yarn

`yarn add aws-spot-price`

### Example

#### Code

```javascript
import { getGlobalSpotPrices } from 'aws-spot-price';

(async () => {
const results = await getGlobalSpotPrices({
regions: ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2'],
familyTypes: ['c3', 'c4', 'c5'],
sizes: ['large', 'medium', 'xlarge'],
limit: 5,
});
console.log(JSON.stringify(results, null, 2));
})();
```

#### Results

```json
[
{
"AvailabilityZone": "us-east-2a",
"InstanceType": "c4.large",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "0.018100",
"Timestamp": "2019-11-05T03:07:19.000Z"
},
{
"AvailabilityZone": "us-east-2c",
"InstanceType": "c4.large",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "0.018100",
"Timestamp": "2019-11-05T03:07:19.000Z"
},
{
"AvailabilityZone": "us-east-2a",
"InstanceType": "c5.large",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "0.019000",
"Timestamp": "2019-11-04T14:51:42.000Z"
},
{
"AvailabilityZone": "us-east-2c",
"InstanceType": "c5.large",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "0.019000",
"Timestamp": "2019-11-04T14:51:42.000Z"
},
{
"AvailabilityZone": "us-east-2b",
"InstanceType": "c5.large",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "0.019300",
"Timestamp": "2019-11-04T14:51:42.000Z"
}
]
```
2 changes: 1 addition & 1 deletion bin/aws-spot-price
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../dist/aws-spot-price.bundle');
require('../dist/cli');
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
"name": "hoonoh"
},
"bin": "bin/aws-spot-price",
"main": "dist/module.js",
"types": "types/module.d.ts",
"license": "MIT",
"scripts": {
"prepublishOnly": "yarn clean && yarn build",
"clean": "rm -rf dist && rm -rf coverage",
"prepublishOnly": "yarn clean && yarn build && yarn types",
"clean": "rm -rf dist && rm -rf types && rm -rf coverage",
"build:ec2-types": "ts-node -T scripts/generate-ec2-types.ts",
"build": "patch-package && webpack",
"changelog": "conventional-changelog -i CHANGELOG.md -s -p angular",
"test": "patch-package && jest --runInBand --verbose",
"test:coverage": "yarn test --coverage",
"test:ci": "yarn test:coverage --ci --reporters=jest-junit --coverageReporters lcov --coverageReporters cobertura",
"test:ci": "yarn test:coverage --reporters=default jest-junit --coverageReporters lcov --coverageReporters cobertura",
"lint": "eslint **/*.ts",
"lint:types": "tsc",
"types": "tsc",
"semantic-release": "semantic-release"
},
"config": {
Expand All @@ -47,7 +49,8 @@
}
},
"eslintIgnore": [
"dist"
"dist",
"types"
],
"prettier": {
"printWidth": 100,
Expand Down Expand Up @@ -129,6 +132,7 @@
"prettier": "^1.18.2",
"prompts": "^2.2.1",
"semantic-release": "^15.13.27",
"string-replace-loader": "^2.2.0",
"table": "^5.4.6",
"ts-jest": "^24.1.0",
"ts-loader": "^6.2.1",
Expand Down
6 changes: 3 additions & 3 deletions scripts/generate-ec2-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import * as prettier from 'prettier';
import prettier from 'prettier';

import { getGlobalSpotPrices } from '../src/lib/lib';
import { getGlobalSpotPrices } from '../src/lib/core';

const familyGeneral = ['a', 't', 'm'];

Expand Down Expand Up @@ -92,7 +92,7 @@ const sortInstances = (i1: string, i2: string): number => {
const getEc2Types = async (): Promise<string> => {
let prices;
try {
prices = await getGlobalSpotPrices({ silent: true });
prices = await getGlobalSpotPrices();
} catch (error) {
console.log(`getGlobalSpotPrices error: ${error}`);
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-spot-prices-mock-data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as EC2 from 'aws-sdk/clients/ec2';
import EC2 from 'aws-sdk/clients/ec2';
import { readFileSync, writeFileSync } from 'fs';
import { find, uniqWith, xorWith } from 'lodash';
import { resolve } from 'path';
import * as yargs from 'yargs';
import yargs from 'yargs';

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

Expand Down
2 changes: 1 addition & 1 deletion src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('cli', () => {
});

describe('test by spawnSync', () => {
const cliJsPath = resolve(__dirname, '../dist/aws-spot-price.bundle.js');
const cliJsPath = resolve(__dirname, '../dist/cli.js');
it('should stdout help screen', () => {
const s = spawnSync('node', [cliJsPath, '--help'], { encoding: 'utf-8' });
expect(s.stdout).toMatchSnapshot();
Expand Down
Loading