using boto3 to get usage data of a user based on tag value

65 views Asked by At

Im trying to use boto3 w cost explorer to get usage data of user based on tag value. but It keeps timing out. Is this below script correct? There are no errors but I see no output

import boto3

ce_client = boto3.client('ce')

# Granularity='DAILY'|'MONTHLY'|'HOURLY'

time_period = {
    'Start': '2023-05-05',
    'End': '2023-06-06'
}

# Specify the filter for the tag key ('User') and tag value ('sample')
tag_filter = {
    'Tags': {
            'Key': 'User',
            'Values': [
                'sample',
            ]
            }
            }

# Retrieve cost and usage data based on the specified tag filter
response = ce_client.get_cost_and_usage(
    TimePeriod=time_period,
    Granularity='MONTHLY',
    Metrics=['BlendedCost'],
    Filter=tag_filter
)

# Process the response data as needed
print(response)  # Example: Print the response (adjust as per your requirement)


#print("EC2 Instance IDs:", instance_ids) 

altrernatively ive tried to use AWS CLI to do this but it keeps stalling and timing out. this is what i used

aws ce get-cost-and-usage \
    --time-period Start=2023-05-01,End=2023-06-01 \
    --granularity MONTHLY \
    --metrics "BlendedCost" \
    --filter "Tags.Key=User,Values=sample"

thanks in advance!

0

There are 0 answers