1

I'm testing the AWS Cost Explorer API (I'm using the .NET SDK), in particular the GetCostAndUsageWithResources method to get the costs split by resource.

This is the code I'm testing with:

string nextPageToken = null;

do
{
    var costRequest = new GetCostAndUsageWithResourcesRequest()
    {
        Granularity = Granularity.HOURLY,
        GroupBy = {
            new GroupDefinition() {
                Key = "RESOURCE_ID",
                Type = GroupDefinitionType.DIMENSION
            }
        },
        Metrics = { "BlendedCost" },
        NextPageToken = nextPageToken
    };

    var costResponse = await client.GetCostAndUsageWithResourcesAsync(costRequest);
    nextPageToken = costResponse.NextPageToken;

    foreach (var resultByTime in costResponse.ResultsByTime)
    {
        foreach (var instanceGroup in resultByTime.Groups)
        {
            var instanceId = instanceGroup.Keys.First();
            if(g.Keys.First() != "NoResourceId" && !g.Keys.First().StartsWith("i-"))
            {
                Debugger.Break(); //NEVER gets hit
            }
        }
    }

} while (!string.IsNullOrEmpty(nextPageToken));

However, as you can see from the comment in the code, I have an issue: the resource ID (which is the dimension I'm grouping by) seems to only be retrieved correctly for EC2 machine instances (IDs that start with i-). Otherwise, all other results have the ID key set to NoResourceId

What am I doing wrong here? Why does the Cost Explorer API only populate the Resource ID of EC2 instances, and all others are not identified? What if I want to know the costs of all other AWS services, how do I identify to which service the result belongs?

Am I doing something wrong here in the way I invoke the API? What am I missing?

Master_T
  • 7,232
  • 11
  • 72
  • 144
  • Have you tried creating tags for the services? – Devon Bessemer Sep 16 '22 at 16:57
  • @Devon: yes, we have everything tagged, and if I use the tagging API I can retrieve the tags for all services. But I don't think this is tags we're talking about here, bit the actual Id of the resource. – Master_T Sep 17 '22 at 18:35
  • This is expected behaviour & is not wrong: not everything in AWS has a resource ID. What is your actual expected output? Cost split by *service*? What is the problem you are trying to solve? Is there a specific view you can create using the console that you would like to recreate programmatically? – Ermiya Eskandary Sep 18 '22 at 14:39
  • @ErmiyaEskandary: Thanks for your comment. I'm trying to get information on how much I'm paying for various items. EC2 instances are just one of them, but what if I want to know how much I'm paying for an EC2 Volume? Or a backup? Or a S3 bucket? Or a Route53 hosted zone? Can I only know the total cost of the service, is there no way to know how much I'm paying for a single item? – Master_T Sep 19 '22 at 07:22

0 Answers0