Use Cost Explorer APIs

Updated 1 month ago by Archana Singh

The Cost Explorer API allows you to query your cost and usage data. You can query total, idle, and unallocated cloud costs in your cluster or Application. The response helps you to understand your primary cost contributors. You can use this data to manage and optimize your cost and resources.

Currently, these APIs can provide data for up to the last seven days. If you do not specify StartTime and EndTime or the specified time value is greater than seven days, it returns an exception error.

This topic lists examples of the export data API usage.

In this topic:

Before You Begin

Example 1: Get Total, Idle, and Unallocated Cost Across Your Clusters

Use this sample query to get the total, idle, and unallocated cost across your clusters.

You can also specify a limit and offset to the query. Limit specifies the number of entries that will be returned and offset specifies the index from start.

The time values passed for these queries follow Unix Time. You can use Epoch and Unix Timestamp Conversion Tools to convert.
Request
{
ceClusterBillingData(aggregateFunction: [{function: SUM, cost: TOTALCOST}, {function: SUM, cost: IDLECOST}, {function: SUM, cost: UNALLOCATEDCOST}], filters: [{startTime: {operator: AFTER, value: 1594166400000}}, {endTime: {operator: BEFORE, value: 1594771199000}}], groupBy: [{entity: Cluster}, {entity: Region}], sortCriteria: {sortType: TOTALCOST, order: DESCENDING}, limit: 2, offset: 1) {
data {
cluster
region
totalCost
idleCost
unallocatedCost
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"cluster": "5e4492209d084a327c5f9690",
"region": "us-central1",
"totalCost": 114.40402222222215,
"idleCost": 0,
"unallocatedCost": 49.168831484429724
},
{
"cluster": "5db7df8c01d32e806e20db28",
"region": "us-central1",
"totalCost": 114.37949999999987,
"idleCost": 34.45507082155397,
"unallocatedCost": 49.1824891993497
}
]
}
}
}

Example 2: Get Total Cost Across Your Clusters

Use this sample query to get the details of the total cost across your clusters.

Request
{
ceClusterBillingData(aggregateFunction: [{function: SUM, cost: TOTALCOST}], filters: [{startTime: {operator: AFTER, value: 1594166400000}}, {endTime: {operator: BEFORE, value: 1594771199000}}], groupBy: [{entity: Cluster}, {time: {timePeriod: DAY}}], sortCriteria: {sortType: TIME, order: ASCENDING}) {
data {
startTime
cluster
totalCost
}
}
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"startTime": 1594166400000,
"cluster": "5db7df8c01d32e806e20db28",
"totalCost": 22.182955555555555
},
{
"startTime": 1594166400000,
"cluster": "5e4492209d084a327c5f9690",
"totalCost": 22.18296666666667
},
{
"startTime": 1594166400000,
"cluster": "5e685bc20b238de6cbfb2b0a",
"totalCost": 46.358784
},
{
"startTime": 1594166400000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 5.6495999999999995
},
{
"startTime": 1594252800000,
"cluster": "5db7df8c01d32e806e20db28",
"totalCost": 23.258011111111113
},
{
"startTime": 1594252800000,
"cluster": "5e4492209d084a327c5f9690",
"totalCost": 23.27865555555555
},
{
"startTime": 1594252800000,
"cluster": "5e685bc20b238de6cbfb2b0a",
"totalCost": 20.736714000000003
},
{
"startTime": 1594252800000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 5.6495999999999995
},
{
"startTime": 1594339200000,
"cluster": "5db7df8c01d32e806e20db28",
"totalCost": 22.64108888888889
},
{
"startTime": 1594339200000,
"cluster": "5e4492209d084a327c5f9690",
"totalCost": 22.692766666666667
},
{
"startTime": 1594339200000,
"cluster": "5e685bc20b238de6cbfb2b0a",
"totalCost": 20.421504000000002
},
{
"startTime": 1594339200000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 5.6495999999999995
},
{
"startTime": 1594425600000,
"cluster": "5db7df8c01d32e806e20db28",
"totalCost": 23.24521111111111
},
{
"startTime": 1594425600000,
"cluster": "5e4492209d084a327c5f9690",
"totalCost": 23.205488888888897
},
{
"startTime": 1594425600000,
"cluster": "5e685bc20b238de6cbfb2b0a",
"totalCost": 20.421504000000002
},
{
"startTime": 1594425600000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 5.6495999999999995
},
{
"startTime": 1594512000000,
"cluster": "5db7df8c01d32e806e20db28",
"totalCost": 23.05223333333334
},
{
"startTime": 1594512000000,
"cluster": "5e4492209d084a327c5f9690",
"totalCost": 23.044144444444445
},
{
"startTime": 1594512000000,
"cluster": "5e685bc20b238de6cbfb2b0a",
"totalCost": 20.421504000000002
},
{
"startTime": 1594512000000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 5.649600000000003
}
]
}
}
}

