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ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