Giter Club home page Giter Club logo

aws-cost-cli's Introduction

aws-cost-cli

CLI tool to perform cost analysis on your AWS account with Slack integration

Installation

Install the package globally or alternatively you can also use npx

npm install -g aws-cost-cli

Usage

For the simple usage, just run the command without any options.

aws-cost

The output will be a the totals with breakdown by service. Optionally, you can pass the following options to modify the output:

$ aws-cost --help

  Usage: aws-cost [options]

  A CLI tool to perform cost analysis on your AWS account

  Options:
    -V, --version                  output the version number

    -k, --access-key [key]         AWS access key
    -s, --secret-key [key]         AWS secret key
    -r, --region [region]          AWS region (default: us-east-1)

    -p, --profile [profile]        AWS profile to use (default: "default")

    -j, --json                     Get the output as JSON
    -u, --summary                  Get only the summary without service breakdown
    -t, --text                     Get the output as plain text (no colors / tables)

    -S, --slack-token [token]      Slack token for the slack message
    -C, --slack-channel [channel]  Slack channel to post the message to

    -v, --version                  Get the version of the CLI
    -h, --help                     Get the help of the CLI

In order to use the CLI you can either pass the AWS credentials through the options i.e.:

aws-cost -k [key] -s [secret] -r [region]

or if you have configured the credentials using aws-cli, you can simply run the following command:

aws-cost

To configure the credentials using aws-cli, have a look at the aws-cli docs for more information.

Detailed Breakdown

The default usage is to get the cost breakdown by service

aws-cost

You will get the following output

Default Usage

Total Costs

You can also get the summary of the cost without the service breakdown

aws-cost --summary

You will get the following output

Summary Usage

Plain Text

You can also get the output as plain text

aws-cost --text

You will get the following output in response

Text Usage

JSON Output

You can also get the output as JSON

aws-cost --json
You will get the following output in response
{
  "account": "theroadmap",
  "totals": {
    "lastMonth": 0.38,
    "thisMonth": 11.86,
    "last7Days": 1.29,
    "yesterday": 0.22
  },
  "totalsByService": {
    "lastMonth": {
      "AmazonCloudWatch": 0,
      "Tax": 0,
      "AWS Key Management Service": 0,
      "AWS Service Catalog": 0,
      "Amazon Simple Email Service": 0.38,
      "Amazon Simple Notification Service": 0,
      "Amazon Simple Storage Service": 0.00001
    },
    "thisMonth": {
      "AmazonCloudWatch": 0,
      "Tax": 0,
      "AWS Key Management Service": 0,
      "AWS Service Catalog": 0,
      "Amazon Simple Email Service": 11.85,
      "Amazon Simple Notification Service": 0,
      "Amazon Simple Storage Service": 0
    },
    "last7Days": {
      "AmazonCloudWatch": 0,
      "Tax": 0,
      "AWS Key Management Service": 0,
      "AWS Service Catalog": 0,
      "Amazon Simple Email Service": 1.28,
      "Amazon Simple Notification Service": 0,
      "Amazon Simple Storage Service": 0
    },
    "yesterday": {
      "AmazonCloudWatch": 0,
      "Tax": 0,
      "AWS Key Management Service": 0,
      "AWS Service Catalog": 0,
      "Amazon Simple Email Service": 0.22,
      "Amazon Simple Notification Service": 0,
      "Amazon Simple Storage Service": 0
    }
  }
}

Slack Integration

You can also get the output as a slack message

aws-cost --slack-token [token] --slack-channel [channel]

You will get the message on slack with the breakdown:

Slack Usage

You can set up a GitHub workflow similar to this which can send the daily cost breakdown to Slack.

Note

Regarding the credentials, you need to have the following permissions in order to use the CLI:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": [
        "iam:ListAccountAliases",
        "ce:GetCostAndUsage"
      ],
      "Resource": "*"
    }
  ]
}

Also, please note that this tool uses AWS Cost Explorer under the hood which costs $0.01 per request.

License

MIT © Kamran Ahmed

aws-cost-cli's People

Contributors

aavileli avatar kamranahmedse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-cost-cli's Issues

Support authentication from IAM Roles

Feature Request

Is your feature request related to a problem? Please describe.

aws-cost is unable to run on accounts that authenticate via IAM Role-based authentication.

For example:

[profile marketingadmin]
role_arn = arn:aws:iam::123456789012:role/marketingadminrole
source_profile = user1

Describe the solution you'd like

aws-cost is able to run with IAM Role based authentication.

Describe alternatives you've considered

I've tried setting the AWS AUTH ENV Vars which I get post-authenticating from the IAM Role but it still doesn't work

AWS_PROFILE=
AWS_REGION=
AWS_SECRET_ACCESS_KEY=
AWS_ACCESS_KEY_ID=
AWS_SESSION_TOKEN=

It gives the same error

    Error:
    
    Could not find the AWS credentials in the following files for the profile "marketingadmin":
      ~/.aws/credentials
      ~/.aws/config

    If the config files exist at different locations, set the following environment variables:
      AWS_SHARED_CREDENTIALS_FILE
      AWS_CONFIG_FILE

    You can also configure the credentials via the following command:
      aws configure --profile superdev

    You can also provide the credentials via the following options:
      --access-key
      --secret-key
      --region
    

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

