title: How to get AWS account/service cost using CloudWatch API?
body: <p>I am aware that in order to get the detailed cost of an AWS account, I need to enable programmatic billing access and push the billing report to an S3 bucket in the respective AWS account.</p>
  
  <p>This approach works fine for me. However, the problem is the bill size to be downloaded and processed is huge to just get the account and service-level cost of all linked AWS accounts for the past months.</p>
  
  <p>I am trying to explore the AWS CloudWatch APIs to get the linked account level cost. I am trying to get the list of all linked accounts and the services being consumed by them using the following command:</p>
  
  <pre><code>aws cloudwatch list-metrics --namespace "AWS/Billing"
  </code></pre>
  
  <p>which works exactly as expected and returns the result correctly. However, when trying to get the cost of these accounts using the following command:</p>
  
  <pre><code>aws cloudwatch get-metric-statistics --namespace "AWS/Billing"
  --dimensions "Name=LinkedAccount,Value=&lt;linked-account-id&gt;" --metric-name=EstimatedCharges --start-time=2017-10-01T19:30:00Z --end-time=2017-10-10T19:30:00Z --period=3600 --statistics=Sum
  </code></pre>
  
  <p>I get an almost empty JSON of the following form:</p>
  
  <pre><code>{
      "Datapoints": [],
      "Label": "EstimatedCharges"
  }
  </code></pre>
  
  <p>I have ensured that I am using the right linked account ID and the other parameters being provided are as it is mentioned above. What am I missing here to get the account or service level costs?</p>
  
  <p><strong>Edit 1:</strong> I have set the region in my config file to us-east-1 since the region is a mandatory field for the commands to work. Is this somehow the cause? If yes, what is the fix/workaround?</p>
  
  <p><strong>Update 1:</strong> I am able to get the overall AWS account cost (primary account cost + linked account cost), using the above API, by just tweaking the dimensions, but am still not able to get the individual account/service cost (individual costs of primary and linked accounts).</p>
  
  <p><strong>Update 2:</strong> I am able to get linked account cost when I use an IAM user from the account whose cost I need. However, the <code>LinkedAccount</code> dimension is still not working: When I use the primary account's IAM user to get the cost of its linked accounts using the <code>LinkedAccount</code> dimension, it still returns an empty result. My expectation was, since I can get the linked accounts' costs in the bills of the primary account and I can see the linked accounts' costs when I log into AWS portal, I should be able to get the linked accounts' costs using the CloudWatch APIs. Still trying to get a solution for getting linked accounts' costs using the primary account's IAM user using CW APIs.</p>