Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 需要释放所有区域中使用boto3未关联的所有弹性IP_Amazon Web Services_Boto3_Release_Elastic Ip - Fatal编程技术网

Amazon web services 需要释放所有区域中使用boto3未关联的所有弹性IP

Amazon web services 需要释放所有区域中使用boto3未关联的所有弹性IP,amazon-web-services,boto3,release,elastic-ip,Amazon Web Services,Boto3,Release,Elastic Ip,我使用boto3和lamda来释放未使用的弹性IP。在这里,我需要释放我的AWS帐户的所有区域中存在的所有IP def elastic_ips_cleanup(): client = boto3.client('ec2') addresses_dict = client.describe_addresses() for eip_dict in addresses_dict['Addresses']: if "InstanceId" not in eip_d

我使用boto3和lamda来释放未使用的弹性IP。在这里,我需要释放我的AWS帐户的所有区域中存在的所有IP

def elastic_ips_cleanup():
    client = boto3.client('ec2')
    addresses_dict = client.describe_addresses()
    for eip_dict in addresses_dict['Addresses']:
        if "InstanceId" not in eip_dict:
            print (eip_dict['PublicIp'] +
                   " doesn't have any instances associated, releasing")
            client.release_address(AllocationId=eip_dict['AllocationId'])
我使用了上面的代码,但是它只在我执行lambda函数的特定区域释放IP


预期输出:它应该释放所有区域中存在的所有未使用的弹性IP。

一旦初始化
boto3。客户端仅在特定区域工作。默认情况下,
.aws/config

您可以通过区域循环并使用特定区域通过可选参数
region\u name=region\u name
重新初始化客户端。然后很显然,重新运行yuor函数

您可以使用:

import boto3
import logging

def elastic_ips_cleanup(region):
    client = boto3.client('ec2', region_name=region)
    # The rest of code you said you have tested already...


regions = [r['RegionName'] for r in boto3.client('ec2').describe_regions()['Regions']]

for region in regions:
    logging.info(f"Cleaning {region}")
    elastic_ips_cleanup(region)



那太好了!!!如果您给出所有活动区域列表的语法..因为我是新手..我不太了解这些代码。您好,我检查了上面的脚本,但它仍然只在当前区域释放IP。我能够使用以下代码获得所需的输出
client=boto3。client('ec2',region['RegionName'])addresses_dict=client。在addresses_dict['addresses']中描述eip_dict的_addresses():如果“InstanceId”不在eip_dict中:#print(eip_dict['PublicIp'])print(“即将删除%s中的%s”%(eip_dict['PublicIp'],RegionName'])响应=client.release_address(AllocationId=eip_dict['AllocationId'])