0

I am preparing a script that can fetch me the cost of my AWS account. Here I have used Python3 and boto3 SDK by AWS. But for further granularity, I also want to fetch region-wise cost for each service. I am unable to get a good doc for this. Following is the currently working code.

cost_and_usage = client.get_cost_and_usage(
    TimePeriod={
        "Start": START_DATE,
        "End": END_DATE
    },
    Granularity="DAILY",
    Metrics=["UnblendedCost"],
    GroupBy=[
        {
            "Type": "DIMENSION",
            "Key": "LINKED_ACCOUNT",
        },
        {
            "Type": "DIMENSION",
            "Key": "SERVICE",
        }
    ]
)

What changes should I make in the code that I will get the cost for every service by region.

Atharva Kale
  • 59
  • 1
  • 9
  • 1
    You can only do 2 vectors in the groupby section as far as I know. My suggestion is to separate it to 2 queries, So run one query to get the linked accounts, and in the loop just change the "Linked_Account" to Region, and run a loop for each linked account as a filter, and not a "groupby". This should allow you to create a data structure to store all the data based on each account, region and service – Dvir669 Jul 27 '21 at 18:48
  • I have only one "LINKED_ACCOUNT", so if I remove "REGION" will that make any impact? – Atharva Kale Jul 28 '21 at 07:52
  • so why would you need to group by the linked account? – Dvir669 Jul 28 '21 at 19:36
  • @Dvir669 when I tried with `REGION`, it simply did not show all the intervals I asked. It only showed few time periods. While in `LINKED_ACCOUNT`, it shows all the intervals asked. – Atharva Kale Aug 02 '21 at 06:17

0 Answers0