npm package is not the same version as source and returns an error

Source is tagged with v0.2.5
Package is tagged with v0.2.4

When running the package (from npm install -g aws-cost-cli) I get a credential error:

InvalidClientTokenId: The security token included in the request is invalid

Running aws sts get-caller-identity is successful, so my credentials are valid.

However, if I build from source and then run locally:

npm run build
node ./bin/index.js

The tool works and I get my account costs.

I published a test package to npm using the latest source code and it worked, so seems to me the fix is to push the changes in v0.2.5 to npm.

Add columns to display the difference (delta) for Yesterday's costs in both percentage and absolute values.

Feature Request

Is your feature request related to a problem? Please describe.
I'd like to begin by saying thank you for this tool! We use it daily to get a cost report in our Teams channel. We often find ourselves looking at just the summary (Especially the Yesterday field since it tracks daily cost) and notice increases or decreases often, which leads us to trying to navigate the detailed cost report in order to track which service caused the increase/decrease in costs.

Describe the solution you'd like
It would be extremely helpful if we could automatically compute & display a delta (difference) on the daily cost, especially since most aws resources are billed per hour, so daily cost is constant. For 2 consecutive daily executions of the report, it would look like:

...
Yesterday: $743

...
Yesterday: $669 (-10%)

and then in the per-service breakdown we could do the same (including a percentage delta):
Service Last Month This Month Last 7 Days Yesterday
Amazon OpenSearch Service $8071.72 $5599.53 $445.28 $62.68 (down/up +/- X%)

Something like this would help us react much quicker to cost changes.

Describe alternatives you've considered
For now the only alternative I can think of is using a script to fetch the last message from our teams chat and doing a diff between today's and yesterday's output and manipulating today's message, which is very ugly.

Are you willing to resolve this issue by submitting a Pull Request?

  • No, I don't have the time and I wouldn't even know how to start.

Thank you!

AWS SDK for Javascript v3 Upgrade

Feature Request

Is your feature request related to a problem? Please describe.
There is a warning when running aws-cost that AWS SDK for Javascript v2 will be entering maintenance mode in 2023.

Describe the solution you'd like
Upgrade to AWS SDK for Javascript v3. Having a quick look through the codebase this shouldn't be a massive undertaking but does depend on experience with v3, especially in Typescript given it's a lot more modular now. This may also help to resolve #5 and #6 as I believe v3 has better support for using environment variables to load AWS credentials.

Describe alternatives you've considered
N/A

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

I may well have the time at some point so I may try and get a PR up at some point but not committing to anything just now.

Inaccessible host: `ce.ap-southeast-1.amazonaws.com` at port `undefined`

Current Behavior

aws-cost --profile NAME

Expected Behavior

Display the cost of the profile NAME

Debug log

Getting pricing data/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/event_listeners.js:611
        this.response.error = AWS.util.error(new Error(message), {
                                             ^

UnknownEndpoint: Inaccessible host: `ce.ap-southeast-1.amazonaws.com' at port `undefined'. This service may not be available in the `ap-southeast-1' region.
    at Request.ENOTFOUND_ERROR (/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/event_listeners.js:611:46)
    at Request.callListeners (/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/request.js:686:14)
    at error (/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/event_listeners.js:443:22)
    at ClientRequest.<anonymous> (/opt/homebrew/lib/node_modules/aws-cost-cli/node_modules/aws-sdk/lib/http/node.js:99:9)
    at ClientRequest.emit (node:events:513:28)
    at ClientRequest.emit (node:domain:489:12)
    at TLSSocket.socketErrorListener (node:_http_client:494:9)
    at TLSSocket.emit (node:events:513:28) {
  code: 'UnknownEndpoint',
  region: 'ap-southeast-1',
  hostname: 'ce.ap-southeast-1.amazonaws.com',
  retryable: true,
  originalError: Error: getaddrinfo ENOTFOUND ce.ap-southeast-1.amazonaws.com
      at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
    errno: -3008,
    code: 'NetworkingError',
    syscall: 'getaddrinfo',
    hostname: 'ce.ap-southeast-1.amazonaws.com',
    region: 'ap-southeast-1',
    retryable: true,
    time: 2023-01-23T02:18:55.422Z
  },
  time: 2023-01-23T02:18:55.422Z
}

Node.js v18.11.0

Add support to SSO login

Feature Request

Is your feature request related to a problem? Please describe.

I want to use SSO login instead of configuring ~/.aws/credentials and ~/.aws/config

Describe the solution you'd like

Add support for SSO login

Describe alternatives you've considered

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Support environment variables

Feature Request

Please make a support of env vars as many other software:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_REGION

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Add support for aws organizations

Feature Request

Is your feature request related to a problem? Please describe.

Not Really

Describe the solution you'd like

Would like to be able to get a cost summary of all of the accounts within our AWS Organization account posted into slack each day.

Describe alternatives you've considered

Not looked into other options

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Enable testing functions

Feature Request

Is your feature request related to a problem? Please describe.

I need tests before adding features to this tool

Describe the solution you'd like

  • add testing tool as dev dependency
  • enable it to run on CI

Describe alternatives you've considered

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

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.