boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
8.99k stars 1.86k forks source link

get_cost_and_usage_with_resources - Granularity issue #2369

Closed OneMoeLine closed 4 years ago

OneMoeLine commented 4 years ago

Describe the bug Unexpected behavior when using the get_cost_and_usage_with_resources API with granularity of MONTHLY. It seems to default to HOURLY even when granularity is not provided. This is not the case when using the get_cost_and_usage API.

Error: Traceback (most recent call last): File "c:/Users/Student/Documents/aws_api.py", line 23, in response = client.get_cost_and_usage_with_resources( File "C:\Users\Student\AppData\Roaming\Python\Python38\site-packages\botocore\client.py", line 316, in _api_call return self._make_api_call(operation_name, kwargs) File "C:\Users\Student\AppData\Roaming\Python\Python38\site-packages\botocore\client.py", line 626, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetCostAndUsageWithResources operation: Start date is more than 14 days ago for hourly granularity. Please adjust start date as we support only 14 days of data at hourly granularity.

Steps to reproduce `import boto3 import pprint

client = boto3.client('ce')

response = client.get_cost_and_usage_with_resources( Granularity='DAILY', Metrics=[ 'AmortizedCost', 'UsageQuantity' ], TimePeriod={ 'Start': '2020-02-01', 'End': '2020-04-01' }, )

pprint.pprint(response)`

Expected behavior I expected to be able to set granularity to monthly for the period.

Debug logs 2020-04-02 14:56:12,374 botocore.hooks [DEBUG] Event before-parameter-build.cost-explorer.GetCostAndUsageWithResources: calling handler <function generate_idempotent_uuid at 0x033A4A48> 2020-04-02 14:56:12,375 botocore.hooks [DEBUG] Event before-call.cost-explorer.GetCostAndUsageWithResources: calling handler <function inject_api_version_header_if_needed at 0x033AC7C0> 2020-04-02 14:56:12,386 botocore.endpoint [DEBUG] Making request for OperationModel(name=GetCostAndUsageWithResources) with params: {'url_path': '/', 'query_string': '', 'method': 'POST', 'headers': {'X-Amz-Target': 'AWSInsightsIndexService.GetCostAndUsageWithResources', 'Content-Type': 'application/x-amz-json-1.1', 'User-Agent': 'Boto3/1.12.34 Python/3.8.1 Windows/10 Botocore/1.15.34'}, 'body': b'{"Granularity": "DAILY", "Metrics": ["AmortizedCost", "UsageQuantity"], "TimePeriod": {"Start": "2020-02-01", "End": "2020-04-01"}}', 'url': 'https://ce.us-east-1.amazonaws.com/', 'context': {'client_region': 'aws-global', 'client_config': <botocore.config.Config object at 0x0377A820>, 'has_streaming_input': False, 'auth_type': None}} 2020-04-02 14:56:12,388 botocore.hooks [DEBUG] Event request-created.cost-explorer.GetCostAndUsageWithResources: calling handler <bound method RequestSigner.handler of <botocore.signers.RequestSigner object at 0x0377A730>> 2020-04-02 14:56:12,389 botocore.hooks [DEBUG] Event choose-signer.cost-explorer.GetCostAndUsageWithResources: calling handler <function set_operation_specific_signer at 0x033A49B8> 2020-04-02 14:56:12,392 botocore.auth [DEBUG] Calculating signature using v4 auth. 2020-04-02 14:56:12,409 botocore.auth [DEBUG] CanonicalRequest: POST /

content-type:application/x-amz-json-1.1 host:ce.us-east-1.amazonaws.com x-amz-date:20200402T185612Z x-amz-target:AWSInsightsIndexService.GetCostAndUsageWithResources

content-type;host;x-amz-date;x-amz-target 740dbe86c78f7b1d65d445521086903f0255e796c6265b5dbdddf0f218b25da1 2020-04-02 14:56:12,414 botocore.auth [DEBUG] StringToSign: AWS4-HMAC-SHA256 20200402T185612Z 20200402/us-east-1/ce/aws4_request 56bcff60106a595f5ce7da95eddde7c9ff828293c2dbb7fbeaefcd3d4c2885b4 2020-04-02 14:56:12,416 botocore.auth [DEBUG] Signature: 3ac309274cb415f38482d3709853eb5f9d04fa9e3ad83223b0cb256cbd5f30e6 2020-04-02 14:56:12,419 botocore.endpoint [DEBUG] Sending http request: <AWSPreparedRequest stream_output=False, method=POST, url=https://ce.us-east-1.amazonaws.com/, headers={'X-Amz-Target': b'AWSInsightsIndexService.GetCostAndUsageWithResources', 'Content-Type': b'application/x-amz-json-1.1', 'User-Agent': b'Boto3/1.12.34 Python/3.8.1 Windows/10 Botocore/1.15.34', 'X-Amz-Date': b'20200402T185612Z', 'Authorization': b'AWS4-HMAC-SHA256 Credential=AKIAJZYGINOE7WCIQQLA/20200402/us-east-1/ce/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=3ac309274cb415f38482d3709853eb5f9d04fa9e3ad83223b0cb256cbd5f30e6', 'Content-Length': '131'}> 2020-04-02 14:56:12,421 urllib3.connectionpool [DEBUG] Starting new HTTPS connection (1): ce.us-east-1.amazonaws.com:443 2020-04-02 14:56:12,592 urllib3.connectionpool [DEBUG] https://ce.us-east-1.amazonaws.com:443 "POST / HTTP/1.1" 400 187 2020-04-02 14:56:12,593 botocore.parsers [DEBUG] Response headers: {'Date': 'Thu, 02 Apr 2020 18:56:14 GMT', 'Content-Type': 'application/x-amz-json-1.1', 'Content-Length': '187', 'Connection': 'keep-alive', 'x-amzn-RequestId': 'b141ca47-4fe2-41c5-83cd-ffa4f376b82e'} 2020-04-02 14:56:12,602 botocore.parsers [DEBUG] Response body: b'{"__type":"ValidationException","message":"Start date is more than 14 days ago for hourly granularity. Please adjust start date as we support only 14 days of data at hourly granularity."}' 2020-04-02 14:56:12,606 botocore.hooks [DEBUG] Event needs-retry.cost-explorer.GetCostAndUsageWithResources: calling handler <botocore.retryhandler.RetryHandler object at 0x0377ACE8> 2020-04-02 14:56:12,607 botocore.retryhandler [DEBUG] No retry needed. Traceback (most recent call last): File "c:/Users/Student/Documents/aws_api.py", line 23, in response = client.get_cost_and_usage_with_resources( File "C:\Users\Student\AppData\Roaming\Python\Python38\site-packages\botocore\client.py", line 316, in _api_call return self._make_api_call(operation_name, kwargs) File "C:\Users\Student\AppData\Roaming\Python\Python38\site-packages\botocore\client.py", line 626, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetCostAndUsageWithResources operation: Start date is more than 14 days ago for hourly granularity. Please adjust start date as we support only 14 days of data at hourly granularity.

swetashre commented 4 years ago

@tbm4xd - Thank you for your post. According to the documentation the start date cannot be earlier than 14 days ago. It is irrespective of granularity.

I agree the error is misleading. Currently start date can't be earlier than 14 days ago irrespective of type of granularity. I have already contacted the service team about the issue.

Hope it helps.

OneMoeLine commented 4 years ago

@swetashre Thanks that makes sense. I missed that in the documentation :)

swetashre commented 4 years ago

@tbm4xd - I am glad i could help. Closing this issue as the issue has been resolved.

tf42src commented 2 months ago

Having the same issue. @swetashre does it make sense to limit the range to 14 days but allow Granularity="MONTHLY" ?

Also the documentation for GRANULARITY is misleading [1]:

Granularity (string) –
[REQUIRED]
Sets the Amazon Web Services cost granularity to MONTHLY, DAILY, or HOURLY. If Granularity isn’t set, the response object doesn’t include the Granularity, MONTHLY, DAILY, or HOURLY.

How can Granularity not be set and REQUIRED at the same time?

Also, this hasn't been addressed for 4 years?

[1] https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ce/client/get_cost_and_usage_with_resources.html