使用AWS re:Post即您表示您同意 AWS re:Post 使用条款

Copy snapshot from one account to another account in same region

0

Hi I need a copy snapshot from account 111AAA to account 222BBB in us-east-1 [ one account to another account] in boto3 could you please help me on this

  • please accept the answer if it was useful

已提问 7 个月前492 查看次数
3 回答
1

Lambda is limited (15 minutes max), and this may not be enough for a backup copy. Please consider using AWS Backup and Backup policies

I made an article about it https://www.automat-it.com/multi-account-backup-copy-in-aws/

profile picture
专家
已回答 7 个月前
profile picture
专家
已审核 6 个月前
1

Hi,

Why don't you just use the existing Cross-Account Snapshot Copy Policies: see https://docs.aws.amazon.com/ebs/latest/userguide/event-policy.html

They do exactly what you want with full automation.

Best,

Didier

profile pictureAWS
专家
已回答 7 个月前
0

Question mentions snapshot but does not explicitly say EBS. Snapshots could also be created for several databases and below answer is for EBS.

import boto3
import os

def lambda_handler(event, context):
    # Get the account number from an environment variable
    target_account = os.getenv('TARGET_ACCOUNT')

    # Initialize the EC2 client
    ec2 = boto3.client('ec2')

    # Get the snapshot ID from the event input
    snapshot_id = event.get('snapshot_id')
    if not snapshot_id:
        print("Error: No snapshot ID provided in the event")
        return {
            'statusCode': 400,
            'body': 'Error: No snapshot ID provided in the event'
        }

    try:
        # Share the snapshot with the target account
        response = ec2.modify_snapshot_attribute(
            SnapshotId=snapshot_id,
            Attribute='createVolumePermission',
            OperationType='add',
            UserIds=[target_account]
        )

        print(f"Shared snapshot {snapshot_id} with account {target_account}")
        return {
            'statusCode': 200,
            'body': 'Snapshot shared successfully'
        }
    except Exception as e:
        print(f"Error sharing snapshot: {e}")
        return {
            'statusCode': 500,
            'body': f"Error sharing snapshot: {e}"
        }

Here's how the code works:

  1. The function first retrieves the target account number from an environment variable named TARGET_ACCOUNT. This assumes that you have set this environment variable in your Lambda function configuration.

  2. You'll need to replace 'your-snapshot-id' with the actual ID of the snapshot you want to share.

  3. The modify_snapshot_attribute method is used to share the snapshot with the target account. The Attribute parameter is set to 'createVolumePermission', which allows the target account to create volumes from the shared snapshot. The OperationType parameter is set to 'add' to grant the permission, and the UserIds parameter is a list containing the target account ID.

  4. If the operation is successful, the function returns a success response with a status code of 200 and a message indicating that the snapshot was shared successfully.

  5. If an exception occurs during the process, the function returns an error response with a status code of 500 and the error message.

Make sure to replace the 'your-snapshot-id' placeholder with the actual ID of the snapshot you want to share, and ensure that the TARGET_ACCOUNT environment variable is set correctly in your Lambda function configuration.

AWS
专家
已回答 6 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则