Python boto3-如何跨区域工作

Python boto3-如何跨区域工作,python,boto3,Python,Boto3,我开始用python和boto3编写lambda函数 我设法分别在每个地区工作,但我不知道如何在几个地区一起工作 以下是我宣布我的客户的方式: region= 'ap-southeast-2' ec2 = boto3.client('ec2', region_name=region) 如果我不给它一个区域,它将在创建lambda函数的区域上运行,这是我不想要的 以前有人做过吗?如果不给它一个区域,它将使用运行Lambda函数的区域 如果您想通过Boto3在多个区域中进行AWS API调用,您必

我开始用python和boto3编写lambda函数

我设法分别在每个地区工作,但我不知道如何在几个地区一起工作

以下是我宣布我的客户的方式:

region= 'ap-southeast-2'
ec2 = boto3.client('ec2', region_name=region)
如果我不给它一个区域,它将在创建lambda函数的区域上运行,这是我不想要的


以前有人做过吗?

如果不给它一个区域,它将使用运行Lambda函数的区域


如果您想通过Boto3在多个区域中进行AWS API调用,您必须为每个区域创建一个客户端对象,并为每个区域进行单独的API调用。

这里我列出了跨多个区域的所有可用实例

你可以这样做

1。列出所有可用区域。

    client = boto3.client('ec2')


    def get_all_regions():
        all_regions = [ region['RegionName'] for region 
        inclient.describe_regions['Regions']]
        return all_regions

    regions_arr = []
    regions = get_all_regions()
    for x in range(0, len(regions_arr)):
         client_2 = boto3.client('ec2', region_name=regions_arr[x])
         response = client_2.describe_instance_status()

         for x in response['InstanceStatuses']:
              print(x['InstanceId'])
2。将结果附加到python列表中

    for each_reg in regions:
        regions_arr.append(each_reg)
3。循环遍历python列表中的每个区域,并将region\u name属性设置为该区域。

    client = boto3.client('ec2')


    def get_all_regions():
        all_regions = [ region['RegionName'] for region 
        inclient.describe_regions['Regions']]
        return all_regions

    regions_arr = []
    regions = get_all_regions()
    for x in range(0, len(regions_arr)):
         client_2 = boto3.client('ec2', region_name=regions_arr[x])
         response = client_2.describe_instance_status()

         for x in response['InstanceStatuses']:
              print(x['InstanceId'])
希望这有帮助。谢谢