aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
239 stars 79 forks source link

Get-CECostUsage group by tag key "Name" does not report back tag values #312

Closed crAZiAc closed 1 year ago

crAZiAc commented 1 year ago

Describe the bug

Given this piece of code:

  $period = New-Object Amazon.CostExplorer.Model.DateInterval 
 $period.Start = [String]::Format("{0}-{1:d2}-{2:d2}", 2022, 11, 1)
    $period.End = [String]::Format("{0}-{1:d2}-{2:d2}", 2022, 11, 30)

$metric = @("UnblendedCost", "UsageQuantity")

    $group1 = New-Object Amazon.CostExplorer.Model.GroupDefinition
    $group1.Type = [Amazon.CostExplorer.GroupDefinitionType]::TAG
    $group1.Key = "Name"

    $group2 = New-Object Amazon.CostExplorer.Model.GroupDefinition
    $group2.Type = [Amazon.CostExplorer.GroupDefinitionType]::DIMENSION
    $group2.Key = "LINKED_ACCOUNT"

    $groups = @(
        $group2
        $group1
    )

    $dimension = New-Object Amazon.CostExplorer.Model.DimensionValues
    $dimension.Key = "SERVICE"
    $dimension.Values = "Amazon Elastic Compute Cloud - Compute"

    $filter = New-Object Amazon.CostExplorer.Model.Expression
    $filter.Dimensions = $dimension

        $costUsage = Get-CECostAndUsage `
            -TimePeriod $period `
            -Granularity MONTHLY `
            -Metric $metric `
            -GroupBy $groups `
            -Filter $filter`

The value for the $costUsage.ResultsByTime[0].Groups[0].Keys is

Name$, with no names for the tag value for tags with the name "Name"

Expected Behavior

A cost usage output grouped by linked_account and by tag values on the tag called "Name"

Current Behavior

The cost usage output is only grouped on LINKED_ACCOUNT

Reproduction Steps

In PowerShell, run this piece of code:

 $period = New-Object Amazon.CostExplorer.Model.DateInterval 
 $period.Start = [String]::Format("{0}-{1:d2}-{2:d2}", 2022, 11, 1)
    $period.End = [String]::Format("{0}-{1:d2}-{2:d2}", 2022, 11, 30)
$metric = @("UnblendedCost", "UsageQuantity")

    $group1 = New-Object Amazon.CostExplorer.Model.GroupDefinition
    $group1.Type = [Amazon.CostExplorer.GroupDefinitionType]::TAG
    $group1.Key = "Name"

    $group2 = New-Object Amazon.CostExplorer.Model.GroupDefinition
    $group2.Type = [Amazon.CostExplorer.GroupDefinitionType]::DIMENSION
    $group2.Key = "LINKED_ACCOUNT"

    $groups = @(
        $group2
        $group1
    )

    $dimension = New-Object Amazon.CostExplorer.Model.DimensionValues
    $dimension.Key = "SERVICE"
    $dimension.Values = "Amazon Elastic Compute Cloud - Compute"

    $filter = New-Object Amazon.CostExplorer.Model.Expression
    $filter.Dimensions = $dimension

        $costUsage = Get-CECostAndUsage `
            -TimePeriod $period `
            -Granularity MONTHLY `
            -Metric $metric `
            -GroupBy $groups `
            -Filter $filter`

Possible Solution

No suggestions

Additional Information/Context

No response

AWS Tools for PowerShell version used

ModuleType Version PreRelease Name ExportedCommands


Binary 4.1.95 AWSPowerShell.NetCore {Add-AARCustomDomain, Add-AARResourceTag, Add-AASScalableTarget, Add-ABCResourceTag…}

PowerShell version used

Name Value


PSVersion 7.3.2 PSEdition Core GitCommitId 7.3.2 OS Microsoft Windows 10.0.22000 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

Operating System and version

Windows 11

ashishdhingra commented 1 year ago

@crAZiAc Thanks for reporting the issue. Get-CECostUsage CmdLet is autogenerated and internally uses AWSSDK.CostExplorer package. The AWSSDK.CostExplorer package is auto-generated from service models and is just a wrapper for invoking service operations, GetCostAndUsage in this case.

Could you please share the following:

Thanks, Ashish

crAZiAc commented 1 year ago

Hi Ashish,

Please be aware that I am assuming an IAM role from another account.

Here's the verbose log (stripped of sensitive details:

Amazon Information: 0 : Resolved DefaultConfigurationMode for RegionEndpoint [us-east-2] to [Legacy].
Amazon Information: 1 : Resolved DefaultConfigurationMode for RegionEndpoint [us-east-2] to [Legacy].
Amazon Verbose: 0 : Received response (truncated to 1024 bytes): [<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
  <AssumeRoleResult>
    <AssumedRoleUser>
      <AssumedRoleId>REDACTED</AssumedRoleId>
      <Arn>REDACTED</Arn>
    </AssumedRoleUser>
    <Credentials>
      <AccessKeyId>REDACTED</AccessKeyId>
      <SecretAccessKey>REDACTED</SecretAccessKey>
      <SessionToken>REDACTED</SessionToken>
      <Expiration>2023-02-10T20:39:33Z</Expiration>
    </Credentials>
  </AssumeRoleRes]
