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

Yar*_*tov 2 amazon-ec2 amazon-web-services boto3 amazon-ami

我正在尝试自动执行我在 AWS EC2 控制台上拥有的“复制 AMI”功能,有人能指点我一些通过 boto3 执行此操作的 Python 代码吗?

小智 7

更准确地说。

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

获取到 us-west-2 区域的 boto3 EC2 客户端会话,然后在 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'
)
Run Code Online (Sandbox Code Playgroud)


Joh*_*ein 5

来自EC2 — Boto 3 文档

response = client.copy_image(
    ClientToken='string',
    Description='string',
    Encrypted=True|False,
    KmsKeyId='string',
    Name='string',
    SourceImageId='string',
    SourceRegion='string',
    DryRun=True|False
)
Run Code Online (Sandbox Code Playgroud)

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