How to get AWS VCPU's utilization number via API

847 views Asked by At

I would like to get the number of AWS VCPU's used by all running instances in the region via GO API, so I can run some preflight checks before I create new instances, to see if there are enough VCPU's available, without increasing the quota.

In other words, if we look at the console we can see that Running On-Demand Standard (A, C, D, H, I, M, R, T, Z) instances service quota, shows Applied quota value column that indicates the current VCPU's limit. If you click on this field, you can see the 'Utilization' field that shows the used VCPU's. This is what I need.

AWS Service Quotas - Running On-Demand Standard instances

Thanks!

1

There are 1 answers

7
John Rotenstein On

To discover where this information was available, I looked into the Service Quotas AWS service.

The aws service-quotas list-service command lists the service codes, such as:

{
    "Services": [
        {
            "ServiceCode": "AWSCloudMap",
            "ServiceName": "AWS Cloud Map"
        },
        ...
        {
            "ServiceCode": "ec2",
            "ServiceName": "Amazon Elastic Compute Cloud (Amazon EC2)"
        },
        ...
        {
            "ServiceCode": "xray",
            "ServiceName": "AWS X-Ray"
        }
    ]
}

So, the service code for Amazon EC2 is ec2.

Next, I listed the quotas for this service using aws service-quotas list-service-quotas --service-code ec2. Amongst the output I found:

{
    "Quotas": [
    ...
        {
            "ServiceCode": "ec2",
            "ServiceName": "Amazon Elastic Compute Cloud (Amazon EC2)",
            "QuotaArn": "arn:aws:servicequotas:ap-southeast-2:123456789012:ec2/L-1216C47A",
            "QuotaCode": "L-1216C47A",
            "QuotaName": "Running On-Demand Standard (A, C, D, H, I, M, R, T, Z) instances",
            "Value": 640.0,
            "Unit": "None",
            "Adjustable": true,
            "GlobalQuota": false,
            "UsageMetric": {
                "MetricNamespace": "AWS/Usage",
                "MetricName": "ResourceCount",
                "MetricDimensions": {
                    "Class": "Standard/OnDemand",
                    "Resource": "vCPU",
                    "Service": "EC2",
                    "Type": "Resource"
                },
                "MetricStatisticRecommendation": "Maximum"
            }
        },

    ...
    ]
}

This seemed to match the quota displayed in the Management Console.

I then noticed that the UsageMetric information looked like an Amazon CloudWatch metric:

            "UsageMetric": {
                "MetricNamespace": "AWS/Usage",
                "MetricName": "ResourceCount",
                "MetricDimensions": {
                    "Class": "Standard/OnDemand",
                    "Resource": "vCPU",
                    "Service": "EC2",
                    "Type": "Resource"
                },
                "MetricStatisticRecommendation": "Maximum"
            }

I then went to Amazon CloudWatch, clicked All metrics and entered a search term of usage standard:

Amazon CloudWatch metrics search

Aha! The Standard/OnDemand metric appeared!

Selecting the checkbox then showed a chart:

Amazon CloudWatch metrics chart

This metric matches the Utilization figure shown in the Service Quotas page.

Bottom line

Service Quota metrics are available through Amazon CloudWatch. You can use standard API calls to retrieve these metrics, or access them through the Amazon CloudWatch Metrics management console. You can also Create Alarms on these metrics to notify you when they exceed desired limits.