Example 3: Get Total and Idle Cost in Your Cluster Workloads

Use this sample query to get the total and idle cost in your cluster workloads. This example uses a Kubernetes cluster.

Request
{
ceClusterBillingData(aggregateFunction: [{function: SUM, cost: TOTALCOST}, {function: SUM, cost: IDLECOST}], filters: [{startTime: {operator: AFTER, value: 1594166400000}}, {endTime: {operator: BEFORE, value: 1594771199000}}], groupBy: [{entity: Workload}, {entity: Namespace}], sortCriteria: {sortType: TOTALCOST, order: DESCENDING}) {
data {
k8s {
workload
namespace
}
totalCost
idleCost
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"k8s": {
"workload": "Unallocated",
"namespace": "Unallocated"
},
"totalCost": 171.94164690970473,
"idleCost": 0
},
{
"k8s": {
"workload": "qa-target-zeaakf",
"namespace": "harness-delegate"
},
"totalCost": 11.351383425925924,
"idleCost": 3.209113214533147
},
{
"k8s": {
"workload": "eks-custom-delegate-zeaakf-0",
"namespace": "harness-delegate"
},
"totalCost": 7.762500000000001,
"idleCost": 5.411073869204506
},
{
"k8s": {
"workload": "qa-target-automation-1-xicobc",
"namespace": "harness-delegate"
},
"totalCost": 7.561381759259261,
"idleCost": 1.4539720434025662
}
]
}
}
}

Example 4: Get Total Cost in Your Cluster Workloads

Use this sample query to get the total cost in your cluster workloads. This example uses a Kubernetes cluster.

Request
{
ceClusterBillingData(aggregateFunction: [{function: SUM, cost: TOTALCOST}], filters: [{startTime: {operator: AFTER, value: 1594166400000}}, {endTime: {operator: BEFORE, value: 1594771199000}}], groupBy: [{entity: Workload}, {time: {timePeriod: DAY}}], sortCriteria: {sortType: TIME, order: ASCENDING}) {
data {
startTime
k8s {
workload
}
totalCost
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"startTime": 1594166400000,
"k8s": {
"workload": "harness-example-1594104124442-deployment-canary"
},
"totalCost": 0.000009444444444444445
},
{
"startTime": 1594166400000,
"k8s": {
"workload": "kube-dns-autoscaler"
},
"totalCost": 0.005425000000000001
},
{
"startTime": 1594166400000,
"k8s": {
"workload": "stackdriver-metadata-agent-cluster-level"
},
"totalCost": 0.036605
},
{
"startTime": 1594166400000,
"k8s": {
"workload": "harness-example-1594104294676-deployment-canary"
},
"totalCost": 0.000015277777777777777
},
{
"startTime": 1594166400000,
"k8s": {
"workload": "asb-1"
},
"totalCost": 0
}
]
}
}
}

Example 5: Get the Cost of ECS Service in Your Cluster

Use this sample query to get the cost of ECS Service in your cluster.

Request
{
ceClusterBillingData(aggregateFunction: [{function: SUM, cost: TOTALCOST}, {function: SUM, cost: IDLECOST}], filters: [{startTime: {operator: AFTER, value: 1594166400000}}, {endTime: {operator: BEFORE, value: 1594771199000}}], groupBy: [{entity: EcsService}, {entity: LaunchType}], sortCriteria: {sortType: TOTALCOST, order: DESCENDING}) {
data {
ecs {
service
launchType
}
totalCost
idleCost
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"ecs": {
"service": "ecs-example",
"launchType": "EC2"
},
"totalCost": 6.952500000000006,
"idleCost": 5.19847219844681
},
{
"ecs": {
"service": "cce_app_01__cce_svc_aws__cce_aws_env__2",
"launchType": "EC2"
},
"totalCost": 0.3530566406249998,
"idleCost": 0.33087744140624925
},
{
"ecs": {
"service": "cce_app_02__ce_svc_01_env_02__qa__2",
"launchType": "EC2"
},
"totalCost": 0.17652832031250026,
"idleCost": 0.1641562499999997
}
]
}
}
}

Example 6: Get Total and Idle Cost in Your Harness Applications

Use this sample query to get the total, idle, and unallocated cost in your Harness Application. For more information on how Harness organizes and models your projects using Applications, review Harness Applications.

