Skip to content

A tiny CLI tool to help save costs in development environments when you're asleep and don't need them!

License

Notifications You must be signed in to change notification settings

aramalipoor/aws-cost-saver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aws-cost-saver

CircleCI codecov

A tiny CLI tool to help save costs in development environments when you're asleep and don't need them!

Disclaimer

This utility is meant for development environments only where stopping and removing resources is not risky.

Usage

# Install
$ npm install -g aws-cost-saver

# Try
$ aws-cost-saver conserve --help
$ aws-cost-saver conserve --dry-run --no-state-file
$ aws-cost-saver conserve --dry-run --no-state-file --only-summary
$ aws-cost-saver conserve -d -n --tag Team=Tacos
$ aws-cost-saver conserve -d -n -t Team=Tacos -t Application=Orders

# Use
$ aws-cost-saver conserve
$ aws-cost-saver restore

Commands

Under the hood aws-sdk is used, therefore AWS Credentials are read in this order:

  1. From AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION environment variables.
  2. From shared ini file (i.e. ~/.aws/credentials)

Conserve

This command uses various tricks to conserve as much money as possible. To be able to restore, this command will create a state-file.

USAGE
  $ aws-cost-saver conserve [-d|--dry-run] [-s|--state-file aws-cost-saver.json] ...

