【问题标题】:Boto3 not copying snapshot to other regions, other options?Boto3 不将快照复制到其他区域,其他选项?
【发布时间】:2018-05-19 09:44:50
【问题描述】:

[AWS 新手]

你好,

我正在尝试跨区域移动我的 EBS 卷快照副本。我一直在尝试使用 Boto3 来移动快照。我的目标是每天自动将最新快照从us-east-2 区域移动到us-east-1 区域。

我在终端中使用了aws configure 命令来设置我的安全凭证并将区域设置为us-east-2

我正在使用 pandas 获取最新的快照 ID,使用以下代码:

import boto3
import pandas as pd
from pandas.io.json.normalize import nested_to_record    
import boto.ec2


    client = boto3.client('ec2')
    aws_api_response = client.describe_snapshots(OwnerIds=['self'])
    flat = nested_to_record(aws_api_response)
    df = pd.DataFrame.from_dict(flat)
    df= df['Snapshots'].apply(pd.Series)
    insert_snap = df.loc[df['StartTime'] == max(df['StartTime']),'SnapshotId']
    insert_snap = insert_snap.reset_index(drop=True)

insert_snap 返回类似snap-1234ABCD 的快照ID

我正在尝试使用此代码将快照从us-east-2 移动到 us-east-1

client.copy_snapshot(SourceSnapshotId='%s' %insert_snap[0],
                     SourceRegion='us-east-2',
                     DestinationRegion='us-east-1',
                     Description='This is my copied snapshot.')

快照正在使用上述行在同一区域中复制。

我还尝试通过终端中的aws configure 命令切换区域,在同一区域复制快照时也会出现同样的问题。

Boto3 中存在一个错误,即跳过copy_snapshot() 代码中的目标参数。在此处找到信息:https://github.com/boto/boto3/issues/886

我已尝试将此代码插入 lambda 管理器,但不断收到错误 "errorMessage": "Unable to import module 'lambda_function'":

region = 'us-east-2'
ec = boto3.client('ec2',region_name=region)

def lambda_handler(event, context):
    response=ec.copy_snapshot(SourceSnapshotId='snap-xxx',
                     SourceRegion=region,
                     DestinationRegion='us-east-1',
                     Description='copied from Ohio')
    print (response)

我没有选择,我可以做些什么来自动传输 aws 中的快照?

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 snapshot amazon-ebs


    【解决方案1】:

    根据CopySnapshot - Amazon Elastic Compute Cloud

    CopySnapshot 将快照副本发送到您将 HTTP 请求发送到的区域终端节点,例如 ec2.us-east-1.amazonaws.com(在 AWS CLI 中,这是使用 --region 参数或默认值指定的AWS 配置文件中的区域)。

    因此,您应该将copy_snapshot() 命令发送到us-east-1,并将源区域设置为us-east-2

    如果你想移动最近的快照,你可以运行:

    import boto3
    
    SOURCE_REGION = 'us-east-2'
    DESTINATION_REGION = 'us-east-1'
    
    # Connect to EC2 in Source region
    source_client = boto3.client('ec2', region_name=SOURCE_REGION)
    
    # Get a list of all snapshots, then sort them
    snapshots = source_client.describe_snapshots(OwnerIds=['self'])
    snapshots_sorted = sorted([(s['SnapshotId'], s['StartTime']) for s in snapshots['Snapshots']], key=lambda k: k[1])
    latest_snapshot = snapshots_sorted[-1][0]
    
    print ('Latest Snapshot ID is ' + latest_snapshot)
    
    # Connect to EC2 in Destination region
    destination_client = boto3.client('ec2', region_name=DESTINATION_REGION)
    
    # Copy the snapshot
    response = destination_client.copy_snapshot(
        SourceSnapshotId=latest_snapshot,
        SourceRegion=SOURCE_REGION,
        Description='This is my copied snapshot'
        )
    
    print ('Copied Snapshot ID is ' + response['SnapshotId'])
    

    【讨论】:

    • 无论如何我可以使用您上面的代码仅复制源区域中状态为“已完成”的快照吗?提前谢谢你
    • 使用:describe_snapshots(OwnerIds=['self'], Filters=[{'Name':'status', 'Values':['completed']}])
    • 这实际上是非常了不起的@JohnRotenstein。我如何也复制快照的名称?
    • @Busted 快照的名称(我认为)只是一个Name标签,所以你可以在复制后添加标签。
    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2020-08-15
    相关资源
    最近更新 更多