使用boto3在多个区域中启动EC2 [英] Launching EC2 in multiple regions using boto3

查看:75
本文介绍了使用boto3在多个区域中启动EC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码启动EC2实例

I am using below code to launch EC2 instance

     import boto3
     client = boto3.client('ec2',region_name='us-east-1')

     resp = client.run_instances(ImageId='ami-01e3b8c3a51e88954',
                        InstanceType='t2.micro',
                        MinCount=1,MaxCount=1)
     for instance in resp['Instances']:
     print(instance['InstanceId'])

此代码有效.但是我现在的要求是一次在多个区域中启动实例.有人可以建议如何实现这一目标吗?

This code is working.But my requirement now is to launch the instance in multiple regions at a time. Can anyone suggest how to achieve this ?

推荐答案

首先,您需要找到每个区域的ami ID.AMI不是跨区域的,因此,对于每个区域,您都应该找到AMI ID.

First, you would need to find the ami ID's for each region. AMI's are not cross-region, therefore, for each region you should find the AMI ID's.

然后您将执行以下操作:

Then you would do something like:

import boto3

regions = {
    'us-east-1': 'ami-01e3b8c3a51e88954',
    'eu-west-1': 'ami-XXXXXXXXXXXXXXXXX',
}

for region in regions:
    region_client = boto3.client('ec2', region_name=region)

    resp = region_client.run_instances(ImageId=regions[region],
                                InstanceType='t2.micro',
                                MinCount=1, MaxCount=1)
    for instance in resp['Instances']:
        print(instance['InstanceId'])

这篇关于使用boto3在多个区域中启动EC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