Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy AMI to another region using boto3

I'm trying to automate "Copy AMI" functionality I have on my AWS EC2 console, can anyone point me to some Python code that does this through boto3?

like image 325
Yaroslav Bulatov Avatar asked Oct 22 '25 06:10

Yaroslav Bulatov


2 Answers

To be more precise.

Let's say the AMI you want to copy is in us-east-1 (Source region). Your requirement is to copy this into us-west-2 (Destination region)

Get the boto3 EC2 client session to us-west-2 region and then pass the us-east-1 in the SourceRegion.

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'
)
like image 104
RaghuCK Avatar answered Oct 25 '25 05:10

RaghuCK


From EC2 — Boto 3 documentation:

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

Make sure you send the request to the destination region, passing in a reference to the SourceRegion.

like image 44
John Rotenstein Avatar answered Oct 25 '25 04:10

John Rotenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!