[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Basic infrastructure](index.md) > Cost analysis by resource using Object Storage

# Cost analysis by resource using Yandex Object Storage

Configure the automatic export of expense details for your [billing account](../../billing/concepts/billing-account.md) and generate a report with info on the costs associated with the resources within a folder. To get data from the Object Storage bucket, we will be using the [S3 Select](../../storage/concepts/s3-select-language.md) query language.

To analyze the costs:

1. [Get your cloud ready](#before-you-begin).
1. [Create an Object Storage bucket](#create-bucket).
1. [Get the expense details](#get-billing-detail).
1. [Generate a report](#create-report).

If you no longer need the resources you created, [delete them](#clear-out).

## Get your cloud ready {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).


### Required paid resources {#paid-resources}

The cost of analyzing expense details includes fees for bucket data storage and data operations (see [Object Storage pricing](../../storage/pricing.md)).


### Set up your environment {#setup-environment}

Install and configure the [AWS CLI](../../storage/tools/aws-cli.md).

## Create an Object Storage bucket {#create-bucket}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder where you want to create a bucket.
  1. Navigate to **Object Storage**.
  1. At the top right, click **Create bucket**.
  1. In the ** Name** field, enter a name for the bucket.
  1. Click **Create bucket**.

{% endlist %}

## Get the expense details {#get-billing-detail}

{% list tabs group=instructions %}

- Management console {#console}

  1. Go to [**Yandex Cloud Billing**](https://center.yandex.cloud/billing/accounts).
  1. Select the billing account you need from the list.
  1. Navigate to the **Usage details** tab.
  1. At the top right, click **Export details** and select **Periodic export**.
  1. In the window that opens:

     * In the **Bucket** field, enter the name of the bucket you [created earlier](#create-bucket). This bucket will store a CSV file with your expense details.
     * In the **Directory** field, enter the directory name for the file. The name must end with `/`.
     * In the **Language** field, select the language to display product names in: **English** or **Русский**.
     * In the **Usage details** field, select ***Resource**.

  1. Click **Create**.

{% endlist %}

## Generate a report {#create-report}

{% list tabs group=programming_language %}

- Bash {#bash}

  1. Run the query against Object Storage and save the output to a file:

     1. For convenience, save:

        * Name of the details bucket to a variable named `$bucket`:

          ```bash
          bucket=<bucket_name>
          ```

        * [Key](../../storage/concepts/object.md#key) of the details object to a variable named `$key`:

          ```bash
          key=<object_key>
          ```

        * Expression to request data from the details file, to the `$query` variable:

          ```bash
          query="select service_name,resource_id,sku_id,sku_name,\"date\",cost from S3Object where service_name='Compute Cloud'"
          ```

     1. Run this command:

        ```bash
        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"
        ```

  1. To output the total cost for each resource, run an aggregation query using `awk`:

     ```bash
     < output.csv awk -F, '{n[$2]+=$6} END{for(i in n){print n[i] "\t" i}}' | sort -nrk1
     ```

     Result:

     ```bash
     591.206 c8rska9493**********
     254.092 c8rk36jv5n**********
     93.4481 a7l9vsb1bt**********
     ...
     0.742366 fdv0u8gkl**********
     0.694901 c8rhi3km7**********
     0.545707 a7l7fb0at**********
     ```

{% endlist %}

## How to delete the resources you created {#clear-out}

To shut down the resources you created and stop paying for them:

1. [Delete](../../storage/operations/objects/delete.md) all objects from the bucket.
1. [Delete](../../storage/operations/buckets/delete.md) the bucket.