You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boto3跨区域复制EBS快照失败,求AWS自动化迁移替代方案

Solutions for Automated Cross-Region EBS Snapshot Replication

Hey there! I feel your pain with the Boto3 copy_snapshot quirk and Lambda import issues. Let’s walk through several reliable ways to get your daily us-east-2 → us-east-1 snapshot replication up and running smoothly:

1. Fix the Boto3/Lambda Approach (The Bug Workaround)

First, let’s tackle that Lambda error and the Boto3 bug. The copy_snapshot issue you mentioned happens when you call it from the source region’s client—it ignores the DestinationRegion parameter. The fix is to use the target region’s EC2 client instead. Also, your "Unable to import module" error is likely due to a simple misconfiguration (like wrong filename or missing permissions), not a code issue.

Here’s a corrected Lambda script that works:

import boto3

def lambda_handler(event, context):
    source_region = 'us-east-2'
    dest_region = 'us-east-1'
    
    # 1. Get the latest snapshot from source region
    source_ec2 = boto3.client('ec2', region_name=source_region)
    snapshots = source_ec2.describe_snapshots(OwnerIds=['self'])['Snapshots']
    if not snapshots:
        print("No snapshots found in source region")
        return
    
    latest_snap = max(snapshots, key=lambda x: x['StartTime'])
    snap_id = latest_snap['SnapshotId']
    print(f"Found latest snapshot: {snap_id}")
    
    # 2. Copy snapshot to target region using target region's client
    dest_ec2 = boto3.client('ec2', region_name=dest_region)
    response = dest_ec2.copy_snapshot(
        SourceSnapshotId=snap_id,
        SourceRegion=source_region,
        Description=f"Daily copy from us-east-2 - {latest_snap['Description']}"
    )
    print(f"Copy initiated: {response['SnapshotId']}")
    return response

Lambda Setup Tips:

  • Name your file lambda_function.py (required for Lambda to recognize it)
  • Attach an IAM role to your Lambda with these permissions (adjust regions as needed):
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": "ec2:DescribeSnapshots",
                "Resource": "*",
                "Condition": {"StringEquals": {"ec2:Region": "us-east-2"}}
            },
            {
                "Effect": "Allow",
                "Action": "ec2:CopySnapshot",
                "Resource": "*",
                "Condition": {"StringEquals": {"ec2:Region": "us-east-1"}}
            }
        ]
    }
    
  • Use EventBridge (formerly CloudWatch Events) to trigger this Lambda daily—set a cron schedule like 0 0 * * ? * (every midnight UTC).

2. Use AWS CLI + EventBridge (No Python Required)

If you prefer avoiding code entirely, use a shell script with AWS CLI and schedule it via EventBridge:

Step 1: Create a Shell Script (save as copy-snapshot.sh)

#!/bin/bash

SOURCE_REGION="us-east-2"
DEST_REGION="us-east-1"

# Get latest snapshot ID from source region
LATEST_SNAP=$(aws ec2 describe-snapshots --owner-ids self --region $SOURCE_REGION --query 'Snapshots | sort_by(@, &StartTime) | [-1].SnapshotId' --output text)

# Copy snapshot to target region
aws ec2 copy-snapshot \
    --source-snapshot-id $LATEST_SNAP \
    --source-region $SOURCE_REGION \
    --region $DEST_REGION \
    --description "Daily copy from us-east-2"

Step 2: Schedule with EventBridge

  • Create a new EventBridge rule with a daily schedule.
  • Choose "EC2 Run Command" or "Lambda" as the target:
    • For Lambda: Package the script in a Lambda layer (or use a Python wrapper to run the CLI commands)
    • For EC2 Run Command: Run the script on a managed EC2 instance (ensure the instance has IAM permissions for EC2 snapshot actions)

3. AWS Data Pipeline (Managed No-Code Workflow)

AWS Data Pipeline has a pre-built template for copying EBS snapshots—perfect for scheduled, managed replication:

  • Go to the Data Pipeline console and click "Create new pipeline"
  • Select the "Copy EBS Snapshot" template
  • Configure:
    • Source Region: us-east-2
    • Destination Region: us-east-1
    • Schedule: Set to run daily (adjust time zone as needed)
    • Filter Options: Add a condition to only copy the latest snapshot (use StartTime in the filter)
  • Launch the pipeline—it will handle scheduling, retries, and logging automatically.

4. AWS Step Functions (For Complex Workflows)

If you need error handling, notifications, or additional steps (like tagging the copied snapshot), use Step Functions to build a state machine:

  • Create a state machine with two steps:
    1. Get Latest Snapshot: A Lambda task that fetches the newest snapshot ID from us-east-2
    2. Copy Snapshot: Another Lambda task (or a Step Functions EC2 API call) that copies the snapshot to us-east-1
  • Add error handlers to retry failed copies or send SNS notifications if something goes wrong
  • Schedule the state machine via EventBridge for daily execution.

All these methods avoid the Boto3 copy_snapshot bug and will reliably automate your cross-region snapshot replication. Pick the one that fits your comfort level with code vs managed services!

内容的提问来源于stack exchange,提问作者RustyShackleford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:35:06