Accessing Cost and Usage Reports
Cost and usage reports are comma-separated value (CSV) files that are generated daily and stored in an Object Storage bucket. This topic describes how to access these reports.
Required IAM Policy
To use Oracle Cloud Infrastructure, you must be granted security access in a policy by an administrator. This access is required whether you're using the Console or the REST API with an SDK, CLI, or other tool. If you get a message that you don’t have permission or are unauthorized, verify with your administrator what type of access you have and which compartment to work in.
To use cost and usage reports, the following policy statement is required:
define tenancy usage-report as ocid1.tenancy.oc1..aaaaaaaaned4fkpkisbwjlr56u7cj63lf3wffbilvqknstgtvzub7vhqkggq endorse group <group> to read objects in tenancy usage-report
Authentication and Authorization
Each service in Oracle Cloud Infrastructure integrates with IAM for authentication and authorization, for all interfaces (the Console, SDK or CLI, and REST API).
An administrator in your organization needs to set up groups , compartments , and policies that control which users can access which services, which resources, and the type of access. For example, the policies control who can create new users, create and manage the cloud network, launch instances, create buckets, download objects, etc. For more information, see Getting Started with Policies. For specific details about writing policies for each of the different services, see Policy Reference.
If you’re a regular user (not an administrator) who needs to use the Oracle Cloud Infrastructure resources that your company owns, contact your administrator to set up a user ID for you. The administrator can confirm which compartment or compartments you should be using.
Using the Console
To download a cost or usage report:
- Open the navigation menu and click Governance and Administration. Under Cost Management, click Cost and Usage Reports.
- Click the report you want to download from the list, and follow your browser's instructions for downloading.
Using the API
For information about using the API and signing requests, see REST APIs and Security Credentials. For information about SDKs, see Software Development Kits and Command Line Interface.
To download a cost or usage report, use the Object Storage APIs. The reports are stored in the tenancy's home region. The Object Storage namespace used for the reports is
bling; the bucket name is the tenancy OCID.
The following example shows how to download a cost report, usage report (or both) using a Python script:
import oci import os # This script downloads all of the cost, usage, (or both) reports for a tenancy (specified in the config file). # # Pre-requisites: Create an IAM policy to endorse users in your tenancy to read cost reports from the OCI tenancy. # # Example policy: # define tenancy reporting as ocid1.tenancy.oc1..aaaaaaaaned4fkpkisbwjlr56u7cj63lf3wffbilvqknstgtvzub7vhqkggq # endorse group group_name to read objects in tenancy reporting # # Note - The only value you need to change is the group name. Do not change the OCID in the first statement. reporting_namespace = 'bling' # Download all usage and cost files. You can comment out based on the specific need: prefix_file = "" # For cost and usage files # prefix_file = "reports/cost-csv" # For cost # prefix_file = "reports/usage-csv" # For usage # Update these values destintation_path = 'downloaded_reports' # Make a directory to receive reports if not os.path.exists(destintation_path): os.mkdir(destintation_path) # Get the list of reports config = oci.config.from_file(oci.config.DEFAULT_LOCATION, oci.config.DEFAULT_PROFILE) reporting_bucket = config['tenancy'] object_storage = oci.object_storage.ObjectStorageClient(config) report_bucket_objects = object_storage.list_objects(reporting_namespace, reporting_bucket, prefix=prefix_file) for o in report_bucket_objects.data.objects: print('Found file ' + o.name) object_details = object_storage.get_object(reporting_namespace, reporting_bucket, o.name) filename = o.name.rsplit('/', 1)[-1] with open(destintation_path + '/' + filename, 'wb') as f: for chunk in object_details.data.raw.stream(1024 * 1024, decode_content=False): f.write(chunk) print('----> File ' + o.name + ' Downloaded')