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.
Thanks!
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: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:This seemed to match the quota displayed in the Management Console.
I then noticed that the
UsageMetric
information looked like an Amazon CloudWatch metric:I then went to Amazon CloudWatch, clicked All metrics and entered a search term of
usage standard
:Aha! The
Standard/OnDemand
metric appeared!Selecting the checkbox then showed a 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.