Bootstrap FreeKB - Amazon Web Services (AWS) - Get Cost and Usage using the AWS CLI
Amazon Web Services (AWS) - Get Cost and Usage using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

The aws ce get-cost-and-usage command can be used to return the total cost for a certain AWS service in a certain time frame. In this example, the S3 total cost was a whopping $0.026 for January of 2024!

~]$ aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-01-31 --granularity MONTHLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment --filter '{"Dimensions": {"Key": "SERVICE","Values": ["Amazon Simple Storage Service"]}}'
{
    "GroupDefinitions": [
        {
            "Type": "DIMENSION",
            "Key": "SERVICE"
        },
        {
            "Type": "TAG",
            "Key": "Environment"
        }
    ],
    "ResultsByTime": [
        {
            "TimePeriod": {
                "Start": "2024-01-01",
                "End": "2024-01-31"
            },
            "Total": {},
            "Groups": [
                {
                    "Keys": [
                        "Amazon Simple Storage Service",
                        "Environment$"
                    ],
                    "Metrics": {
                        "BlendedCost": {
                            "Amount": "0.2621694181",
                            "Unit": "USD"
                        },
                        "UnblendedCost": {
                            "Amount": "0.2621694181",
                            "Unit": "USD"
                        },
                        "UsageQuantity": {
                            "Amount": "481081.9302019198",
                            "Unit": "N/A"
                        }
                    }
                }
            ],
            "Estimated": false
        }
    ],
    "DimensionValueAttributes": []
}

 

And here is an example to get the cost for your t3.small EC2 instances.

aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-03-31 --granularity MONTHLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment --filter '{ "Dimensions": { "Key": "INSTANCE_TYPE", "MatchOptions": ["EQUALS"], "Values": ["t3.small"] }}'

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0b761a in the box below so that we can be sure you are a human.