Request
{
ceClusterBillingData(aggregateFunction: [{function: SUM, cost: TOTALCOST}, {function: SUM, cost: IDLECOST}], filters: [{startTime: {operator: AFTER, value: 1594166400000}}, {endTime: {operator: BEFORE, value: 1594771199000}}], groupBy: [{entity: Application}], sortCriteria: {sortType: TOTALCOST, order: DESCENDING}) {
data {
harness {
application
}
totalCost
idleCost
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"harness": {
"application": "cce_app_01"
},
"totalCost": 0.3578607586320755,
"idleCost": 0.33087744140624925
},
{
"harness": {
"application": "cce_app_02"
},
"totalCost": 0.17652832031250026,
"idleCost": 0.1641562499999997
}
]
}
}
}

Example 7: Get Raw Data

Use this sample query to get raw data, such as cluster name, cluster type, average memory, and CPU utilization, etc.

Request
{
ceClusterBillingData(filters:[{startTime:{operator:AFTER, value:1593839612000}}, {endTime:{operator:BEFORE, value:1594185212000}}] limit: 10){
data{
startTime
cluster
totalCost
idleCost
unallocatedCost
systemCost
clusterType
region
k8s{
workload
namespace
}
ecs{
service
launchType
}
avgCpuUtilization
avgMemoryUtilization
maxCpuUtilization
maxMemoryUtilization
memoryLimit
memoryRequest
cpuLimit
cpuRequest
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"startTime": 1594166400000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 2.76,
"idleCost": 1.5653063312665463,
"unallocatedCost": 0.5091926027624538,
"systemCost": 0.44759646492004396,
"clusterType": "K8S",
"region": "eu-central-1",
"k8s": {
"workload": null,
"namespace": null
},
"ecs": {
"service": null,
"launchType": null
},
"avgCpuUtilization": 0.0874776659826389,
"avgMemoryUtilization": 0.4092622368948973,
"maxCpuUtilization": 0.359142599,
"maxMemoryUtilization": 0.5701184272766113,
"memoryLimit": 0,
"memoryRequest": 8192,
"cpuLimit": 0,
"cpuRequest": 2048
},
{
"startTime": 1594166400000,
"cluster": "5efc799adc9315eea531ccbe",
"totalCost": 0.043199999999999995,
"idleCost": 0,
"unallocatedCost": 0.03297722931056097,
"systemCost": 0.006449908447265625,
"clusterType": "K8S",
"region": "eu-central-1",
"k8s": {
"workload": null,
"namespace": null
},
"ecs": {
"service": null,
"launchType": null
},
"avgCpuUtilization": 0.02275921297222222,
"avgMemoryUtilization": 0.5611361984370483,
"maxCpuUtilization": 0.046088964,
"maxMemoryUtilization": 0.6234664916992188,
"memoryLimit": 0,
"memoryRequest": 512,
"cpuLimit": 0,
"cpuRequest": 2048
}
]
}
}
}

Example 8: Add Label Information in the Workload Table View

Use this sample query to add label data in the workload table view. This adds Name and Value columns for the selected labels.

Request
{
ceClusterBillingData( aggregateFunction: [{function: SUM, cost: TOTALCOST}, {function: SUM, cost: IDLECOST}],
filters: [{startTime: {operator: AFTER, value: 1594166400000}},
{endTime: {operator: BEFORE, value: 1594771199000}}],
groupBy: [{entity: Workload}, {entity: Namespace}],
select : [{ labels : ["app", "version"] } ],
sortCriteria: {sortType: TOTALCOST, order: DESCENDING}
limit:4) {
data {
k8s {
workload
namespace
selectedLabels{
name
value
}
}
totalCost
idleCost
}
}
}
Response
{
"data": {
"ceClusterBillingData": {
"data": [
{
"k8s": {
"workload": "harness-example-deployment",
"namespace": "harness",
"selectedLabels": [
{
"name": "app",
"value": "harness-example"
},
{
"name": "version",
"value": "-"
}
]
},
"totalCost": 4.452178888888889,
"idleCost": 2.138144994067336
},
{
"k8s": {
"workload": "harness-test-appd-deployment",
"namespace": "harness",
"selectedLabels": [
{
"name": "app",
"value": "harness-test-appd"
},
{
"name": "version",
"value": "-"
}
]
},
"totalCost": 4.444068611111109,
"idleCost": 2.0589668015521623
},
{
"k8s": {
"workload": "mongo",
"namespace": "mongo",
"selectedLabels": [
{
"name": "app",
"value": "mongo"
},
{
"name": "version",
"value": "-"
}
]
},
"totalCost": 1.2370908333333333,
"idleCost": 0.6569387846433649
},
{
"k8s": {
"workload": "proff-point-polling-enabled-kmpysm",
"namespace": "harness-delegate",
"selectedLabels": [
{
"name": "app",
"value": "-"
},
{
"name": "version",
"value": "-"
}
]
},
"totalCost": 1.213107037037037,
"idleCost": 0.6089169647607484
}
]
}
}
}


How did we do?