Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

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'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...