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?