Search code examples
amazon-web-servicesboto3data-transfer

How to get aws data transfer cost?


How can I get all data transfer costs from my aws account? is there any boto3 api that would support it? PS- I require all kinds of data transfer costs, for all regions.


Solution

  • You can use the Boto3 - Cost Explorer library.

    Specify the time period and filters you want to apply. In your case, to get Data Transfer cost across all regions the code should be similar to this (change parameters accordingly):

    import boto3
    
    client = boto3.client('ce')
    
    
    def lambda_handler(event, context):
    
        response = client.get_cost_and_usage(
            TimePeriod={
                'Start': '2021-06-01',
                'End': '2021-12-01'
            },
            Filter={
                'Dimensions': {
                    'Key': 'USAGE_TYPE_GROUP',
                    'Values': [
                        'EC2: Data Transfer - Internet (Out)'
                    ],
                    'MatchOptions': [
                        'EQUALS',
                    ]
                }
            },
            Granularity='MONTHLY',
            Metrics=['UnblendedCost']
        )
    
        print(response)