Amazon Information: 1 : Request metrics: AsyncCall = False; CanonicalRequest = POST\n/\n\ncontent-type:application/x-www-form-urlencoded; charset=utf-8\nhost:sts.amazonaws.com\nuser-agent:AWSPowerShell.NetCore/4.1.95.0 .NET_Core/7.0.2 OS/Microsoft_Windows_10.0.22000 PowerShellCore/2023.-1 ClientSync\nx-amz-content-sha256:00228ec32d1b62d122ae08761227e131864c2539eaccd69f884bcf403e73388c\nx-amz-date:20230210T193933Z\n\ncontent-type;host;user-agent;x-amz-content-sha256;x-amz-date\n00228ec32d1b62d122ae08761227e131864c2539eaccd69f884bcf403e73388c; StringToSign = AWS4-HMAC-SHA256\n20230210T193933Z\n20230210/us-east-1/sts/aws4_request\nbd8d357062ab2e4ca1d27cc4b877d48adb43ec71b1d442c9a315473fd63e8b15; ServiceName = Amazon.SecurityToken; ServiceEndpoint = https://sts.amazonaws.com/; MethodName = AssumeRoleRequest; RequestSize = 202; StatusCode = OK; BytesProcessed = 1158; AWSRequestID = 7cb42514-8375-4872-bae3-1b33319f93a1; CredentialsRequestTime = 00:00:00.0000023; RequestSigningTime = 00:00:00.0004518; HttpRequestTime = 00:00:00.0208602; ResponseUnmarshallTime = 00:00:00.0002054; ResponseProcessingTime = 00:00:00.0038268; ClientExecuteTime = 00:00:00.0284649; 
Amazon Verbose: 0 : Received response (truncated to 1024 bytes): REDACTED
Amazon Information: 1 : Request metrics: AsyncCall = True; CanonicalRequest = POST\n/\n\ncontent-type:application/x-amz-json-1.1\nhost:ce.us-east-1.amazonaws.com\nuser-agent:AWSPowerShell.NetCore/4.1.95.0 .NET_Core/7.0.2 OS/Microsoft_Windows_10.0.22000 PowerShellCore/2023.-1 ClientAsync\nx-amz-api-version:2017-10-25\nx-amz-content-sha256:2605b91435f5e2198831423fd11c99bd0f5b2a1aa13d0c6ce63dd9ef75a47b80\nx-amz-date:20230210T193933Z\nx-amz-security-token:REDACTED\nx-amz-target:AWSInsightsIndexService.GetCostAndUsage\n\ncontent-type;host;user-agent;x-amz-api-version;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-target\n2605b91435f5e2198831423fd11c99bd0f5b2a1aa13d0c6ce63dd9ef75a47b80; StringToSign = AWS4-HMAC-SHA256\n20230210T193933Z\n20230210/us-east-1/ce/aws4_request\nfd990048784227f616c2001f5349611255aa165028e868b8f3657e09a0f39391; ServiceName = Amazon.CostExplorer; ServiceEndpoint = https://ce.us-east-1.amazonaws.com/; MethodName = GetCostAndUsageRequest; RequestSize = 308; StatusCode = OK; BytesProcessed = 31995; AWSRequestID = 42f9865a-2706-4a71-be33-471589379643; CredentialsRequestTime = 00:00:00.0752087; RequestSigningTime = 00:00:00.0003744; HttpRequestTime = 00:00:00.2380976; ResponseUnmarshallTime = 00:00:00.0160191; ResponseProcessingTime = 00:00:00.0178098; ClientExecuteTime = 00:00:00.3328773;

This scenario also does not work with AWS CLI. Here's the command:

aws ce get-cost-and-usage --profile targetrole --time-period "Start=2022-12-01,End=2022-12-31" --granularity "MONTHLY" --metrics "UnblendedCost" --group-by "Type=TAG,Key=Name"

Here's the response to command:

{
    "GroupDefinitions": [
        {
            "Type": "TAG",
            "Key": "Name"
        }
    ],
    "ResultsByTime": [
        {
            "TimePeriod": {
                "Start": "2022-12-01",
                "End": "2022-12-31"
            },
            "Total": {},
            "Groups": [
                {
                    "Keys": [
                        "Name$"
                    ],
                    "Metrics": {
                        "UnblendedCost": {
                            "Amount": "371146.1287015752",
                            "Unit": "USD"
                        }
                    }
                }
            ],
            "Estimated": false
        }
    ],
    "DimensionValueAttributes": []
}

The AWS console will give the exact same output as above.

ashishdhingra commented 1 year ago

@crAZiAc Few other questions:

Based on the information provided by you, this doesn't appear to be SDK issue since:

Thanks, Ashish

github-actions[bot] commented 1 year ago

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.