Rohan M
Rohan M

Reputation: 1

Getting error while using get_cost_and_usage in lambda with boto

I am trying to get the cost of the previous day using cost explorer while using boto and lambda. But I get and error

" "errorMessage": "An error occurred (ValidationException) when calling the GetCostAndUsage operation: " and the error type is "ClientError".

I have specified the region to us-east-1. Also my policy is

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ce:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

My Code is below

ce = boto3.client('ce')

cost = ce.get_cost_and_usage(TimePeriod={'Start': '2019-7-15', 'End': '2019-7-17'}, Granularity = 'DAILY')

print(cost)

Upvotes: 0

Views: 3514

Answers (1)

Josh Kupershmidt
Josh Kupershmidt

Reputation: 2710

I think you have two problems:

  1. Invalid timestamp formatting, that will cause an error like this:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetCostAndUsage operation: Start time  is invalid. Valid format is: yyyy-MM-dd.
  1. Missing the mandatory Metrics keyword to the get_cost_and_usage() call.

Changing your get_cost_and_usage() call to something like this should work:

cost = ce.get_cost_and_usage(
    TimePeriod={'Start': '2019-07-15', 'End': '2019-07-17'},
    Granularity = 'DAILY',
    Metrics=['UnblendedCost'])

Upvotes: 1

Related Questions