Amazon web services 使用boto3将AMI复制到另一个区域

Amazon web services 使用boto3将AMI复制到另一个区域,amazon-web-services,amazon-ec2,boto3,amazon-ami,Amazon Web Services,Amazon Ec2,Boto3,Amazon Ami,我正在尝试自动化AWS EC2控制台上的“复制AMI”功能,有人能告诉我一些通过boto3实现这一功能的Python代码吗?来自: 确保将请求发送到目标区域,并传入对源区域的引用,以便更准确地说 假设您要复制的AMI位于us-east-1(源区域)。 您的要求是将其复制到us-west-2(目的地地区) 将boto3 EC2客户端会话发送到us-west-2区域,然后在SourceRegion中传递us-east-1 import boto3 session1 = boto3.client('e

我正在尝试自动化AWS EC2控制台上的“复制AMI”功能,有人能告诉我一些通过boto3实现这一功能的Python代码吗?

来自:

确保将请求发送到目标区域,并传入对
源区域

的引用,以便更准确地说

假设您要复制的AMI位于us-east-1(源区域)。 您的要求是将其复制到us-west-2(目的地地区)

将boto3 EC2客户端会话发送到us-west-2区域,然后在SourceRegion中传递us-east-1

import boto3
session1 = boto3.client('ec2',region_name='us-west-2')

response = session1.copy_image(
   Name='DevEnv_Linux',
   Description='Copied this AMI from region us-east-1',
   SourceImageId='ami-02a6ufwod1f27e11',
   SourceRegion='us-east-1'
)

我大部分时间都使用EC2.ServiceResource之类的高级资源,因此下面是我使用EC2资源和低级客户端的代码

source_image_id = '....'
profile = '...'
source_region = 'us-west-1'
source_session = boto3.Session(profile_name=profile, region_name=source_region)
ec2 = source_session.resource('ec2')
ami = ec2.Image(source_image_id)
target_region = 'us-east-1'
target_session = boto3.Session(profile_name=profile, region_name=target_region)
target_ec2 = target_session.resource('ec2')
target_client = target_session.client('ec2')
response = target_client.copy_image(
  Name=ami.name,
  Description = ami.description,
  SourceImageId = ami.id,
  SorceRegion = source_region
)
target_ami = target_ec2.Image(response['ImageId'])
source_image_id = '....'
profile = '...'
source_region = 'us-west-1'
source_session = boto3.Session(profile_name=profile, region_name=source_region)
ec2 = source_session.resource('ec2')
ami = ec2.Image(source_image_id)
target_region = 'us-east-1'
target_session = boto3.Session(profile_name=profile, region_name=target_region)
target_ec2 = target_session.resource('ec2')
target_client = target_session.client('ec2')
response = target_client.copy_image(
  Name=ami.name,
  Description = ami.description,
  SourceImageId = ami.id,
  SorceRegion = source_region
)
target_ami = target_ec2.Image(response['ImageId'])