OPTIONS
  -d, --dry-run                          Only print actions and write state-file of current resources.
  -n, --no-state-file                    Ignore saving current state, useful when want to only conserve as much money as possible.
  -s, --state-file state-file.json       [default: file://aws-cost-saver.json] Where to keep original state of stopped/decreased resources to restore later.
  -w, --overwrite-state-file             Overwrite state-file if it exists. WARNING: Use with caution as this might overwrite non-restored state-file.
  
  -u, --use-trick trick-machine-name     Enables an individual trick. Useful for tricks that are disabled by default. Can be used multiple times.
  -i, --ignore-trick trick-machine-name  Disables an individual trick. Useful when you do not like to use a specific trick. Can be used multiple times.
  --no-default-tricks                    Disables all default tricks. Useful alongside --use-trick to enable only specific set of tricks.
  
  -t, --tag Name[=Value]                 Tags to narrow down targeted resources. Multiple tags will be AND-ed. Providing "Value" is optional.
  -r, --region eu-central-1              [default: eu-central-1] AWS Region to converse resources in.
  -p, --profile my-aws-profile           [default: default] AWS Profile to use from ~/.aws/config
  -m, --only-summary                     Do not render live progress. Only print final summary in a clean format.
  -h, --help                             Show CLI help

Restore

To restore AWS resources stopped or removed by the conserve command.

USAGE
  $ aws-cost-saver restore [-d|--dry-run] [-s|--state-file aws-cost-saver.json] ...

OPTIONS
  -d, --dry-run          Only list actions and do not actually execute them.
  -s, --state-file       [default: file://aws-cost-saver.json] Path to load previous state of your AWS resources from.
  
  -r, --region           [default: eu-central-1] AWS region to restore resoruces in.
  -p, --profile          [default: default] AWS profile to lookup from ~/.aws/config
  -m, --only-summary     Do not render live progress. Only print final summary in a clean format.
  -h, --help             Show CLI help.

State-file

When resources are conserved their current state (e.g. number of shards, number of instances, etc.) will be saved in a state-file to be to used to restore at a later time.

At the moment when you restore the resources it's up to you to either remove the state-file or save it for a later use.

Supported storage providers

aws-cost-saver supports file: and s3: storage providers.

AWS S3

$ aws-cost-saver conserve --dry-run --state-file s3://my-bucket-name/some-dir/aws-cost-saver.json

Local File System

$ aws-cost-saver conserve --dry-run --state-file file://./aws-cost-saver.json
$ aws-cost-saver conserve --dry-run --state-file file:///etc/aws-cost-saver.json

# Local file system is the default storage
$ aws-cost-saver conserve --dry-run --state-file ./aws-cost-saver.json
$ aws-cost-saver conserve --dry-run --state-file /etc/aws-cost-saver.json

Keeping an ideal state-file

One use-case is to keep an ideal state in a state-file (e.g. result of first time you run conserve) then always conserve resources without keeping the state and restore from the ideal state-file.

For example on day 1:

# Generate a state-file of current resources:
aws-cost-saver conserve --dry-run --state-file ideal-state.json

# Manually update numbers if you like:
vi ideal-state.json

Then the daily routine can look like this:

# In the morning, bring back the ideal state:
aws-cost-saver restore --state-file ideal-state.json

# ... develop amazing software :D

# At night, conserve as much as possible without keeping the state:
aws-cost-saver conserve --no-state-file

Tricks

Here is a list of tricks aws-cost-saver uses to reduce AWS costs when you don't need them.

# shutdown-ec2-instances

Stopping running EC2 instances will save compute-hour. This trick will keep track of stopped EC2 instances in the state-file and start them again on restore.

# stop-fargate-ecs-services

Stopping AWS Fargate ECS services (i.e. tasks) will save compute-hour. This trick will keep track of stopped Fargate ECS services in the state-file and start them again on restore.

# stop-rds-database-instances

Stopping RDS databases will save underlying EC2 instance costs. This trick will keep track of stopped databases in the state-file and start them again on restore.

# decrease-dynamodb-provisioned-rcu-wcu

Provisioned RCU and WCU on DynamoDB tables costs hourly. This trick will decrease them to minimum value (i.e. 1). Original values will be stored in state-file to be restored later.

# remove-nat-gateways

NAT Gateways are charged hourly. This trick will remove NAT Gateways while you don't use your services, and creates them again on "restore" command.

  • Removing NAT Gateways stops instances access to internet.
  • This trick is currently disabled by default because removing/recreating NAT gateway will change the ID therefore IaC such as terraform will be confused. Use --use-trick flag to explicitly enable it:
$ aws-cost-saver conserve --use-trick remove-nat-gateways

# snapshot-and-remove-elasticache-clusters

ElastiCache clusters cost hourly but unfortunately it's not possible to stop them like an EC2 instance. To save costs this trick will take a snapshot of current cluster (preserving data, config and cluster ID) and delete it. To restore it'll create a new cluster based on snapshot taken.

  • Due to AWS limitation, backup and restore is supported only for clusters running on Redis.
  • This trick is currently disabled by default to be tested by early users. Use --use-trick flag to explicitly enable it:
$ aws-cost-saver conserve --use-trick snapshot-remove-elasticache-redis

# decrease-kinesis-streams-shards

Kinesis Stream Shards cost hourly. This trick will decrease open shards to the minimum of 1, in multiple steps by halving number of shards in each step. Currently this trick is useful when you're doing UNIFORM_SCALING, i.e. default config of Kinesis Stream.

# stop-rds-database-clusters

Stopping RDS clusters will save underlying EC2 instance costs. This trick will keep track of stopped clusters in the state-file and start them again on restore.

# scaledown-auto-scaling-groups

When Auto Scaling Groups are configured they might launch EC2 instances. This trick will set "desired", "min" and "max" capacity of ASGs to zero and keep track of original values in the state-file. Scaling-down an ASG will terminate all instances therefore temporary volumes will be lost.

  • This trick is currently disabled by default. Use --use-trick flag to explicitly enable it:
$ aws-cost-saver conserve --use-trick scaledown-auto-scaling-groups

# suspend-auto-scaling-groups

When Auto Scaling Groups processes are active they might launch EC2 instances. This trick will suspend all processes of ASGs to prevent launching new instances.

# etc

If you know any other tricks to save some money feel free to create a Pull Request or raise an issue.

Alternatives

There are various ways to save money on AWS that need per-case judgement and it'll be hard to generalize into aws-cost-saver, but here is a list of useful resources:

License

License: MIT

AWS Cost Saver is licensed under MIT License. See LICENSE for the full license text.

About

A tiny CLI tool to help save costs in development environments when you're asleep and don't need them!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages