Cost analysis by resource
Set up automatic export of the expense details for your billing account and generate a report with information about the costs associated with the resources within a folder. The data will be retrieved from the Object Storage bucket using the S3 Select query language.
To analyze costs:
If you no longer need the resources you created, delete them.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of analyzing expense details includes fees for bucket data storage and operations with data (see Object Storage pricing).
Configure the environment
Install and configure the AWS CLI.
Create an Object Storage bucket
- In the management console
, select the folder where you want to create a bucket. - In the list of services, select Object Storage.
- At the top right, click Create bucket.
- In the ** Name** field, enter a name for the bucket.
- Click Create bucket.
Get expense details
-
Go to Yandex Cloud Billing
. -
Select the billing account you need from the list.
-
Go to the Usage details tab.
-
At the top right, click Export details and select Periodic export.
-
In the window that opens:
- In the Bucket field, enter the name of the bucket you created earlier. A CSV file with your expense details will be kept there.
- In the Directory field, enter the name of the directory for the file. The last character must be
/
. - In the Language field, select the language to display product names in: English or Русский.
- In the Usage details field, select the Resource type of details.
-
Click Create.
Generate a report
-
Run the query against Object Storage and save the output to a file:
-
For convenience, save:
-
Name of the bucket containing the details, to a variable named
$bucket
:bucket=<bucket_name>
-
Key of the object containing the details, to a variable named
$key
:key=<object_key>
-
S3 Select expression to request the data from the details file, to a variable named
$query
:query="select service_name,resource_id,sku_id,sku_name,\"date\",cost from S3Object where service_name='Compute Cloud'"
-
-
Run this command:
aws --endpoint https://storage.yandexcloud.net s3api select-object-content \ --bucket $bucket \ --key $key \ --expression "$query" \ --expression-type 'SQL' \ --input-serialization 'CSV={FileHeaderInfo=USE,FieldDelimiter=,}' \ --output-serialization 'CSV={}' \ "output.csv"
-
-
To output the total cost for each resource, run an aggregation query using the
awk
utility:< output.csv awk -F, '{n[$2]+=$6} END{for(i in n){print n[i] "\t" i}}' | sort -nrk1
Result:
591.206 c8rska9493********** 254.092 c8rk36jv5n********** 93.4481 a7l9vsb1bt********** ... 0.742366 fdv0u8gkl********** 0.694901 c8rhi3km7********** 0.545707 a7l7fb0at**********
How to delete the resources you created
To shut down the created resources and stop paying for them: