0

Suppose I have organization and I want to get the cost as per account wise. How can I get it?

I am using aws-sdk for nodejs. how can I List all account and get the cost account wise?

I am using AWS lambda for this. Any help would highly be appreciated.

I am trying to use below with an single account but I am not able to set Dimension, AND I am helpless with online resource

var params = {
  Granularity: DAILY | MONTHLY | HOURLY, /* required */
  Metric: BLENDED_COST | UNBLENDED_COST | AMORTIZED_COST | NET_UNBLENDED_COST | NET_AMORTIZED_COST | USAGE_QUANTITY | NORMALIZED_USAGE_AMOUNT, /* required */
  TimePeriod: { /* required */
    End: 'STRING_VALUE', /* required */
    Start: 'STRING_VALUE' /* required */
  },
  Filter: { /* Expression */
    And: [
      /* recursive Expression */,
      /* more items */
    ],
    CostCategories: {
      Key: 'STRING_VALUE',
      MatchOptions: [
        EQUALS | ABSENT | STARTS_WITH | ENDS_WITH | CONTAINS | CASE_SENSITIVE | CASE_INSENSITIVE,
        /* more items */
      ],
      Values: [
        'STRING_VALUE',
        /* more items */
      ]
    },
    Dimensions: {
      Key: AZ | INSTANCE_TYPE | LINKED_ACCOUNT | LINKED_ACCOUNT_NAME | OPERATION | PURCHASE_TYPE | REGION | SERVICE | SERVICE_CODE | USAGE_TYPE | USAGE_TYPE_GROUP | RECORD_TYPE | OPERATING_SYSTEM | TENANCY | SCOPE | PLATFORM | SUBSCRIPTION_ID | LEGAL_ENTITY_NAME | DEPLOYMENT_OPTION | DATABASE_ENGINE | CACHE_ENGINE | INSTANCE_TYPE_FAMILY | BILLING_ENTITY | RESERVATION_ID | RESOURCE_ID | RIGHTSIZING_TYPE | SAVINGS_PLANS_TYPE | SAVINGS_PLAN_ARN | PAYMENT_OPTION | AGREEMENT_END_DATE_TIME_AFTER | AGREEMENT_END_DATE_TIME_BEFORE,
      MatchOptions: [
        EQUALS | ABSENT | STARTS_WITH | ENDS_WITH | CONTAINS | CASE_SENSITIVE | CASE_INSENSITIVE,
        /* more items */
      ],
      Values: [
        'STRING_VALUE',
        /* more items */
      ]
    },
    Not: /* recursive Expression */,
    Or: [
      /* recursive Expression */,
      /* more items */
    ],
    Tags: {
      Key: 'STRING_VALUE',
      MatchOptions: [
        EQUALS | ABSENT | STARTS_WITH | ENDS_WITH | CONTAINS | CASE_SENSITIVE | CASE_INSENSITIVE,
        /* more items */
      ],
      Values: [
        'STRING_VALUE',
        /* more items */
      ]
    }
  },
  PredictionIntervalLevel: 'NUMBER_VALUE'
};
costexplorer.getCostForecast(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
}); 
August Dev
  • 21
  • 6
  • It looks like you copied the SDK documentation into your app? Does that even run without errors? You need to take the time to actually look at the parameters, and set the values on the parameters you want, instead of copy/pasting entire documentation into your code. Like the dimension should be either `LINKED_ACCOUNT` or `LINKED_ACCOUNT_NAME` to get the costs for a specific account. – Mark B Mar 30 '21 at 16:31
  • Gets me this error ```ValidationException: Dimensions expression must have key and values set\n``` – August Dev Mar 30 '21 at 17:22

2 Answers2

1

Try this code...make sure you have the credentials file in (~/.aws/credentials) or follow the steps in https://stackoverflow.com/a/67059810/10214650

report.js

var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});

var costexplorer = new AWS.CostExplorer({apiVersion: '2017-10-25'});
var params = {
  Metrics: [ 
    'UNBLENDED_COST',
 ],
  TimePeriod: { 
    End: '', /* Fill the end date */
    Start: '' /* Fill the start date  */
  },
  Granularity: 'DAILY', /* MONTHLY or HOURLY */
  GroupBy: [
    {
      Key: 'LINKED_ACCOUNT',
      Type: 'DIMENSION' 
    },
  ]
};
costexplorer.getCostAndUsage(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

This code gives the cost report of the various accounts in your org.

wolverine
  • 74
  • 6
-1

As per the comments on the original question, it appears you are directly copying in the official AWS API documentation.

If you aren't comfortable with using the AWS Cost Explorer APIs, one free and managed solution to getting cost per account would be to sign up for a Vantage account. When you connect your Master AWS account with Vantage, it will automatically import all Member Accounts by default. From there, if you navigate to their "accrued cost" portion of their console you can filter costs down by member account.

As a disclaimer, I work at Vantage and personally have spent months working with Cost Explorer APIs and they tend to be fairly complex if you don't know what you're doing so I thought I'd recommend this as an alternative solution.