简体   繁体   English

如何通过 boto3 获取 AWS EC2 的配额?

[英]How to get quotas of AWS EC2 via boto3?

I'm working on boto3 - SDK python for AWS.我正在研究 boto3 - 适用于 AWS 的 SDK python。

How can I get AWS Service Limits via boto3 library like bellow:如何通过如下所示的 boto3 库获取 AWS 服务限制:

http://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html http://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html

Some of those limits can be queried through EC2.Client.describe_account_attributes()其中一些限制可以通过EC2.Client.describe_account_attributes()查询

describe_account_attributes(**kwargs)

Describes attributes of your AWS account.描述您的 AWS 账户的属性。 The following are the supported account attributes:以下是支持的帐户属性:

... ...

  • max-instances : The maximum number of On-Demand instances that you can run. max-instances :您可以运行的最大按需实例数。

  • vpc-max-security-groups-per-interface : The maximum number of security groups that you can assign to a network interface. vpc-max-security-groups-per-interface :您可以分配给网络接口的最大安全组数。

  • max-elastic-ips : The maximum number of Elastic IP addresses that you can allocate for use with EC2-Classic. max-elastic-ips :您可以分配用于 EC2-Classic 的最大弹性 IP 地址数。

  • vpc-max-elastic-ips : The maximum number of Elastic IP addresses that you can allocate for use with EC2-VPC. vpc-max-elastic-ips :您可以分配用于 EC2-VPC 的最大弹性 IP 地址数。

Why not use awsLimitchecker ?为什么不使用awsLimitchecker I use it to create a report every morning and send it to a slack group, where I monitor our current limits.我每天早上用它来创建一份报告并将其发送给一个 slack 组,在那里我监控我们当前的限制。 Works really well.效果很好。

There is a better option than awsLimitChecker , as not all AWS Service Instances' Quota can be retrieved through it, for example for many EC2 instances you need to use TrustedAdvisor service and many EC2 service instances' quota could not be retrieved at all.有一个比awsLimitChecker更好的选择,因为并非所有 AWS 服务实例的配额都可以通过它检索,例如,对于许多 EC2 实例,您需要使用TrustedAdvisor 服务,而许多 EC2 服务实例的配额根本无法检索。 Also recently AWS has changed the on-demand service quota EC2 instance based limits to vCPU based limit, categorized as follows:此外,最近 AWS 将基于按需服务配额 EC2 实例的限制更改为基于 vCPU 的限制,分类如下:

  • Running On-Demand Standard (A, C, D, H, I, M, R, T, Z) instances -> 1152 vCPUs运行按需标准(A、C、D、H、I、M、R、T、Z)实例 -> 1152 个 vCPU
  • Running On-Demand F instances -> 128 vCPUs运行按需 F 实例 -> 128 个 vCPU
  • Running On-Demand G instances -> 128 vCPUs运行按需 G 实例 -> 128 个 vCPU
  • Running On-Demand P instances -> 128 vCPUs运行按需 P 实例 -> 128 个 vCPU
  • Running On-Demand X instances -> 128 vCPUs运行按需 X 实例 -> 128 个 vCPU

Another approach to retrieve service quota is using boto3's Service Quota client .另一种检索服务配额的方法是使用boto3 的服务配额客户端 You can use APIs like list_service_quotas to retrieve complete list of service quotas support by an aws service ( ec2 ).您可以使用list_service_quotas 之类的API 来检索 aws 服务 ( ec2 ) 支持的服务配额的完整列表。 Or for specific service quota you can use get_service_quota .或者对于特定的服务配额,您可以使用get_service_quota Additionally to use above mentioned APIs make sure to add service quotas actions (policy) into IAM user / group.此外,要使用上述 API,请确保将服务配额操作(策略)添加到 IAM 用户/组中。

Following is source code snippet.以下是源代码片段。

import boto3

map_quota_codes = {}
client = ''

def get_service_quota(self, quota_name):
    """
    Retrieves requested AWS service quota.
    :param str quota_name: Full name of Quota e.g. Running On-Demand Standard (A, C, D, H, I, M, R, T, Z) instances
    """
    if not map_quota_codes or quota_name not in map_quota_codes:
        return {}

    quota_code = map_quota_codes[quota_name]["QuotaCode"]
    res = client.get_service_quota(ServiceCode = 'ec2', QuotaCode = quota_code)

    return res["Quota"]

if __name__ == "main":

    # AWS account login should be done here.
    client = boto3.client('service-quotas')
    service_quota_resp = self.client.list_service_quotas(ServiceCode = 'ec2')

    # this is required to map get correct quota code for respective ec2 service quota.
    for quota in service_quota_resp['Quotas']:
        map_quota_codes[quota["QuotaName"]] = {'QuotaCode': quota["QuotaCode"]}

    while "NextToken" in service_quota_resp:
        service_quota_resp = client.list_service_quotas(ServiceCode = 'ec2', NextToken = service_quota_resp["NextToken"])

        for quota in service_quota_resp['Quotas']:
            map_quota_codes[quota["QuotaName"]] = {'QuotaCode': quota["QuotaCode"]}

End Note: If you like the description and my answer helped you Upvote it and mark it as an answer.尾注:如果您喜欢描述并且我的回答帮助了您,请点赞并将其标记为答案。 thanks谢谢

jack千斤顶

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM